Skip to main content
added 5 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

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

UPDATE:

 

Thanks toWith 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

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

UPDATE:

Thanks to @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

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
I've added the answer
Source Link
user88484
  • 1.9k
  • 4
  • 26
  • 42

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

UPDATE:

Thanks to @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

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

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

UPDATE:

Thanks to @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
Source Link
user88484
  • 1.9k
  • 4
  • 26
  • 42

Spatial join list lat,lon and multipolygon Python

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