Introduction

In this article I show how to calculate distances between two points with the python geopandas library. This library also has other very useful functions for working with geolocated data.

Code

Let’s first import the library and use a made-up data frame

First we are going to import the library and create 2 dataframes. One for points in Argentina and the other one from the obelisk.

import geopandas 
import pandas as pd

df = {'points':  [1, 2,  3, 4, 5],
        'lat': [-34.59099802766986, -34.92056535501151, -33.88679684787259, -34.649638802141546, -31.411566586772533],
        'lon': [-58.40792868551097, -57.95607174374463, -60.57848986051933, -59.42593235787073, -64.19129855857013]
        }


df2 = {'points':  ["obelisk"],
        'lat': [-34.603737359786216],
        'lon': [-58.381564211576254]
        }

df = pd.DataFrame (df, columns = ['points','lat',"lon"])
df2 = pd.DataFrame (df2, columns = ['points','lat',"lon"])

All these points are in Argentina. Now we will create geopandas dataframes:

See that we are using the CRS (coordinated reference system) of google maps, because these points were taken from there. That’s why we put “epsg: 4326”. Also see that in google maps latitude goes before longitude, in geopandas in general it is the other way around.

To obtain the result in meters we will have to change the CRS to one from Argentina. The page http://epsg.io/ is very useful for this. It has different coordinate systems, in which one can choose regarding the tradeoff between a precise system and a system that covers a large area.

gdf.to_crs(epsg=22196  ,inplace=True)
gdf2.to_crs(epsg=22196  ,inplace=True)

Once this is done we can calculate the distances

df["distance_to_obelisk"]=gdf['geometry'].distance(gdf2['geometry'][0])

Comparing with google maps. The second point yields a distance from the obelisk of 52.46km. The difference with the calculated distance is 14 meters:
Distance between the center of “la plata” and the obelisk

print(df["distance_to_obelisk"][1]-52.46*1000)
## 14.72043128291989