Reverse geocoding of thousands of coordinates to get country only
Asked Answered
S

1

5

I have >34.000 geographic coordinates in my data in .csv format, for each of those I need to return the country.

I am using Python, the Geocoder library. The Google API has daily query limit of 2500, so it would take me two weeks to do this.

My exact code seems irrelevant to this question.

I am wondering, can I sidestep the Geocoder library or Google API altogether, given that I only need a country, not street address or anything fine? Somehow coordinates of countries seem to be common knowledge.

I am able to get the same data in .kml format, in a relatively short time.

I haven't found an answer to this question. Any input is appreciated. Edits, pointing me to existing answers somewhere else, etc. Thanks!

Stickpin answered 28/6, 2019 at 14:5 Comment(0)
K
6

You can utilize the geopandas library and use their world dataset:

import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

Now, you can load your coordinates into pandas dataframe and convert them into POINT geometries:

df = pd.read_csv('my_points.csv')
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))

The code above assumes your point columns are named Longitude and Latitude.

Now you can join your points and get the countries they are residing in:

result = gpd.sjoin(gdf, world, how='left')
Kamal answered 28/6, 2019 at 14:21 Comment(1)
Thank you, it worked! With one small caveat, that gpd.crs='init' and world.crs='epsg:4326'. This caused a warning along the lines of "CRS (coordinate reference systems) do not match!", apparently sjoin doesn't work on two data frames with non-matching CRS. I added gdf.crs = {'init' :'epsg:4326'} before executing sjoin, and I got the result.Stickpin

© 2022 - 2024 — McMap. All rights reserved.