[postgis-users] Find LineString segment index containing closest point?
mtnclimb at gmail.com
Wed Aug 5 10:09:14 PDT 2020
On Wed, Aug 5, 2020 at 9:38 AM Martin Davis <mtnclimb at gmail.com> wrote:
> Added to the wiki:
> It seems a bit awkward to use this as it stands for inserting a vertex in
> a line at the closest point. I wonder if the function should also return
> the closest point itself? (In fact, I am certain that it should if it is
> implemented as a native function, since that additional useful information
> costs very little to obtain).
And done... updated the wiki with a new version of the function which
returns the index, distance and computed point on line. Here it is in
action, used for adding a vertex to a line:
WITH data(id, line) AS (VALUES
( 1, 'LINESTRING (0 0, 10 10, 20 20, 30 30)'::geometry )
loc AS (
SELECT id, line, index, geom AS pt
JOIN LATERAL ST_LineLocateSegment( data.line, 'POINT(15
15.1)'::geometry ) AS lls ON true
SELECT id, ST_AsText( ST_AddPoint( line, pt, index ) )
Note this is probably better done using ST_Snap, but sometimes it's useful
to do things in a more explicit way.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the postgis-users