SpatialPolygonsDataFrame にポリゴンのリストがあり、そのうちの 1 つを別の穴として設定する必要があります。
新しく作成されたポリゴンに穴を定義する方法のヘルプで見つけましたがset_Polypath、既存のポリゴンに「穴」フラグを設定する方法は?
ポリゴンを再構築してから、spdf で置き換える必要があるようです。
次の関数は、穴を追加してポリゴンを自動的に再構築します。
library("sp")
AddHoleToPolygon <-function(poly,hole){
# invert the coordinates for Polygons to flag it as a hole
coordsHole <- hole@polygons[[1]]@Polygons[[1]]@coords
newHole <- Polygon(coordsHole,hole=TRUE)
# punch the hole in the main poly
listPol <- poly@polygons[[1]]@Polygons
listPol[[length(listPol)+1]] <- newHole
punch <- Polygons(listPol,poly@polygons[[1]]@ID)
# make the polygon a SpatialPolygonsDataFrame as the entry
new <- SpatialPolygons(list(punch),proj4string=poly@proj4string)
new <- SpatialPolygonsDataFrame(new,data=as(poly,"data.frame"))
return(new)
}
その後、SpatialPolygonsDataFrame 内の 2 つの多角形から全体を持つ多角形を定義できます。
load(url("http://spatcontrol.net/CorentinMBarbu/misc/spdf.rda"))
punchedPoly <-AddHoleToPolygon(spdf[1,],spdf[2,])
そして得る:

そして、spdfのポリゴンを置き換えます
spdf <- rbind(punchedPoly,spdf[2,])
plot(spdf,col=c(1,2),main="New SpatialPolygonsDataFrames")
