[postgis-users] Extend linestring to snap to polygon

346 views
Skip to first unread message

Shane Carey

unread,
Oct 15, 2018, 2:25:37 AM10/15/18
to postgi...@lists.osgeo.org
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

Nicolas Ribot

unread,
Oct 15, 2018, 3:58:31 AM10/15/18
to PostGIS Users Discussion
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

Screen Shot 2018-10-15 at 09.56.22.png

_______________________________________________
postgis-users mailing list
postgi...@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users

Nicolas Ribot

unread,
Oct 15, 2018, 4:03:33 AM10/15/18
to PostGIS Users Discussion
(Line extension here is based on the shortest distance between line and polygon. To extend in the direction on line segments, you can refer to this list, message "Finding closet intersect point along direction of line", april 10 2013)

Nicolas
Reply all
Reply to author
Forward
0 new messages