Tôi có hai đa giác này:
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)

Tôi muốn cắt bỏ những phần spdf1được giao nhau spdf2. Tuy nhiên, tôi muốn spdf1duy trì như một SpatialPolygonsDataFrame và giữ lại bất kỳ thông tin nào có trong đó spdf1@data.
Tôi đã thử gDifference như sau, cắt bỏ các phần spdf1được giao nhau spdf2, nhưng sau đó chuyển đổi spdf1thành SpatialPolygons (nghĩa là loại bỏ thông tin có trong spdf1@data).
gDifference(spdf1, spdf2, byid=T)
Làm thế nào tôi có thể cắt thành spdf1với spdf2nhưng giữ lại dữ liệu chứa trong spdf1@data? Tôi đã kiểm tra và thử câu hỏi tương tự này mà không làm thế nào để phủ một đa giác trên SpatialPointsDataFrame và bảo quản dữ liệu SPDF?
