1

enter image description hereI'm trying to calculate the distance between centriod of the polygon and each and every point of the polygon using GeoPandas Python

I need to find the distance between centroid(18.9568383649 72.831281) and geometry POLYGON ((72.83164071429999 18.9568102857, 72.8314213636 18.9569958182, 72.831512 18.9569766667,72.83164071429999 18.9568102857))

How I can find the distance? I'm not tied to using GeoPandas.

closed as off-topic by ahmadhanb, Vince, TomazicM, LaughU, Fran Raga Oct 11 at 9:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – ahmadhanb, Vince, TomazicM, LaughU, Fran Raga
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Do you know the formula for computing the distance between two points? If not, look up Euclidean distance. Then you can just loop through all your polygon vertices and apply that formula. I would also reproject your polygon to a CRS in meters, as computing distances with lon/lats is almost always wrong. – Jon Oct 10 at 14:24
  • yes I know the formula but the here I have used haversine as they are geospatial data , I'm looking for something which will give me the distance between centroid and each and every point of the polygon in python – user151670 Oct 10 at 14:27
  • 1
    If you know the formula, why can't you apply it? Can you add more details to your question to let us know where you're having issues? – Jon Oct 10 at 14:28
  • sure, if my dataframe has cenlat=18.8989 ,centlong=72.8989, lat=19.78979, long=73.87897 i know I'm going to apply this formula df['distance']= df[['cenlat', 'lat', 'cenlong', 'lon']].apply(lambda x: \ math.acos(np.sin(x[0]* np.pi/180)*np.sin(x[1]*np.pi/180)+ \ np.cos(x[0]*np.pi/180)*np.cos(x[1]*np.pi/180)*np.cos((x[2]-x[3])*np.pi/180))* 6371000 , axis=1) but if my dataframe contains values like a polygon list how should I proceed? please refer the image for your reference – user151670 Oct 10 at 14:34
  • 2
    Geopandas stores your polygons as shapely objects. You can access them with gdf.geometry.values[0] to get the first one. Then, you can use the .coords() and .xy() methods to access the values of each point of the polygon. See the shapely user manual: shapely.readthedocs.io/en/stable/manual.html – Jon Oct 10 at 14:50