Sevak Ghazaryan
Sevak Ghazaryan

Reputation: 23

Postgis calculate the distance between points more than two

I have a table that has a position in it. Is there any solid way to calculate the distance between A --> B --> C --> D etc using Postgis.

Distance = AB + AC + CD etc.

Each point has {lat, lng}.

I appreciate any help you can provide.

Upvotes: 0

Views: 424

Answers (1)

JGH
JGH

Reputation: 17836

You can use lead or lag to work with next/previous row of a window. You can then compute the distance and sum it if you want, in an outer query.

SELECT sum(dist) 
FROM (
  SELECT id, ST_DistanceSphere(geom, lead(geom) OVER(ORDER BY id)) as dist 
  FROM myTable) sub;

Upvotes: 2

Related Questions