Calculating Distance Between Points in GeoDataFrame

  • Share this:

Code introduction


This function calculates the distance between each point in a GeoDataFrame and a specified point. It uses the geometric distance calculation method from the GeoPandas library.


Technology Stack : GeoPandas, NumPy

Code Type : Geographic data processing

Code Difficulty : Intermediate


                
                    
import geopandas as gpd
import numpy as np

def calculate_distance_between_points(shp1, shp2):
    """
    This function calculates the distance between two points in a GeoDataFrame.
    It uses the `geodesic` method from the GeoPandas library.
    """
    def distance(row):
        return row.geometry.distance(shp2.geometry.iloc[0])

    return shp1.assign(distance=distance)