Get Distance Between Two Points in GeoPandas
Asked Answered
M

1

19

I have two points as below. I need to get the distance between them in meters.

POINT (80.99456 7.86795)
POINT (80.97454 7.872174)

How can this be done via GeoPandas?

Middleweight answered 3/9, 2020 at 10:52 Comment(0)
D
27

Your points are in a lon, lat coordinate system (EPSG:4326 or WGS 84). To calculate a distance in meters, you would need to either use the Great-circle distance or project them in a local coordinate system to approximate the distance with a good precision.

For Sri Lanka, you can use EPSG:5234 and in GeoPandas, you can use the distance function between two GeoDataFrames.

from shapely.geometry import Point
import geopandas as gpd
pnt1 = Point(80.99456, 7.86795)
pnt2 = Point(80.97454, 7.872174)
points_df = gpd.GeoDataFrame({'geometry': [pnt1, pnt2]}, crs='EPSG:4326')
points_df = points_df.to_crs('EPSG:5234')
points_df2 = points_df.shift() #We shift the dataframe by 1 to align pnt1 with pnt2
points_df.distance(points_df2)

The result should be 2261.92843 m

Depart answered 3/9, 2020 at 13:57 Comment(3)
by using the Kandawala / Sri Lanka Grid - EPSG:5234 CRS it is obvious the knowledge and extent you are going to answer the Question. thank you.Absentee
When I try this I am getting a tuple returned 0 NaN 1 41148.927095 what might I be doing wrong?Quantify
To get this to work I had to add .iloc[1] to the end of points_df.distance(points_df2) to return the distance value between the 2 points.Quantify

© 2022 - 2024 — McMap. All rights reserved.