On Mon, 26 Jun 2023 11:44:47 -0700 (PDT), Jim wrote:
> Sandro,
>
> Thank you for the insight.
>
> And to clarify my question about ST_LENGTH following the "contour" of
> the Linestring, I may have used the word "contour" incorrectly. What
> I meant to ask is, given a Linestring A-B-C-D, which represents a
> road, does ST_LENGTH compute the sum of the lengths A-B , B-C, and
> C-D
>
certainly yes, the LENGTH algorithm works exactly this way
> or does it return the straight line distance between A and D?
>
absolutely not: if you need to know the distance between
Start and End points of the linestring you must explicitly
call ST_Distance()
> Using my original assumptions, since my objective is to compute the
> length of the Linestring from A-X-P then I might use conceptually:
>
> 1. ST_ClosestPoint(Linestring(A-B), POINT(P) giving me Point X
> 2. Create a new Linestring A-X using what combination of ST_????
> giving me Linestring(A-X)
>
> 3. Compute ST_Length(Linestring(A-X) + ST_LENGTH(Line(X-P)
>
> Does that sound reasonable?
>
please see the attached figure:
1. first of all you must determine the X point position, that
is you must identify the point on the linestring closest
to the external point P
the appropriate Spatial SQL function is ST_ClosestPoint()
2. then you must insert X into the linestring by calling
ST_Snap()
3. now you'll be ready for cutting the linestring at point X;
in other words you'll extract A-B-C-X starting from
A-B-C-D using X as a blade. see ST_Split() and friends.
4. final steps: you simply have to add the lengths of A-B-C-X
and of X-P
all together now in a single SQL expression:
SELECT ST_Length(
ST_SplitLeft(
ST_Snap(x.l, ST_ClosestPoint(x.l, x.p), 0.001),
ST_ClosestPoint(x.l, x.p)))
+ ST_Length(
ST_ShortestLine(x.l, x.p))
FROM
(SELECT ST_GeomFromText('LINESTRING(0 0, 100 0)') AS l,
MakePoint(50, 10) AS p) AS x;
I'm happy to leave all the work of refining and fine-tuning
the above SQL expression to you ;-)
bye Sandro