DreamingMan
DreamingMan

Reputation: 73

reverse geopy geocoding pandas

I have the following data frame in a Jupyter Notebook that has a list of GPS coordinates with from geopy.geocoders import Nominatim and import pandas as pd.

    stop_id     Lat         Long
0   2        53.352280  -6.263668
1   3        53.352345  -6.263758
2   4        53.352604  -6.264143
3   6        53.352783  -6.264417
4   7        53.352867  -6.264543
5   8        53.353287  -6.265152

I have been trying to add a new column populated with the corresponding addresses to the GPS coordinates.

To do this I tried

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

but got the following error message:

ValueError: Must be a coordinate pair or Point.

I then created another column [LatLong]

df['LatLong'] = df[df.columns[1:]].apply(
    lambda x: ', '.join(x.dropna().astype(float).astype(str)),axis=1)

    stop_id     Lat         Long         LatLong
0   2       53.352280   -6.263668    53.35228, -6.263668
1   3       53.352345   -6.263758    53.352345, -6.263758
2   4       53.352604   -6.264143    53.352604, -6.264143
3   6       53.352783   -6.264417    53.352783, -6.264417
4   7       53.352867   -6.264543    53.352867, -6.264543
5   8       53.353287   -6.265152    53.353287, -6.265152

I then ran the the following code:

df['address'] = geolocator.reverse(df['LatLong'])

however, I just get the exact same error message.

The code I have used above is adapted from other answers on this site to similar questions and GeoPy's documentation, so I am presuming my code is not exact enough to extract the GPS coordinates in the correct way for geopy.

Can anyone point out my error to me?

Upvotes: 3

Views: 3846

Answers (2)

julia
julia

Reputation: 1

"A large number of DataFrame rows might produce a significant amount of geocoding requests to a Geocoding service, which might be throttled by the service (e.g. by returning Too Many Requests 429 HTTP error or timing out).

geopy.extra.rate_limiter.RateLimiter class provides a convenient wrapper, which can be used to automatically add delays between geocoding calls to reduce the load on the Geocoding service. Also it can retry failed requests and swallow errors for individual rows."

I found this on Geopy Documentation. Maybe you should change tre RateLimiter , see if it helps

Upvotes: 0

Stephen Rauch
Stephen Rauch

Reputation: 49842

Problem

Your error message says:

ValueError: Must be a coordinate pair or Point

In both:

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

and

df['address'] = geolocator.reverse(df['LatLong'])

you are sending a pandas structure into a method that does not understand them.

Solution

I have no way to test this, but a solution can look something like:

df['address'] = df.apply(
    lambda row: geolocator.reverse((row['Lat'], row['Long'])), axis=1)

Upvotes: 1

Related Questions