1

I have a dataframe with lat, lon columns in WGS84.

enter image description here

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
0

1 Answer 1

2

Spatial Join:

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

3
  • My lat lon are not in a SHP file, they are in two separate columns in a pandas dataframe
    – user88484
    Commented Sep 2, 2020 at 6:19
  • Convert the pandas df to a geopandas using the lat longs
    – Bera
    Commented 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
    Commented Sep 2, 2020 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.