Calculate Distance Between Points

  • Share this:

Code introduction


This function calculates the distance between two spatial points. It first creates two GeoDataFrames, each containing a point. Then it calculates the distance between the two points using the `distance` method of GeoDataFrame.


Technology Stack : GeoPandas, Shapely

Code Type : Geospatial Data Analysis

Code Difficulty : Intermediate


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

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

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