def get_geodata(location, fallback_city_name): search_str = '' if 'street' in location: search_str += location['street'] if 'house_number' in location: search_str += ' ' + location['house_number'] if 'postcode' in location: search_str += ', ' + location['postcode'] + ' ' + location['city'] elif 'city' in location: search_str += ', ' + location['city'] else: search_str += ', ' + fallback_city_name elif 'name' in location: search_str += location['name'] + ', ' + fallback_city_name search_str += ', ' + settings.GEO_SEARCH_COUNTRY geolocator = OpenCage(settings.OPENCAGEDATA_KEY) location = geolocator.geocode(search_str, language="de", exactly_one=False) if len(location) == 0: return None return { 'lat': location[0].latitude, 'lng': location[0].longitude, }
class OpenCageApi(object): def __init__(self, api_key): self.geolocator = OpenCage(api_key=api_key) def get_location_information(self, lat=None, long=None, city=None, country=None): location = None if lat and long: location = self.geolocator.reverse(query=(lat, long)) elif city or country: location = self.geolocator.geocode(query=f'{city} {country}') return { 'country': location.raw.get('components').get('country'), 'continent': location.raw.get('components').get('continent'), 'latitude': location.latitude, 'longitude': location.longitude, }
def get_geolocator(fallback=False): if settings.GEOEXTRACT_ENGINE.lower() == 'opencagedata' and not fallback: if not settings.OPENCAGEDATA_KEY: raise ValueError( "OpenCage Data is selected as Geocoder, however no OPENCAGEDATA_KEY is set" ) geolocator = OpenCage(settings.OPENCAGEDATA_KEY) else: geolocator = Nominatim() return geolocator
def get_geolocator(fallback=False): if settings.GEOEXTRACT_ENGINE.lower() == "opencage" and not fallback: if not settings.OPENCAGE_KEY: raise ValueError( "OpenCage Data is selected as Geocoder, however no OPENCAGE_KEY is set" ) geolocator = OpenCage(settings.OPENCAGE_KEY) else: geolocator = Nominatim(user_agent=slugify(settings.PRODUCT_NAME) + "/1.0") return geolocator
def __init__(self, raw_data, api_key): # Declaring the instance data: self.raw_data = raw_data self.api_key = api_key # Declaring instance of geocoder: OpenCage self.geolocator = OpenCage(self.api_key) # Adding rate limiter to avoid API bottleneck: self.geocode = RateLimiter(self.geolocator.geocode, min_delay_seconds=1) # Building the transformed dataframe: self.data = self.build_geoprocessed_data()
class OpenCage(Provider): def __init__(self): self.geocoder = OpenCage_geopy(api_key=secrets['api-key']['opencage']) def raw_location_keys(self) -> Tuple: return 'bounds', 'components', 'confidence', 'formatted', 'geometry' def max_requests(self) -> int: return internal_config['max-requests'] def identifier(self) -> str: return 'opencage' def geocode(self, address) -> Location: return self.geocoder.geocode(address, country="ie")
def latlng_to_address(lat, lng) -> str: search_str = str(lat) + ", " + str(lng) if settings.GEOEXTRACT_ENGINE == "opencage": if not settings.OPENCAGE_KEY: raise ValueError( "OpenCage Data is selected as Geocoder, however no OPENCAGE_KEY is set" ) location = OpenCage(settings.OPENCAGE_KEY).reverse(search_str) if location: return _format_opencage_location(location) else: location = Nominatim(user_agent=slugify(settings.PRODUCT_NAME) + "/1.0").reverse(search_str) if len(location) > 0: return _format_nominatim_location(location[0]) return search_str
def get_geolocators() -> List[Tuple[str, Geocoder]]: geolocators = [] if settings.GEOEXTRACT_ENGINE == "opencage": if not settings.OPENCAGE_KEY: raise ValueError( "OpenCage Data is selected as Geocoder, however no OPENCAGE_KEY is set" ) geolocators.append(("opencage", OpenCage(settings.OPENCAGE_KEY))) if settings.MAPBOX_TOKEN: geolocators.append(("mapbox", MapBox(settings.MAPBOX_TOKEN))) nominatim_url = urlparse(settings.NOMINATIM_URL) geolocators.append(( "nominatim", Nominatim( user_agent=slugify(settings.PRODUCT_NAME) + "/1.0", domain=nominatim_url.netloc, scheme=nominatim_url.scheme, ), )) return geolocators
def __init__(self, api_key): self.geolocator = OpenCage(api_key=api_key)
def __init__(self): self.geocoder = OpenCage_geopy(api_key=secrets['api-key']['opencage'])
def get_geolocator(): # @TODO Support other geocoders as well geolocator = OpenCage(settings.OPENCAGEDATA_KEY) return geolocator