1

When trying to do an assignment on the intro to Python GIS (https://automating-gis-processes.github.io/CSC18/lessons/L3/geocoding.html), Python errors. Geocoding addresses with Nominatim backend gives the error of violating Nominatim's ToS. I understand that it requires a user agent but everything I found fails.

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

fp = '/ownpath/addresses.txt/'
data = pd.read_csv(fp, sep = ';')

Now after entering the required code into the IPython console

from geopandas.tools import geocode
geo = geocode(data['addr'], provider = 'nominatim')

I get the error:

ConfigurationError: Using Nominatim with default or sample `user_agent` "geopy/2.0.0" is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"`.

Trying to change the user agent with these examples as it says in the error as well as ways for trying to fix the user agent do not work. I am new to GIS and do not know how to continue. Can anyone fix this problem?

Sterewan
  • 19
  • 2
  • Please read the ToS, which can be found at the link in the error message. According to the ToS you need to provide your own unique UserAgent. It can be any string, but it has to be created by yourself. – KostyaEsmukov Aug 04 '20 at 21:42

4 Answers4

2

Replace geolocator = Nominatim() with geolocator = Nominatim(user_agent="Your_app-name")

For Example,

address = 'Scarborough,Toronto'

geolocator = Nominatim()
location = geolocator.geocode(address)
latitude_x = location.latitude
longitude_y = location.longitude
print('The Geograpical Co-ordinate of Seattle,Washington are {}, {}.'.format(latitude_x, longitude_y))

will become

address = 'Scarborough,Toronto'

geolocator = Nominatim(user_agent="smy-application")
location = geolocator.geocode(address)
latitude_x = location.latitude
longitude_y = location.longitude
print('The Geograpical Co-ordinate of Seattle,Washington are {}, {}.'.format(latitude_x, longitude_y))
Valerii Boldakov
  • 1,751
  • 9
  • 19
mridulrb
  • 41
  • 3
0

you have to run this

from geopy.geocoders import Nominatim
geopy.geocoders.options.default_user_agent = "yourmeial@emailprovider.com"
geo = geocode(data['addr'], provider='nominatim')
0

Here is what you can try.I am sharing my example from geopy.exc import GeocoderTimedOut from geopy.geocoders import Nominatim geolocator=Nominatim(user_agent="Your_app-name")

Testing this code for one city ` City_name=geolocator.geocode("Whitefield,India")

Lat =City_name.latitude lon=City_name.longitude

print( City_name,lon,Lat) Results: Whitefield, Gandhipura, Bangalore East, Bangalore Urban, Karnataka, 560066, India 77.7497448 12.9696365 ` In the above example i am passing Country with city as the are places in US with the same name.In order to get more accurate result try passing country

Remember you need to use user_agent while using Nominatim Next step Use the apply function add new columns in your dataset for longitude and latitude locations['cor']=locations["New_loc"].apply(geolocator.geocode) locations["longitude"]=locations["cor"].apply(lambda x: x.longitude if x!= None else None) locations["Latitude"]=locations["cor"].apply(lambda x: x.latitude if x!= None else None)

Rishabh
  • 178
  • 9
0

If I were you, I would solve the issue this way:
I would have a text file with the addresses say addresses.txt saved locally, then I would geocode as follows:

# Import necessary modules
import pandas as pd
from geopandas.tools import geocode

# Specify the filepath: reading directly on path, or from web

fp = r"C:/Users/YourUser/full-file-path-on-your-machine/addresses.txt"

Assuming it is lodged on C/.
You can replace the "C/.../addresses.txt" with a url link

# Read the data
data = pd.read_csv(fp, sep = ";")
print(data)

# Geocode addresses with arcgis as the 'your user_agent application'

geocoded = geocode(data["addr"], provider = "arcgis")
geocoded.head(5)
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Safari-TF
  • 11
  • 2