Calculating Distance Between Points in GeoPandas

  • Share this:

Code introduction


Calculate the distance between two points in GeoPandas.


Technology Stack : GeoPandas, Shapely, NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
def calculate_distance_between_points(point1, point2):
    from geopandas import GeoDataFrame
    from shapely.geometry import Point
    import numpy as np

    # Create GeoDataFrames with the points
    gdf1 = GeoDataFrame([point1], geometry=[point1])
    gdf2 = GeoDataFrame([point2], geometry=[point2])

    # Calculate the distance between the two points
    distance = gdf1.geometry.distance(gdf2.geometry).iloc[0]

    return distance