def geolocate(address, try_all=True): try: # see https://astral.readthedocs.io/en/latest/#cities a = lookup(address, database()) return a.latitude, a.longitude except: pass # use online geocoder location_data = geocoder.geonames(address, method='details', key='jarbas') if not location_data.ok: location_data = geocoder.geocodefarm(address) if not location_data.ok: location_data = geocoder.osm(address) if try_all: # more are just making it slow if not location_data.ok: location_data = geocoder.google(address) if not location_data.ok: location_data = geocoder.arcgis(address) if not location_data.ok: location_data = geocoder.bing(address) if not location_data.ok: location_data = geocoder.canadapost(address) if not location_data.ok: location_data = geocoder.yandex(address) if not location_data.ok: location_data = geocoder.tgos(address) if location_data.ok: location_data = location_data.json lat = location_data.get("lat") lon = location_data.get("lng") return lat, lon raise ValueError
def geolocate(address, service="geocodefarm"): data = {} if service == "geocodefarm": location_data = geocoder.geocodefarm(address) elif service == "osm": location_data = geocoder.osm(address) elif service == "google": location_data = geocoder.google(address) elif service == "arcis": location_data = geocoder.arcgis(address) elif service == "bing": location_data = geocoder.bing(address) elif service == "canadapost": location_data = geocoder.canadapost(address) elif service == "yandex": location_data = geocoder.yandex(address) elif service == "tgos": location_data = geocoder.tgos(address) else: raise ValueError("Unknown geocoder service") if location_data.ok: location_data = location_data.json data["country"] = location_data.get("country") data["country_code"] = location_data.get("country_code") data["region"] = location_data.get("region") data["address"] = location_data.get("address") data["state"] = location_data.get("state") data["confidence"] = location_data.get("confidence") data["lat"] = location_data.get("lat") data["lon"] = location_data.get("lng") data["city"] = location_data.get("city") data["postal"] = location_data.get("postal") data["timezone"] = location_data.get("timezone_short") else: return None location = { "city": { "name": data["city"], "state": { "name": data["state"], "country": { "code": data["country_code"], "name": data["country"] } } }, "coordinate": { "latitude": data["lat"], "longitude": data["lon"] }, "timezone": { "name": data["timezone"] } } return location
def test_canadapost(): url_1 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Find/v2.10/json3ex.ws?Key=fake&LastId=&Country=ca&SearchFor=Everything&SearchTerm=453+Booth+Street%2C+ON&LanguagePreference=en&%24cache=true' url_2 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Find/v2.10/json3ex.ws?Key=fake&LastId=CA%7CCP%7CENG%7CON-OTTAWA-BOOTH_ST-453&Country=ca&SearchFor=Everything&SearchTerm=453+Booth+Street%2C+ON&LanguagePreference=en&%24cache=true' url_3 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/RetrieveFormatted/v2.10/json3ex.ws?Key=fake&Id=CA%7CCP%7CB%7C80225509&Source=&MaxResults=3&cache=true' data_file_1 = 'tests/results/canadapost_find_1.json' data_file_2 = 'tests/results/canadapost_find_2.json' data_file_3 = 'tests/results/canadapost_retrieve.json' with requests_mock.Mocker() as mocker, open(data_file_1, 'r') as input_1, open(data_file_2, 'r') as input_2, open(data_file_3, 'r') as input_3: mocker.get(url_1, text=input_1.read()) mocker.get(url_2, text=input_2.read()) mocker.get(url_3, text=input_3.read()) g = geocoder.canadapost(location, key='fake', maxRows=3) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 6 assert fields_count >= 15
def test_canadapost(): url_1 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Find/v2.10/json3ex.ws?Key=fake&LastId=&Country=ca&SearchFor=Everything&SearchTerm=453+Booth+Street%2C+ON&LanguagePreference=en&%24cache=true' url_2 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Find/v2.10/json3ex.ws?Key=fake&LastId=CA%7CCP%7CENG%7CON-OTTAWA-BOOTH_ST-453&Country=ca&SearchFor=Everything&SearchTerm=453+Booth+Street%2C+ON&LanguagePreference=en&%24cache=true' url_3 = 'https://ws1.postescanada-canadapost.ca/AddressComplete/Interactive/RetrieveFormatted/v2.10/json3ex.ws?Key=fake&Id=CA%7CCP%7CB%7C80225509&Source=&MaxResults=3&cache=true' data_file_1 = 'tests/results/canadapost_find_1.json' data_file_2 = 'tests/results/canadapost_find_2.json' data_file_3 = 'tests/results/canadapost_retrieve.json' with requests_mock.Mocker() as mocker, open( data_file_1, 'r') as input_1, open(data_file_2, 'r') as input_2, open(data_file_3, 'r') as input_3: mocker.get(url_1, text=input_1.read()) mocker.get(url_2, text=input_2.read()) mocker.get(url_3, text=input_3.read()) g = geocoder.canadapost(location, key='fake', maxRows=3) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count == 6 assert fields_count == 15
def geolocate(self, address, try_all=True): if address in self.geocache: return self.geocache[address] try: # should be installed from default skills from astral.geocoder import database, lookup # see https://astral.readthedocs.io/en/latest/#cities a = lookup(address, database()) self.geocache[address] = (a.latitude, a.longitude) return a.latitude, a.longitude except: pass # use online geocoder location_data = geocoder.osm(address) if not location_data.ok: location_data = geocoder.geocodefarm(address) if try_all: # more are just making it slow if not location_data.ok: location_data = geocoder.google(address) if not location_data.ok: location_data = geocoder.arcgis(address) if not location_data.ok: location_data = geocoder.bing(address) if not location_data.ok: location_data = geocoder.canadapost(address) if not location_data.ok: location_data = geocoder.yandex(address) if not location_data.ok: location_data = geocoder.tgos(address) if location_data.ok: location_data = location_data.json lat = location_data.get("lat") lon = location_data.get("lng") self.geocache[address] = (lat, lon) return lat, lon raise ValueError
def test_canadapost(): g = geocoder.canadapost(address) assert g.ok