2

I need to calculate the distance between some points on a line (LineString), using PostGIS, to find the nearest over a set of MultiPoint.

I know there is a function ST_Distance(), but this one returns a distance as the crow flies, with no possibility to take a path into account.

The problem is : "which of A or B is the nearest of S ?"

Here is a little drawing, for a better explanation :

Drawing

Is there an alternative to ST_Distance() ?


I have already read Getting distance between two points over linestring using PostGIS?, but unfortunately, it doesn't give a complete reply : I have a MultiPoint feature.

The solution given wasn't succesfull, but I guess it may just need an adaptation ..?

1

Here's a quick adaptation of the answer for this question. The additional part that you need is ST_Dump

SELECT
    ST_Length(ST_LineSubstring(
        line,
        ST_LineLocatePoint(line, s),
        ST_LineLocatePoint(line, (ST_Dump(p)).geom)))
    ,(ST_Dump(p)).path[1] ind
FROM (
  SELECT
    'LINESTRING(0 0, 10 0, 10 5, 0 5)'::geometry line,
    'POINT(1 0)'::geometry s,
    'MULTIPOINT(1 5, 10 3, 8 5)'::geometry p
) data
ORDER BY 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.