def search(self): # First, store the SearchQuerySet received from other processing. sqs = super(ResourceSearchForm, self).search() address = self.cleaned_data.get('a') if (address): g = geocoders.Nominatim( ) # this should be changed to openstreetmap try: place, (lat, lng) = g.geocode(address) print "address: %s, lat : %g, lng : %g" % (address, lat, lng) loc = Point(lng, lat) max_dist = D(km=10) #sqs = sqs.dwithin('location',loc,max_dist).distance('location',loc) except geopy.exc.GeocoderServiceError: pass time = self.cleaned_data.get('t') if (time): # extract serialized events from search query set index events = sqs.exclude(event='toto') excluded = list() for e in events: if e.event: # we only check if we can go to the next upcoming occurrence # checking all occurrences would be too costly ev = deserialize(e.event).after(datetime.now()) if not ev in time.occurrences(dtstart=ev): excluded.append(e.pk) if (excluded): sqs = sqs.exclude(id__in=excluded) return sqs
def filter_by_range(self, isvalidlocation): """Further filters out results to be within an arbitrary geographic distance. Note that this uses a shortest-line method of finding distance and does not account for road layout (or bodies of water)""" try: concertloc = geocoders.Nominatim( user_agent="testing_location_find_10230950239").geocode( isvalidlocation, True) except geopy_exc.GeocoderTimedOut: print('Locator timed out, waiting 30s before continuing') sleep(30) else: if concertloc: concertloc = (concertloc.latitude, concertloc.longitude) # TODO update to allow for user input of maximum range valid_range = 200 # miles dist = distance.distance(self.user_location[1], concertloc).miles print( f'Finding geographic distance between {self.user_location} and {concertloc}' ) sleep(5) return False if dist > valid_range else concertloc else: return False # alternate method for getting distances, currently excluded in the interest of not potentially abusing a free service if not True: dist = requests.get( f'https://www.distance-cities.com/distance-{" ".join(self.user_location[0])}-to-{"-".join(concertloc)}' )
def save(self, force_insert=False, force_update=False, using=None): if self.country: g = geocoders.Nominatim(user_agent="OEG-CRM") address_string = u"{}, {} {} {}, {}, {}".format( self.street_address, self.postal_code, self.postal_code_suffix, self.city, self.state_province, self.country.name.replace(", Republic of", ""), ) address_string = address_string.replace(", ,", ", ") try: place, (lat, lng) = g.geocode(address_string) if lat: self.latitude = lat self.longitude = lng except TypeError: pass super(Address, self).save(force_insert=force_insert, force_update=force_update, using=using)
def prepare_sel(self, assistant, page): address = self.vals["_address"].get_text() if not address: return try: agent = version.DRATS_NAME + '/' + version.DRATS_VERSION service = geocoders.Nominatim(user_agent=agent) places = service.geocode(address, exactly_one=False) self.set_page_complete(page, True) except URLError as err: printlog("Geocode", " : Did not find `%s': %s" % (address, err)) places = [] lat = lon = 0 self.set_page_complete(page, False) i = 0 self.vals["AddressList"].set_values([]) for place, (lat, lon) in places: i += 1 self.vals["AddressList"].add_item(place, lat, lon) if i == -1: page.hide() self.set_current_page(self.get_current_page() + 1)
def retrieve(self, address=None, google_api=None, language=None): self.geocoder = geocoders.Nominatim(user_agent='cpskinapp') if not address: raise GeocoderQueryError return self.geocoder.geocode(address, exactly_one=False, language=language)
def get_location(address: str): """Fetches location using geopy. :param address: Address :returns: Location :raises: geopy.exc.GeopyError """ geocoder = geocoders.Nominatim(user_agent="restaurants") return geocoder.geocode(address)
def make_from_slug(slug): g = geocoders.Nominatim() # this should be changed to openstreetmap m = re_latitude_longitude.match(slug) lat = m.group(1) lng = m.group(2) location = fromstr("POINT(%s %s)" % (lng, lat)) address = g.reverse("%s, %s" % (lat, lng), exactly_one=1) return GeoLocation(address=address, location=location)
def get_country_code(location): try: locator = gg.Nominatim(user_agent="myGeocoder") location = locator.geocode(location, addressdetails=True) print(location.raw["address"]["country_code"].upper()) return location.raw["address"]["country_code"].upper() except: print("") return ""
def geocode(self): self.lat = None self.lon = None if self.city_or_town and self.country: location = geocoders.Nominatim( timeout=10).geocode(f"{self.city_or_town}, {self.country}") if location: self.lat = location.latitude self.lon = location.longitude
def __init__(self, reverse_geo=False): """Initializer. Args: reverse_geo: Boolean flag defining whether to reverse lookup coordinates for output. Note that his is costly. """ self._reverse_geo = reverse_geo if self._reverse_geo: self._geolocator = geocoders.Nominatim()
def get_lat_lng_from_address(address): """Return tuple with status and geocoder object 0: error, 1: success, 2: not found, 3: unexpected error """ geocoder = geocoders.Nominatim( user_agent='{0}-cpskinapp'.format(api.portal.get().id), timeout=10, ) geocode = geocoder.geocode(safe_utf8(address)) return (1, geocode)
def geocode(city_or_town, country): if city_or_town and country: self.stdout.write(f"Geocoding: {city_or_town}, {country}") time.sleep(1) location = geocoders.Nominatim(timeout=10).geocode( f"{city_or_town}, {country}" ) if location: return {"lat": location.latitude, "lon": location.longitude} return {"lat": None, "lon": None}
def save(self, *args, **kwargs): g = geocoders.Nominatim() # this should be changed to openstreetmap try: place, (lat, lng) = g.geocode(self.address) self.location = fromstr("POINT(%s %s)" % (lng, lat)) self.address = place except geopy.exc.GeocoderServiceError: self.location = fromstr("POINT(0 0)") print "WARNING: could not geocode %s !!" % self.address super(GeoLocation, self).save(*args, **kwargs)
def geocode(self): self.lat = None self.lon = None if self.city_or_town and self.country: geocoders.options.default_user_agent = "eahub" location = geocoders.Nominatim( timeout=10).geocode(f"{self.city_or_town}, {self.country}") if location: self.lat = location.latitude self.lon = location.longitude
def geo_reverse(coordinates): """ Return decoded information for specified coordinates. """ geolocator = geocoders.Nominatim(user_agent="jammed") while True: try: address = geolocator.reverse(coordinates).address return address except exc.GeopyError: time.sleep(2.5)
def geolocate(wikidata, place): """Geolocate the PLACE in WIKIDATA""" geolocator = geo.Nominatim(user_agent='wikidata') geo.options.default_timeout = 10 geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) for data in wikidata: if data[place]: data['GeoData'] = geocode(data[place]) data['Latitude'] = data['GeoData'].latitude data['Longitude'] = data['GeoData'].longitude return wikidata
def getNominatimResults(place_name): g = geocoders.Nominatim() try: results = g.geocode(place_name, False, timeout=5) formatted_results = [] for result in results: formatted_results.append(formatExternalGeocode( 'Nominatim', result)) return formatted_results except Exception: return []
def geodecode(location): # check if location already resolved if location in resolved_locations: loc = resolved_locations.get(location, "none") else: g = geocoders.Nominatim(user_agent="testmyspecificCustomTestamoi") loc = g.geocode(location, timeout=10) # store location and coord resolved_locations[location] = loc return loc.latitude, loc.longitude
def user_location_set(self,location): # TODO - find out what this returns for non-existant places (i.e. typos in user input) """Finds user location (latitude,longitude) via Nominatim""" if location: userloc = geocoders.Nominatim(user_agent="testing_location_find_10230950239").geocode(location,True) self.user_location[0] = tuple(abv for abv in self.abbreviation_to_state.keys() if abv in location or self.abbreviation_to_state[abv] in location) if not self.user_location[0]: self.user_location[0] = 'none' self.user_location[1] = (userloc.latitude,userloc.longitude) else: self.user_location = ['Not Specified',('Not Specified','Not Specified')]
def find_coord(city_name): ''' Uses web service to calculate terminal coordinates from city name. Input: - city_name: City name from address_struct field Output: - lng: longitude of city - lat: latitude of city ''' geo_code = geocoders.Nominatim(user_agent="terminal_status") _, (lat, lng) = geo_code.geocode(city_name) return lat, lng
def __init__(self, name): self.name = name # get latitude, longitude geolocator = geocoders.Nominatim(user_agent=GEOLOC_APP_NAME) self.point = geolocator.geocode(self.name) if self.point == None: exit(f'ERROR: Can not find place by name.') # get timezone tf = TimezoneFinder() self.timezone = tf.timezone_at(lat=self.point.latitude, lng=self.point.longitude)
def get_lat_long(self): geolocator = geocoders.Nominatim(user_agent='city_finder') city_state = self.name + ', ' + self.state print(city_state) location = geolocator.geocode(city_state) try: self.latitude = location.latitude self.longitude = location.longitude except Exception as ex: template = "An exception of type {0} occurred. Did you make sure to enter city and state correctly? " \ "Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print(message) raise ex
def get_geocoordinates(data,column_name,lon_column_name, lat_column_name): # Initialize geolocator geolocator = geocoders.Nominatim(user_agent="xxx",timeout=None) # Generate longitude and latitude variables longitude = [geolocator.geocode(item).longitude for item in data[column_name]] latitude = [geolocator.geocode(item).latitude for item in data[column_name]] # Define in dataset data[lon_column_name] = longitude data[lat_column_name] = latitude # Return return data
def get_location(location_name): try: # check if location already resolved if location_name in resolved_locations: loc = resolved_locations.get(location_name, "none") else: g = geocoders.Nominatim(user_agent="dummy") loc = g.geocode(location_name, timeout=10) # store location and coord resolved_locations[location_name] = loc return jsonify(loc.latitude, loc.longitude) except: return jsonify(-99,99)
def get_timezone(self, locale): try: # This handles common city names, like "Dallas" or "Paris" return timezone(self.astral[locale].timezone) except: try: # This handles codes like "America/Los_Angeles" return timezone(locale) except: try: geolocator = geocoders.Nominatim() location = geolocator.geocode(locale).address return timezone(location) except Exception as e: self.log.error(e) return None
def getCityRegion(self): cityRegion = self.__data.loc[:, ["city", "region"]].apply( lambda l: " ".join(l), axis=1) g = geocoders.Nominatim() coord = [] for location in cityRegion: if system() == 'Windows': geocode = g.geocode("Самара Самарская область", timeout=10, language='pt') coord.append([geocode.latitude, geocode.longitude]) break geocode = g.geocode(location, timeout=10, language='en') coord.append([geocode.latitude, geocode.longitude]) #self.__dp['city_region'] = coord; return coord
def geocodeHS(geocoder, hs_id, lookup_hs_name): if geocoder == 'arcgis': geolocator = geocoders.ArcGIS() elif geocoder == 'baidu': geolocator = geocoders.baidu() elif geocoder == 'geocodefarm': print("Troubleshooting AttributeError: 'NoneType' object has no attribute 'replace'") exit(0) # geolocator = GeocodeFarm() elif geocoder == 'geocoderdotus': geolocator = geocoders.GeocoderDotUS() elif geocoder == 'geonames': geolocator = geocoders.geonames() elif geocoder == 'googlev3': geolocator = geocoders.GoogleV3() elif geocoder == 'nominatim': geolocator = geocoders.Nominatim() elif geocoder == 'openmapquest': geolocator = geocoders.OpenMapQuest() elif geocoder == 'yandex': print('Do not use. Russian site') exit(1) else: print('invalid geodocder specified') exit(1) try: return geolocator.geocode(lookup_hs_name, exactly_one=True, timeout=10) except exc.GeocoderQuotaExceeded: print(geocoder, ':\tGeocoderQuotaExceeded') exit(1) except exc.ConfigurationError: print(geocoder, ':\tConfigurationError') exit(1) except exc.GeocoderAuthenticationFailure: print(geocoder, ':\tGeocoderAuthenticationFailure') exit(1) except exc.GeocoderTimedOut: print(geocoder, ':\tGeocoderTimedOut') exit(1) except exc.GeocoderUnavailable: print(geocoder, ':\tGeocoderUnavailable') exit(1) except exc.GeocoderInsufficientPrivileges: print(geocoder, ':\tGeocderInsufficientPrivileges') except: return None
def lookup_address(address) -> Optional[dict]: """ Looks up the coordinates of a given address. Uses cache for cached addresses. This will block until NOMINATIM_REQUEST_DELAY passes since last request :param address: The address to lookup """ location = LocationCache.get_location(address) if location is None: # Sleep if necessary time_since_last = (time.time() - AddressLookup.last_nom_request) if time_since_last < NOMINATIM_REQUEST_DELAY: time.sleep(NOMINATIM_REQUEST_DELAY - time_since_last) # Try to retrieve the address from Nominatim location_obj = None try: geolocator = gc.Nominatim(user_agent="pyagent") location_obj = geolocator.geocode(address, addressdetails=True) except geopy.exc.ConfigurationError as e: logger.error( "Geocoder error looking up address {0}: {1}".format( address, e)) location_obj = None except geopy.exc.GeocoderTimedOut as e: logger.error("Geocoder timed out for address {0}: {1}".format( address, e)) location_obj = None AddressLookup.last_nom_request = time.time() if location_obj: location = AddressLookup.extract_address_dict(location_obj) else: location = {} LocationCache.add_to_cache(address, location) if not location and location is not None: return None return location else: if not location and location is not None: logger.warning( "Address {0} is in cache as invalid, you may need" " to clear the location.json cache if this is a valid address" .format(address)) return None return location
def nominatim( ctx, database, table, location, delay, latitude, longitude, geojson, spatialite, user_agent, domain, ): "Nominatim (OSM)" click.echo(f"Using Nominatim geocoder at {domain}") fill_context(ctx, database, table, location, delay, latitude, longitude, geojson, spatialite) return geocoders.Nominatim(user_agent=user_agent, domain=domain)
def save(self, *args, **kwargs): # Get user's local timezone if not self.pk: try: locator = geocoders.Nominatim(user_agent=APP_NAME) address = "{user.city}, {user.province}, {user.country}".format( user=self.beamtime.project) _, (latitude, longitude) = locator.geocode(address) usertz = tf.certain_timezone_at( lat=latitude, lng=longitude) or settings.TIME_ZONE except: usertz = settings.TIME_ZONE t = self.beamtime.start - timedelta( days=7 + (self.beamtime.start.weekday() > 4 and self.beamtime.start.weekday() - 4 or 0)) self.send_time = pytz.timezone(usertz).localize( datetime(year=t.year, month=t.month, day=t.day, hour=10)) self.email_subject = self.beamtime.info_subject() self.email_body = self.beamtime.info_body() super().save(*args, **kwargs)