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

Martin Davis mtnclimb at gmail.com
Wed Aug 5 09:38:06 PDT 2020


Added to the wiki:
https://trac.osgeo.org/postgis/wiki/UsersWikiLinearRefFunctions

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).

On Wed, Aug 5, 2020 at 8:46 AM Bruce Rindahl <bruce.rindahl at gmail.com>
wrote:

> My two cents:
> Since you are computing the distance, why not return it?  Also make the
> index zero based like most of the other st_ functions.
>
> CREATE OR REPLACE FUNCTION ST_LineLocateN( line geometry, pt geometry )
> RETURNS table(index integer, dist double precision)
> AS $$
>     SELECT i - 1, dist FROM (
>     SELECT i, ST_Distance(
>         ST_MakeLine( ST_PointN( line, s.i ), ST_PointN( line, s.i+1 ) ),
>         pt) AS dist
>       FROM generate_series(1, ST_NumPoints( line )-1) AS s(i)
>       ORDER BY dist
>     ) AS t LIMIT 1;
> $$
> LANGUAGE sql STABLE STRICT;
> _______________________________________________
> postgis-users mailing list
> postgis-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/postgis-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20200805/9a7f8cfe/attachment.html>


More information about the postgis-users mailing list