geo.py: add crosses_dateline and get_area

This commit is contained in:
rolux 2012-09-01 05:01:08 +02:00
parent f5ad5d73e4
commit fb26797b95

View file

@ -2,6 +2,8 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2008 # GPL 2008
import math
__all__ = ['get_country', 'get_country_name'] __all__ = ['get_country', 'get_country_name']
''' '''
@ -1311,6 +1313,26 @@ COUNTRIES = {
} }
} }
# See http://en.wikipedia.org/wiki/WGS-84
EARTH_RADIUS = 6378137
def crosses_dateline(west, east):
return west['lng'] > east['lng']
def get_area(southwest, northeast):
def radians(point):
return {
'lat': math.radians(point['lat']),
'lng': math.radians(point['lng'])
}
if crosses_dateline(southwest, northeast):
northeast['lng'] += 360
southwest = radians(southwest)
northeast = radians(northeast)
return math.pow(EARTH_RADIUS, 2) * abs(
math.sin(southwest['lat']) - math.sin(northeast['lat'])
) * abs(southwest['lng'] - northeast['lng']);
def get_country(country_code): def get_country(country_code):
country_code = country_code.upper() country_code = country_code.upper()
return COUNTRIES[country_code] if country_code in COUNTRIES else {} return COUNTRIES[country_code] if country_code in COUNTRIES else {}