我有以下两个多边形:
library(sp); library(rgeos); library(maptools)
coords1 <- matrix(c(-1.841960, -1.823464, -1.838623, -1.841960, 55.663696,
55.659178, 55.650841, 55.663696), ncol=2)
coords2 <- matrix(c(-1.822606, -1.816790, -1.832712, -1.822606, 55.657887,
55.646806, 55.650679, 55.657887), ncol=2)
p1 <- Polygon(coords1)
p2 <- Polygon(coords2)
p1 <- Polygons(list(p1), ID = "p1")
p2 <- Polygons(list(p2), ID = "p2")
myPolys <- SpatialPolygons(list(p1, p2))
spdf1 = SpatialPolygonsDataFrame(myPolys, data.frame(variable1 = c(232,
242), variable2 = c(235, 464), row.names = c("p1", "p2")))
proj4string(spdf1) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
+towgs84=0,0,0")
plot(spdf1, col="red")
coords1a <- matrix(c(-1.830219, -1.833753, -1.821154, -1.830219, 55.647353,
55.656629, 55.652122, 55.647353), ncol=2)
p1a <- Polygon(coords1a)
p1a <- Polygons(list(p1a), ID = "p1a")
myPolys1 <- SpatialPolygons(list(p1a))
spdf2 = SpatialPolygonsDataFrame(myPolys1, data.frame(variable1 = c(2),
variable2 = c(3), row.names = c("p1a")))
proj4string(spdf2) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
+towgs84=0,0,0")
plot(spdf2, col="yellow", add=T)
我想剪掉spdf1
与相交的部分spdf2
。但是,我想spdf1
保留为SpatialPolygonsDataFrame并保留其中包含的任何信息spdf1@data
。
I¹ve试图gDifference如下,其中切出的部分spdf1
是由相交spdf2
,但随后转换spdf1
到SpatialPolygons(包含在丢弃即信息spdf1@data
)。
gDifference(spdf1, spdf2, byid=T)
我如何才能切成spdf1
用spdf2
,但保留包含在数据spdf1@data
?我已经检查并尝试了类似的问题,但没有如何在SpatialPointsDataFrame上覆盖多边形并保留SPDF数据?