graph-databasesmemgraphdbopencyphermemgraph

Can I calculate the distance between nodes that have longitude and latitude?


I will expand the dataset of capital cities that I have with longitude and latitude for each city. After that I would like to calculate the distance in kilometers between cities, or to calculate the total distance. Something similar that you can do in Via Michelin or Google Maps.

Do I need to add relations with distance between each city (like I found in https://playground.memgraph.com/sandbox/europe-roads) or can I use coordinates to calculate how far is one city from another?

I'm looking for equivalent of

MATCH p = (:City {name: "Zagreb"})
 -[:Road * wShortest (e, v | e.length) total_weight]->
 (:City {name: "Paris"})
RETURN nodes(p) AS cities, total_weight;

But when I don't have weights, but I have coordinates.


Solution

  • Memgraph has a distance_calculator algorithm that calculates the "distance between two geographic locations". The algorithm has procedures for calculating either single or multiple distances.

    However, Memgraph does not seem to provide a way to directly use that algorithm in a shortest path calculation.

    But the workaround is to use the distance_calculator algorithm to calculate and store the geometric distance between the endpoints of every Road relationship (as a relationship property), and then use Memgraph's weighted-shortest-path algorithm to calculate the shortest path between any 2 cities.