'Geopy distance between two point tuples of (lat, lon) coordinates

I've been trying to measure geographic distance between points in a GeoDataframe (gdb['geometry']) and one specific point, let's say, b_xy.

gdb['geometry'] is a geodatabase containing tuples of lon/lat coordinates as such:

geometry
POINT (-73.958 40.685)
POINT (-73.995 40.663)
POINT (-73.982 40.756)

Whereas b_xy is a simple lon/lat coordinate: (40.757280550000004, -73.98585503545917)

The code given to my professor from the textbook/tutorial he claims to be using for this example, is as such:

'd2b = lambda pt: cdist([(pt.x, pt.y)], [b_xy])[0][0]*10 #hasilnya degrees/radians gdb['d2tsquare'] = gdb['geometry'].to_crs(tgt_crs)
.apply(d2b)'

which gives out a weird output that is presumably in degrees/radians, despite using a projected crs for tgt_crs

I've been trying to use this tutorial on measuring distances between two points, in meters. However, geopy.distance is unable to calculate from a tuple and can only perform singular inputs; it cannot accept data from a geodataframe.

I'm at a loss here for a method that works. Been thinking about making a loop for it but not sure where to start.



Solution 1:[1]

This question is partially answered here. but to add some value here is what you want to do.

import geopy.distance

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print (geopy.distance.geodesic(coords_1, coords_2).km)

Output is : 279.35290160430094 which is the distance in km

Solution 2:[2]

You can use UTM CRS to transform epsg:4386 co-ordinates into meters so distance calculation will be meters.

import geopandas as gpd
import shapely.geometry
import pandas as pd
import io

gdb = gpd.GeoDataFrame(
    geometry=gpd.GeoSeries.from_wkt(
        pd.read_csv(
            io.StringIO(
                """geometry
POINT (-73.958 40.685)
POINT (-73.995 40.663)
POINT (-73.982 40.756)"""
            )
        )["geometry"],
        crs="epsg:4386",
    )
)

p = shapely.geometry.Point(
    -73.98585503545917,
    40.757280550000004,
)

gdb["distance"] = gdb.to_crs(gdb.estimate_utm_crs())["geometry"].distance(
    gpd.GeoSeries([p for _ in range(len(gdb))], crs=gdb.crs).to_crs(
        gdb.estimate_utm_crs()
    )
)

gdb
geometry distance
0 POINT (-73.958 40.685) 8361.98
1 POINT (-73.995 40.663) 10494.9
2 POINT (-73.982 40.756) 355.129

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AlixaProDev
Solution 2 Rob Raymond