[postgis-users] Find LineString segment index containing closest point?

Martin Davis mtnclimb at gmail.com
Tue Aug 4 08:46:39 PDT 2020


A frequent question on GISSE is to find the LineString segment index
containing the closest point to a given point, to allow inserting the
closest point into the line using AT_AddPoint [1][2][3].

AFAIK there's no easy way to do this in PostGIS currently.  It looks like
it requires iterating over the segments of the LineString?

The ST_ClosestPoint function (and the Linear Referencing functions) do have
the segment index information internally.  It would be nice if there was an
extended version of ST_ClosestPoint which returned a record with both the
closest point and the index of the segment containing it.  Or perhaps an
extended Linear Referencing function which returns the point, the segment
index, and the fractional distance along the line.

For comparison, both Turf and JTS provide similar functionality.

[1]
https://gis.stackexchange.com/questions/368479/finding-line-segment-of-point-on-linestring-using-postgis
[2]
https://gis.stackexchange.com/questions/40622/how-to-add-vertices-to-existing-linestrings
<https://gis.stackexchange.com/questions/40622/how-to-add-vertices-to-existing-linestrings?noredirect=1&lq=1>
[3]
https://gis.stackexchange.com/questions/370488/find-closest-index-in-line-string-to-insert-new-vertex-using-postgis
<https://gis.stackexchange.com/questions/370488/find-closest-index-in-line-string-to-insert-new-vertex-using-postgis/370525#370525>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20200804/b878c05f/attachment.html>


More information about the postgis-users mailing list