[R-sig-Geo] Using gIntersection to split polygons with lines
Frederico Mestre
mestre.frederico at gmail.com
Mon Jun 26 12:56:12 CEST 2017
Hello,
I'm trying to use gIntersection() to cut a polygon ("SpatialPolygons") with
a line ("SpatialLinesDataFrame").
I wanted to obtain a "SpatialPolygons" object with the polygons defined by
the lines. This is what I did:
polygons1 <- gIntersection(spgeom1=mypolygon, spgeom2=mylines)
However I get an "SpatialLines" object as an output instead of a
"SpatialPolygons".
Am I doing something wrong? Is there any alternative?
I provide this sample code:
#Creating the polygons
coords <- rbind(c(1,2),c(2,2),c(2,1),c(1,1))
pol1 <- Polygon(coords)
pol2 <- Polygons(srl=list(pol1), ID=1)
pol3 <- SpatialPolygons(Srl=list(pol2))
#Creating the line
l1 <- Line(rbind(c(0.5,1.5),c(2.5,1.5)))
l2 <- Lines(list(l1),ID=1)
l3 <- SpatialLines(list(l2))
line1 <- SpatialLinesDataFrame(l3,data=as.data.frame(matrix(ncol=2,nrow=1)))
#Ploting both
plot(pol3)
plot(line1, add=TRUE)
#Intersection
inters1 <- gIntersection(spgeom1=pol3, spgeom2=line1)
inters1 <- gIntersection(spgeom1=line1, spgeom2=pol3)
plot(inters1)
class(inters1)
It only shows the lines (the object is of class "SpatialLines").
Thanks,
Frederico
ᐧ
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list