[R-sig-Geo] Breaking a polygon at a line

Orme, David d.orme at imperial.ac.uk
Sat Jan 27 14:47:12 CET 2018


Hi,

I’m trying to cut a polygon open into a linestring using a line string as a blade (for want of a better description). The specific use case is that I want to convert a polygon boundary into a GPX route and want to control the starting point.

Using ‘sf’, an simple example of the inputs and target is:

poly <- st_sfc(st_polygon(list(matrix(c(0,2,2,0,0,0,0,2,2,0), ncol=2))))
line <- st_sfc(st_linestring(matrix(c(1,3,1,1), ncol=2)))

plot(poly, xlim=c(-1,4), ylim=c(-1,3), lwd=3, border='grey70')
plot(line, add=TRUE, col='red')

target <- st_sfc(st_linestring(matrix(c(2,2,0,0,2,2,1,0,0,2,2,1), ncol=2)))
text(st_coordinates(target))

Options:
- st_split() always returns the same feature type so it only splits if the line crosses the polygon boundary at least twice.
- st_cast() is converts the polygon to a linestring, but uses the first coordinate in the polygon as the starting point of the line string.

So, the clunky solution I can see is to use st_intersection to find a point on both the line and polygon, find distances from all points on the polygon boundary to that and then juggle some blocks of coordinates to get the order right.

Am I missing something? Bit of a fringe use-case, I suspect but I was curious if there is an existing solution.

Thanks,
David



	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list