I have a dataframe with lat, lon columns in WGS84.
I also have a multipolygon layer (GeoJSON, I can also convert it to a SHP) with all countries' boundaries, where the name of the country is in the attribute table.
I would like to add to the dataframe a column where for each lat, lon I'll have the country name.
What would be the efficient way to do that (assuming I have 2000 lat lon pairs)?
With help from @BERA I've created this function:
import geopandas as gpd
from shapely.geometry import Point
df_countries = gpd.read_file(r"C:\countries.geojson")
def get_countries(df, lat_col, lon_col,df_countries):
df_latlon = df[[lat_col,lon_col]].copy()
df_latlon['Coordinates'] = list(zip(df_latlon[lon_col], df_latlon[lat_col]))
df_latlon['Coordinates'] = df_latlon['Coordinates'].apply(Point)
df_latlon = gpd.GeoDataFrame(df_latlon, geometry='Coordinates')
df_latlon = df_latlon.set_crs(epsg=4326)
df_latlon = gpd.sjoin(df_latlon, df_countries[['CNTRY_NAME','geometry']], how='left')
return df_latlon
1 Answer 1
import geopandas as gpd
dfpoints = gpd.read_file(r"C:\folder\bs_riks.shp")
dfpolys = gpd.read_file(r"C:\folder\ak_riks.shp")
df = gpd.sjoin(dfpoints, dfpolys, how='left')
#or: df = gpd.sjoin(dfpoints, dfpolys[['CNTRY_NAME','geometry']], how='left') #If you dont want all attributes from the polygons
If you have a pandas df of the coords, create geopandas like this: Creating a GeoDataFrame from a DataFrame with coordinates
-
My lat lon are not in a SHP file, they are in two separate columns in a pandas dataframeuser88484– user884842020年09月02日 06:19:12 +00:00Commented Sep 2, 2020 at 6:19
-
Convert the pandas df to a geopandas using the lat longsBera– Bera2020年09月02日 06:26:10 +00:00Commented Sep 2, 2020 at 6:26
-
can you please add it to the code in your answer so I'll mark it as correct?user88484– user884842020年09月02日 06:27:48 +00:00Commented Sep 2, 2020 at 6:27
Explore related questions
See similar questions with these tags.