def gps_coord(self, address): from pygeocode import geocoder import datetime if not address: return None coord = None geolut = self.geolut try: coord = geolut['address'][address] except: today = datetime.date.today() if geolut['lastquery'] == today and not geolut['querylimit']: return None else: geolut['lastquery'] = today geolut['querylimit'] = 2500 try: result = geocoder.geocode_google(address) except geocoder.GeocoderRateLimitError: geolut['querylimit'] = 0 return None except: result = False if result: coord = (result['lat'], result['lng']) else: coord = False geolut['address'][address] = coord geolut['querylimit'] -= 1 return coord
def gps_coord(self,address): "lookup GPS coordinates for address in LUT, if necessary from Google" from pygeocode import geocoder import datetime if not address: return None coord = None geolut = self.geolut try: coord = geolut['address'][address] except: today = datetime.date.today() if geolut['lastquery']==today and not geolut['querylimit']: return None else: geolut['lastquery'] = today geolut['querylimit'] = 2500 try: result = geocoder.geocode_google(address) except geocoder.GeocoderRateLimitError: geolut['querylimit'] = 0 return None except: result = False if result: coord = (result['lat'],result['lng']) else: coord = False geolut['address'][address] = coord geolut['querylimit'] -= 1 return coord
def test_google_geocode_zero_result(self): fake_urllib2 = fudge.Fake('urllib2') fake_urllib2.remember_order() urlopen = fake_urllib2.expects('urlopen') urlopen.with_args( 'http://maps.googleapis.com/maps/api/geocode/json?' 'address=foo+bar' '&sensor=false' ) res = """{"status": "ZERO_RESULTS", "results": []}""" urlopen.returns(StringIO(res)) data = geocoder.geocode_google( 'foo bar', _urllib2=fake_urllib2, ) eq(None, data)
def test_google_geocode_simple(self): fake_urllib2 = fudge.Fake('urllib2') fake_urllib2.remember_order() urlopen = fake_urllib2.expects('urlopen') urlopen.with_args( 'http://maps.googleapis.com/maps/api/geocode/json?' 'address=1600+Amphitheatre+Parkway%2C+Mountain+View%2C+CA' '&sensor=false' ) res = """{"status": "OK", "results": [{"geometry": {"location": {"lat": 37.421972, "lng": -122.084143}, "viewport": {"northeast": {"lat": 37.4251196, "lng": -122.0809954}, "southwest": {"lat": 37.4188244, "lng": -122.0872906}}, "location_type": "ROOFTOP"}, "address_components": [{"long_name": "1600", "types": ["street_number"], "short_name": "1600"}, {"long_name": "Amphitheatre Pkwy", "types": ["route"], "short_name": "Amphitheatre Pkwy"}, {"long_name": "Mountain View", "types": ["locality", "political"], "short_name": "Mountain View"}, {"long_name": "California", "types": ["administrative_area_level_1", "political"], "short_name": "CA"}, {"long_name": "United States", "types": ["country", "political"], "short_name": "US"}, {"long_name": "94043", "types": ["postal_code"], "short_name": "94043"}], "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "types": ["street_address"]}]}""" urlopen.returns(StringIO(res)) data = geocoder.geocode_google( '1600 Amphitheatre Parkway, Mountain View, CA', _urllib2=fake_urllib2, ) expected = OrderedDict([ ('lat', 37.4219720), ('lng', -122.0841430), ]) eq(data, expected)