How to find nearest distance from a coordinate point inside a polygon to the edge (border) of the polygon in Python?
I currently have a dataset with lat and lon coordinates in my first dataframe.
And then I have a shape of a country, Nigeria.
world_filepath = gpd.datasets.get_path('naturalearth_lowres')
world = gpd.read_file(world_filepath)
nigeria = world.loc[world['name'] == 'Nigeria']
pop_est continent name iso_a3 gdp_md_est geometry
56 200963599.0 | Africa | Nigeria NGA | 448120 | POLYGON ((26958.294 253210.950, 35554.540 4322..
I am not sure how to find the NEAREST distance between the coord points in my first dataframe to the nigeria shape border. I have tried using shapely i think but I keep getting 0 since the coordinates are inside of the polygon.
At the end result, I want to the distance from the coord point to the nearest distance to the boarder of Nigeria for all the rows in the first dataframe.
Thank you :)
Edit -- To include more info and code I saved the first dataset with the point coordinates as 'coord_new'
df=gpd.GeoDataFrame(coord_new,geometry=gpd.points_from_xy(coord_new.longitude, coord_new.latitude))
#df= df.to_crs(26392)
#nigeria = nigeria.to_crs(26392)
df['distance1']=nigeria.exterior.distance(df)
df['distance1']=nigeria.boundaries.distance(df)
This is the way that I saw other people doing to find the distance from point to the border of the polygon. However, I keep getting 0 and then NaN in the distance1 column.
source https://stackoverflow.com/questions/75403236/how-to-find-nearest-distance-from-a-coordinate-point-inside-a-polygon-to-the-edg
Comments
Post a Comment