How to modify a polygon to be a hole (SpatialPolygons) changing its slots

老子叫甜甜 提交于 2019-12-20 03:44:03

问题


Edit:

As suggested by Edzer Pebesma in the comments, the recommended method to add a hole to a polygon is not to modify the slot but to rebuild the polygon, as illustrated in this related question.

Original question

Following the help of SpatialPolygons-class I tried to modify a polygon to be a hole of an other polygon but while the "hole polygon" is displayed as a border of the other polygon, its inside is colored as the rest.

What am I doing wrong?

Using defPunched and defHole defined here:

library("sp")
load(url("http://spatcontrol.net/CorentinMBarbu/misc/holeIssue.rda"))
defHole@polygons[[1]]@Polygons[[1]]@hole<-TRUE
OnePolDFToPolygon <- function(x){
     main <- x@polygons[[1]]@Polygons[[1]]
     return(main)
 }
punch <- Polygons(list(OnePolDFToPolygon(defPunched),OnePolDFToPolygon(defHole)),defPunched@polygons[[1]]@ID)
mine <- SpatialPolygons(list(punch),proj4string=defPunched@proj4string)
mine <- SpatialPolygonsDataFrame(mine,data=as(defPunched,"data.frame"))
plot(mine,col="blue",border="green")


回答1:


Holes are supposed to have the opposite ring direction, e.g. by

mine@polygons[[1]]@Polygons[[2]]@coords = mine@polygons[[1]]@Polygons[[2]]@coords[5:1,]
plot(mine, col = 'blue')

you get the plot below. Where did this data come from?



来源:https://stackoverflow.com/questions/29629049/how-to-modify-a-polygon-to-be-a-hole-spatialpolygons-changing-its-slots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!