MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Geospatial: LineString length in kilometer

Let’s say that some documents have GeoJSON / LineString fields that represent someone’s displacement. The coordinates that make up the LineString were brought from the google map api.

{ 
    type: "LineString", 
    coordinates: [[-29.9673156, -51.1497259], [-29.969095, -51.114017]] 
}

Question:

Is it possible to know the length of this LineString in kilometers?

PS. I’m using python / pymongo

Hi Matheus,

There is currently no inbuilt function to calculate the distance between two arbitrary geo locations in the same document, but there are well-known algorithms such as haversine that you could use.

The most straightforward approach would be to use an existing Python library (for example: haversine), but you should also be able to implement this using trigonometry operators in an aggregation pipeline.

Regards,
Stennie