Reverse Geocoding global points for addresses in English using Nominatim and geodataframe

Viewed 375

I am brand new to programming, stackoverflow, python and to the geospace! I am playing around with a small csv dataset of 13 rows and 2 columns - Latitude and Longitude. I am trying to reverse geocode and get the addresses in English.

My dataset -

 Longitude Latitude
54.34      24.47
55.6       24.78
16.36      48.2
151.21    -33.87
4.36       50.85
50.57      26.21
-43.24    -22.92
-49.05    -22.34
-114.07    50.91
8.54       47.41
-70.52    -33.39
116.17     39.92
116.51     39.79

I tried 2 ways to reverse geocode. I get an error each time.

1) First I created a 'coordinates' column and used that column

data['coordinates'] = '{},{}'.format(data['Latitude'], data['Longitude']) 
location = reverse_geocode ((data['coordinates'].progress_map(reverse_geocode)), addressdetails = True)

I get - ValueError: Must be a coordinate pair or Point

2) Because of the above error, I created a geodataframe and used the geometry column. The geometry column is created (data_gdf.head()).

data_gdf = gpd.GeoDataFrame(
data, geometry = gpd.points_from_xy(data['Longitude'], data['Latitude']))
location = reverse_geocode ((data_gdf['geometry'].progress_map(reverse_geocode)), addressdetails = True) 

I however get an error - getTypeError: Failed to create Point instance

Putting it all together -

import shapely
import geopandas as gpd
import os
import geopy
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

# for the progress bar
from tqdm import tqdm, tqdm_notebook   
# instantiate
tqdm.pandas(tqdm_notebook)

# get the csv data and convert to geodataframe
csv = os.path.join (data_folder,filename)
data = pd.read_csv(csv)

data['coordinates'] = '{},{}'.format(data['Latitude'], data['Longitude']) 

data_gdf = gpd.GeoDataFrame(data, geometry = gpd.points_from_xy(data['Longitude'],data['Latitude']))
data_gdf.crs = 'EPSG:4326'

# reverse geocode
geo_service = Nominatim (user_agent = "Reverse Geocoding", timeout=10)
reverse_geocode = RateLimiter (geo_service.reverse, min_delay_seconds=0.001)

#location = reverse_geocode ((data['coordinates'].progress_map(reverse_geocode)), addressdetails = True)
location = reverse_geocode ((data_gdf['geometry'].progress_map(reverse_geocode)), addressdetails = True)
location.raw

My goal during the lockdown was to learn/understand the geospatial scene. I am learning from free online resources. I don't have a teacher/mentor to help me with my errors. Any pointers will be greatly appreciated. Thanks.

0 Answers
Related