def connect( self, api_key, mode="google", domain: str = "maps.googleapis.com", min_delay_seconds: int = 1, **kwargs, ): if min_delay_seconds < 1: error_text = f"Minimum 1 second delay is required not {min_delay_seconds}" self.print_error(error_text) return None if mode == "google": from geopy.geocoders import GoogleV3 client = GoogleV3(api_key=api_key, domain=domain, **kwargs) self.client = RateLimiter( client.geocode, min_delay_seconds=min_delay_seconds ) elif mode == "som": from geopy.geocoders import Nominatim client = Nominatim(user_agent=api_key, **kwargs) self.client = RateLimiter( client.geocode, min_delay_seconds=min_delay_seconds ) else: error_text = "mode should be osm or google" self.print_error(error_text) self.connected = True return self
def test_max_retries(self): self.mock_clock.return_value = 1 rl = RateLimiter(self.mock_func, max_retries=3, return_value_on_exception=sentinel.return_value) # Non-geopy errors must not be swallowed self.mock_func.side_effect = ValueError with self.assertRaises(ValueError): rl(sentinel.arg) self.assertEqual(1, self.mock_func.call_count) self.mock_func.reset_mock() # geopy errors must be swallowed and retried self.mock_func.side_effect = GeocoderServiceError self.assertEqual(sentinel.return_value, rl(sentinel.arg)) self.assertEqual(4, self.mock_func.call_count) self.mock_func.reset_mock() # Successful value must be returned self.mock_func.side_effect = [ GeocoderServiceError, GeocoderServiceError, sentinel.good ] self.assertEqual(sentinel.good, rl(sentinel.arg)) self.assertEqual(3, self.mock_func.call_count) self.mock_func.reset_mock() # When swallowing is disabled, the exception must be raised rl.swallow_exceptions = False self.mock_func.side_effect = GeocoderQuotaExceeded with self.assertRaises(GeocoderQuotaExceeded): rl(sentinel.arg) self.assertEqual(4, self.mock_func.call_count) self.mock_func.reset_mock()
def __init__(self, cnx_pool: mysql.connector.pooling, seconds_delay: int = 3): self.cnx_pool = cnx_pool self.table_name = "missing_postcode" self.geolocator = Nominatim(user_agent="machine_learning_project_uz") self.geocode = RateLimiter(self.geolocator.geocode, min_delay_seconds=seconds_delay, max_retries=20, error_wait_seconds=10.0) self.reverse = RateLimiter(self.geolocator.reverse, min_delay_seconds=seconds_delay, max_retries=20, error_wait_seconds=10.0)
def geocode(addrs, doLog=False): geolocator = GoogleV3(api_key='AIzaSyAPXkMfUe9cobQ86mNNwRjPVUKgE_BENaw') def printDotGeocode(s): print('.', end='', flush=True) return geolocator.geocode(s) if doLog: geocode = RateLimiter(printDotGeocode, min_delay_seconds=0.005) else: geocode = RateLimiter(geolocator.geocode, min_delay_seconds=0.005) locs = addrs.apply(geocode) points = [(l.latitude, l.longitude) if l else (None, None) for l in locs] return points
def create_map(lst): """ (lst) -> None This function creates map with markers as html-file using list of names and locations """ geolocator = Nominatim(user_agent="specify_your_app_name_here") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) map = folium.Map() lay = folium.FeatureGroup(name="Friends_map") for el in lst: try: location = geolocator.geocode(el[1]) if location != None: lat, long = location.latitude, location.longitude lay.add_child( folium.Marker(location=[lat, long], popup=el[0], icon=folium.Icon())) except GeocoderTimedOut: continue map.add_child(lay) map.save("templates/map.html")
def page_second(): st.title("This is my second page") # ... street = st.sidebar.text_input("Street", "") city = st.sidebar.text_input("City", "") province = st.sidebar.text_input("Province", "") country = st.sidebar.text_input("Country", "") geolocator = Nominatim(user_agent="GTA Lookup") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) location = geolocator.geocode(street + ", " + city + ", " + province + ", " + country) st.write(location) lat = location.latitude lon = location.longitude st.write('lat: ' + str(lat)) st.write('lon: ' + str(lon)) map_data = pd.DataFrame({'lat': [lat], 'lon': [lon]}) # st.map(map_data) st.map(map_data, zoom=12)
def main(): """ An interactive function of the module. """ geolocator = Nominatim(user_agent="Closest films locator.") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) ans = input("Do you want to find your location by name? (Y/n): ") if ans in ["y", "Y", ""]: loc_str = input("Enter your current location in English: ") match = geocode(loc_str) if match is not None: latitude, longitude = match.latitude, match.longitude else: print("Error while finding your location.") latitude = float(input("Please enter your current latitude: ")) longitude = float(input("Please enter your current longitude: ")) else: latitude = float(input("Please enter your current latitude: ")) longitude = float(input("Please enter your current longitude: ")) location = geolocator.reverse(f"{latitude}, {longitude}", language='en') map_html_name = 'map.html' print("Generating the map, please wait...") generate_html(location, geocode, latitude=latitude, longitude=longitude, html_path=map_html_name) print(f"Your map is saved in: {map_html_name}")
def geocode(address, attempt=1, max_attempts=5): try: return RateLimiter(geolocator.geocode, min_delay_seconds=1)(address) except GeocoderTimedOut: if attempt <= max_attempts: return geocode(address, attempt=attempt + 1) raise
def getCountry(text): #geo_keys = 'name1', 'name2', 'name3', 'name4', 'name5', n # ] # Put in your API keys as strings key = 'AkuTMRwxGjK2-wtHvgeCMcAoYBVvEB0XF0CaW-q_7u2EHSIfVhKHCwlD5FtF63FR' if text[0] == 'NIL': value = text[1] else: value = text[0] from geopy.geocoders import Bing #geopy.geocoders.options.default_proxies = {"http": "http://37.48.118.4:13010"} geolocator = Bing(api_key=key) geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) location = geocode(value) if location is not None: location = location.raw else: return 'None' #geocode = RateLimiter(geolocator.reverse, min_delay_seconds=1) #lat = location['lat'] #lon = location['lon'] #location = geocode((lat, lon), language='en', exactly_one=True) #location = location.raw #country = location['address']['country'] try: country = location['address']['countryRegion'] return country except KeyError: return text
def test_min_delay(self): min_delay = 3.5 self.mock_clock.side_effect = [1] rl = RateLimiter(self.mock_func, min_delay_seconds=min_delay) # First call -- no delay clock_first = 10 self.mock_clock.side_effect = [clock_first, clock_first] # no delay here rl(sentinel.arg, kwa=sentinel.kwa) self.mock_sleep.assert_not_called() self.mock_func.assert_called_once_with(sentinel.arg, kwa=sentinel.kwa) # Second call after min_delay/3 seconds -- should be delayed clock_second = clock_first + (min_delay / 3) self.mock_clock.side_effect = [clock_second, clock_first + min_delay] rl(sentinel.arg, kwa=sentinel.kwa) self.mock_sleep.assert_called_with(min_delay - (clock_second - clock_first)) self.mock_sleep.reset_mock() # Third call after min_delay*2 seconds -- no delay again clock_third = clock_first + min_delay + min_delay * 2 self.mock_clock.side_effect = [clock_third, clock_third] rl(sentinel.arg, kwa=sentinel.kwa) self.mock_sleep.assert_not_called()
def main(clinics_xlsx, output_shp): df = pd.read_excel(clinics_xlsx) df = df.rename(columns={col: col.lower().replace(' ', '_') for col in df.columns}) geocode = RateLimiter(Nominatim(user_agent='MGGG covid-analysis').geocode, min_delay_seconds=1) geometries = [] for idx, row in tqdm(enumerate(df.itertuples())): loc = geocode({ 'state': getattr(row, 'state'), 'city': getattr(row, 'city'), 'address': getattr(row, 'address') }, ['US']) if loc is None: # A few facilities in the 2020-05-27 version of the # spreadsheets have incorrect state columns. # It's worth retrying without a state specified. loc = geocode({ 'city': getattr(row, 'city'), 'address': getattr(row, 'address') }, ['US']) if loc is not None: geometries.append(Point(*loc[1])) else: print(f'Warning: could not geocode row {idx}.') print(row) geometries.append(None) df['geometry'] = geometries gdf = gpd.GeoDataFrame(df) gdf.crs = 'EPSG:4326' # long/lat projection gdf.to_file(output_shp)
def generate_friends_data(friends_dict: dict) -> list: ''' Get dictionary with info about friends from twitter, geocode their coordinates, and return tuple with them and nickname. ''' geolocator = geopy.Nominatim(user_agent='my-aplication') geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) result = [] used_locations = [] for user in friends_dict: try: location = geolocator.geocode(user['location']) coordinates = [location.latitude, location.longitude] if coordinates in used_locations: coordinates[0] += random.randrange(1, 2) / 10000 coordinates[1] += random.randrange(1, 2) / 10000 used_locations.append(coordinates) result.append( (user['screen_name'], coordinates[0], coordinates[1])) except: continue return result
def get_geocoder(email): """Get geocode function for geocoding through Nominatim with supplied email address as the user_agent, as per Nominatim's usage policy. This geocoder will take at least 1 second per Address queried in accordance with Nominatim's terms of service. Note: this process cannot be multi-threaded or run in parallel. Arguments --------- email : str A string email Address supplied to Nominatim as user_agent Examples -------- >>> email = "*****@*****.**" >>> geocode = get_geocoder(email) >>> geocode("1315 10th St, Sacramento, CA 95814") Location(California State Capitol, 1315, 10th Street, Land Park, Sacramento, Sacramento County, California, 95814, United States of America, (38.57656885, -121.4934010890531, 0.0)) >>> email = "not_an_email" >>> geocode = get_geocoder(email) AssertionError: Must enter a valid email """ #Must enter a valid email assert is_valid_email(email), "Must enter a valid email" geolocator = Nominatim(user_agent=email) geocoder = RateLimiter(geolocator.geocode, min_delay_seconds=1) return geocoder
def use_osm(self, user_agent: str, min_delay_seconds: int = 1, **kwargs): if min_delay_seconds < 1: raise ValueError("Minimum 1 second delay is required for osm") from geopy.geocoders import Nominatim client = Nominatim(user_agent=user_agent, **kwargs) self.client = RateLimiter(client.geocode, min_delay_seconds=min_delay_seconds)
def run(self): geocode = RateLimiter(self.geolocator.geocode, min_delay_seconds=1) geocode = partial(self.geolocator.geocode, language="en") reverse = partial(self.geolocator.reverse, language="en") print("{} init".format(self.geolocator)) self.dataframe["geopy"] = self.dataframe["location"].apply(self.findBorough)
def geolocc(file, year, user_inp): df = modify_df(file, year) geolocator = Nominatim(user_agent="Map_app") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1 / 20) places = list(df['PLACES']) coor = [] # for i in places: try: res_0 = str(geolocator.geocode(i).latitude) res_1 = str(geolocator.geocode(i).longitude) res = res_0, res_1 coor.append(res) except AttributeError: coor.append('None') df['COORDINATES'] = coor df = df[df['COORDINATES'] != 'None'] distance = [] for elem in list(df['COORDINATES']): miles = geodesic(user_inp, elem).miles distance.append(round(miles, 2)) # distance_1 = [round(dist, 2) for dist in distance] df['DISTANCE'] = distance df = df.sort_values(by=['DISTANCE']) df = df[:10] return df
def main(): get_column_name() df = pandas.read_csv('cmc.csv', encoding='utf-8') nom = Nominatim( user_agent= "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/19.9.3.314 Yowser/2.5 Safari/537.36" ) a = 3 i = 0 while a <= 50: #len(df) + 1 sleep(uniform(1, 3)) geocode = RateLimiter(nom.geocode, min_delay_seconds=1) while i < a: # df.iat[i,3] i - строки и 3 столбец geocoder_result = nom.geocode(df.iat[i, 3], timeout=10) try: address = geocoder_result.address coordinates = str(geocoder_result.latitude) + ', ' + str( geocoder_result.longitude) data = {'address': address, 'coordinates': coordinates} write_csv(data) except: lat = None lon = None address = None coordinates = str(lat) + ', ' + str(lon) data = {'address': address, 'coordinates': coordinates} write_csv(data) i = i + 1 a = a + 3
def add_coords(filtered_lst): """ Find coordinates for list of films """ for line in filtered_lst: try: location = line[2] location = location.split(", ") geolocator = geopy.Nominatim(user_agent="main.py") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=0.05) adres = geolocator.geocode(location) if adres == None: location = location[1:] adres = geolocator.geocode(location) if adres == None: location = location[1:] adres = geolocator.geocode(location) coords = (float(adres.latitude), float(adres.longitude)) line.append(coords) except GeocoderUnavailable: line.append("error") return filtered_lst
def locations_to_coordinates(dct_loc): """ dct -> dct This functions transform dict with keys= location to the dict with keys =(latitude, longitude) and values = films made in that cities """ to_retry = dict() coordinates = dict() for loc in dct_loc: l = loc.split(',') la = False lo = False try: geolocator = Nominatim(user_agent="hello_it_is_me") geocode = RateLimiter(geolocator.geocode, error_wait_seconds=2.0, max_retries=0, swallow_exceptions=False, return_value_on_exception=True) location = geolocator.geocode(l[0]) la = location.latitude lo = location.longitude except Exception as err: try: location = geolocator.geocode(l[1]) la = location.latitude lo = location.longitude except Exception as err2: to_retry[loc] = dct_loc[loc] if la and lo: coordinates[(la, lo)] = '| '.join(dct_loc[loc]) return coordinates, to_retry
def get_reverse_geocoder(email): """Get reverse geocode function for reverse geocoding through Nominatim with supplied email address as the user_agent, as per Nominatim's usage policy. This reverse geocoder will take at least 1 second per Address queried in accordance with Nominatim's terms of service. Note: this process cannot be multi-threaded or run in parallel. Arguments --------- email : str A string email Address supplied to Nominatim as user_agent Examples -------- >>> email = "*****@*****.**" >>> reverse_coder = get_reverse_geocoder(email) >>> reverse_coder("37.873621099999994, -122.25768131042068") Location(Evans Hall, Bechtel Drive, Northside, Berkeley, Alameda County, California, 94720, United States of America, (37.873621099999994, -122.25768131042068, 0.0)) >>> email = "not_an_email" >>> reverse_coder = get_reverse_geocoder(email) AssertionError: Must enter a valid email """ #Must enter a valid email assert is_valid_email(email), "Must enter a valid email" geolocator = Nominatim(user_agent=email) reverse_geocoder = RateLimiter(geolocator.reverse, min_delay_seconds=1) return reverse_geocoder
def countries_locations_to_coordinates(country_dict): """ dct -> dct This functions transform dict with keys= location to the dict with keys = (latitude, longitude) and values = films made in that country """ i = 0 to_retry = dict() coordinates = dict() for country in country_dict: la = False lo = False try: geolocator = Nominatim(user_agent="hello_it_is_me") geocode = RateLimiter(geolocator.geocode, error_wait_seconds=5.0, max_retries=0, swallow_exceptions=False, return_value_on_exception=True) location = geolocator.geocode(country) la = location.latitude lo = location.longitude i += 1 except Exception as err: pass to_retry[country] = country_dict[country] if la and lo: coordinates[(la, lo)] = '|'.join(country_dict[country]) return coordinates, to_retry
def getLocation(lat, lon): """ helper function to get address from lat and lon inputs: list of latitude numbers, list of longitude numbers returns string for address returns string with the address """ geolocator = Nominatim( user_agent="project1") # assign nominating as your geolocator location = (lat, lon) reverse = RateLimiter(partial(geolocator.reverse, addressdetails=False, zoom=17), max_retries=5, min_delay_seconds=1) location = reverse(location) if (location != None): street = location.address street = street.split(",") street = street[0] return street return location.address else: # no location found return 1
def coordinates(loc_user_dct): ''' (dict) -> dict Function that returns the dictionary with key that contains latitude and longitude of the friends of user and value is a list of friends that live in the same place. ''' geo_dct = {} geolocator = Nominatim(user_agent="specify_your_app_name_here") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) for key, value in loc_user_dct.items(): try: location = geolocator.geocode(key) tpl_loc = (location.latitude, location.longitude) geo_dct[tpl_loc] = value except Exception: continue new_dct = {'lat': [], 'lon': [], 'friends': []} for key, value in geo_dct.items(): key_loc = list(key) new_dct['lat'].append(key_loc[0]) new_dct['lon'].append(key_loc[1]) new_dct['friends'].append(value) return new_dct
def format_cols_meetup(cnames, data): geolocator = Nominatim(user_agent="bcneventer") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) location = [] for (loc1, loc2) in zip(data.coordinates0, data.coordinates2): try: location.append((float(loc1), float(loc2))) except: location.append((0, 0)) data["location"] = location data = data.rename( columns={ 'Event': cnames[0], 'location': cnames[2], 'Time': cnames[3], 'category': cnames[4] }) data.date_time = pd.to_datetime(data.date_time, unit='ms').astype(str) # add the address as the description column, as given by reverse geocoding the coordinates data[cnames[1]] = [ getAddress(str(location[0]), str(location[1])) for location in data.location ] #data[cnames[1]] = "no description" return (data)
def main(clinics_xlsx, output_shp): df = pd.read_excel(clinics_xlsx) df = df.rename( columns={col: col.lower().replace(' ', '_') for col in df.columns}) geocode = RateLimiter(Nominatim(user_agent='MGGG covid-analysis').geocode, min_delay_seconds=1) geometries = [] for idx, row in tqdm(enumerate(df.itertuples())): loc = geocode( { 'state': getattr(row, 'state'), 'city': getattr(row, 'city'), 'address': getattr(row, 'address') }, ['US']) if loc is not None: geometries.append(Point(loc[1][1], loc[1][0])) else: print(f'Warning: could not geocode row {idx}.') print(row) geometries.append(None) df['geometry'] = geometries gdf = gpd.GeoDataFrame(df) gdf.crs = 'EPSG:4326' # long/lat projection gdf.to_file(output_shp)
def locate(city): for location in locations: if location["city"] == city: return location["latitude"], location["longitude"] geolocator = Nominatim(user_agent="cphbusiness-python-exam-newbiz") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) country = "Denmark" loc = geocode(city + ", " + country) try: latitude = loc.latitude longitude = loc.longitude locations.append({ "city": city, "latitude": latitude, "longitude": longitude }) except: latitude = None longitude = None return latitude, longitude
def reverse_geocode(coordinates, attempt=1, max_attempts=5, languages=["en"], zoom=14): try: return RateLimiter(geolocator.reverse, min_delay_seconds=1)(coordinates, language=languages, zoom=zoom) except GeocoderTimedOut: if attempt <= max_attempts: return reverse_geocode(coordinates, attempt=attempt + 1, languages=languages, zoom=zoom) raise
def generate_location_coordinates(): location_file_path = Path(data_root).joinpath('input_csv', 'locations.csv') df = pd.read_csv(location_file_path) df['query_location'] = df.apply( lambda row: row['#name'] + ', ' + row['country'], axis=1) geolocator = Nominatim(user_agent='coordinates-mali') geocode = RateLimiter(geolocator.geocode, min_delay_seconds=2) df['location'] = df['query_location'].apply(geocode) df['geo_lon'] = df['location'].apply(lambda loc: loc.longitude if loc else None) df['geo_lat'] = df['location'].apply(lambda loc: loc.latitude if loc else None) df['geo_lon'].fillna(df['lon'], inplace=True) df['geo_lat'].fillna(df['lat'], inplace=True) result_df = df[[ '#name', 'country', 'lat', 'lon', 'geo_lat', 'geo_lon', 'location_type' ]] output_path = Path(output_dir).joinpath(output_filename) result_df.to_csv(output_path, index=False) print(f'{output_filename} generated in {output_path}')
def get_confirmed_cases(df): """ Uses the spreadsheet and scraper data to assign lat/lon locations to each area with a confirmed case """ # Group the health region and province together for geocoding and grouping by case df['health_region'] += ', ' + df[ 'province'] # Append province to health region for geocoding df_aggregate = df.groupby( 'health_region').size() # Aggregate by health province # Set up the geocoder, with a rate limiter to deal with timeout issues geo_coder = RateLimiter(Nominatim(user_agent="COVIDScript").geocode, min_delay_seconds=.5) # Start with ontario data confirmed_cases = get_ontario_confirmed_cases(geo_coder) confirmed_cases.extend( get_spreadsheet_confirmed_cases(df_aggregate, geo_coder)) return { 'last_updated': get_time_string(df), 'max_cases': int(df_aggregate.max()), 'confirmed_cases': confirmed_cases }
def find_coords(year, locations): """ This function takes the year and the list of locations to find and writes them in a separate csv file with name YEAR.csv and returns the list of coordinates for each :param year: 1909 < int number < 2018 :param locations: lst : [(location, year, movie), (),()] :return: lst: [(location, year, movie, latitude, longitude), (),()] """ file = str(year) + '.csv' geolocator = Nominatim(user_agent="film_data.py") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) with open(file, "a", encoding='UTF-8') as f: f.write('location,movie,year,lat,lon') for mv in locations: loc = mv[0] location = geolocator.geocode(loc) try: mv += (location.latitude, location.longitude) line = mv[0] + ',' + mv[1] + ',' + mv[2] + ',' +\ str(location.latitude) + ',' + str(location.longitude) + "\n" f.write(line) except: continue return locations