[R-sig-Geo] Split polygon by line

Karl Jarvis karljarvis at gmail.com
Fri Jul 24 23:04:14 CEST 2015


Jill Deines was kind enough to track down and adapt an answer for me 
from a similar question from here:
http://stackoverflow.com/questions/5726297/cut-polygons-using-contour-line-beneath-the-polygon-layers


pol = 
SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0),c(0,0,1,1,0)))), 
ID="polygon")))
line = SpatialLines(list(Lines(list(Line(cbind(c(0,1),c(0.5,0.5)))), 
ID="line")))

lpi <- gIntersection(pol, line)               # intersect your line with 
the polygon
blpi <- gBuffer(lpi, width = 0.000001)  # create a very thin polygon 
buffer of the intersected line
dpi <- gDifference(pol, blpi)                # split using gDifference

plot(dpi)

# color by polygon
plot(pol)
plot(SpatialPolygons(list(Polygons(list(dpi at polygons[[1]]@Polygons[[1]]), "1"))), 

       add=T, col = "lightblue")
plot(SpatialPolygons(list(Polygons(list(dpi at polygons[[1]]@Polygons[[2]]), "2"))), 

      add = TRUE, col = "lightgreen")



More information about the R-sig-Geo mailing list