[postgis-users] Extend linestring to snap to polygon

Nicolas Ribot nicolas.ribot at gmail.com
Mon Oct 15 00:58:12 PDT 2018


Hi,
you can extract the exterior of the polygon (st_boundary),  use linear
referencing function st_lineLocatePoint to find the projection of line's
start and end points then add the points to the line:

with pg as (
  select 'POLYGON((0 0, 5 0, 3 5, 0 0))'::geometry as geom
), line as (
     select 'LINESTRING(1 1, 2 1, 3.5 2)'::geometry as geom
) select st_addPoint(st_addPoint(
           line.geom,
           st_lineinterpolatepoint(
             st_boundary(pg.geom),
             st_lineLocatePoint(st_boundary(pg.geom),
st_startpoint(line.geom))),
           0),
          st_lineinterpolatepoint(
             st_boundary(pg.geom),
             st_lineLocatePoint(st_boundary(pg.geom), st_endpoint(line.geom))),
          st_numpoints(line.geom) + 1)
from pg, line;

(a smarter way must exist, but linear referencing function are quite
efficient to find/build points)

Nicolas

[image: Screen Shot 2018-10-15 at 09.56.22.png]

On Mon, 15 Oct 2018 at 08:25, Shane Carey <careyshan at gmail.com> wrote:

> Hi there,
>
> Is there a way to extend a linestring on both sides until it reaches a
> polygon.
>
> Thanks.
> --
> Le gach dea ghui,
> *Shane Carey*
> *GIS and Data Solutions Consultant*
> _______________________________________________
> 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/20181015/a7ffeaf3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2018-10-15 at 09.56.22.png
Type: image/png
Size: 17220 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20181015/a7ffeaf3/attachment.png>


More information about the postgis-users mailing list