altynbei
altynbei

Reputation: 97

Reverse geocoding of thousands of coordinates to get country only

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!

Upvotes: 5

Views: 3212

Answers (1)

zipa
zipa

Reputation: 27869

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')

Upvotes: 6

Related Questions