2

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")

enter image description here

Community
  • 1
  • 1
cmbarbu
  • 4,354
  • 25
  • 45

1 Answers1

3

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?

enter image description here

Edzer Pebesma
  • 3,814
  • 16
  • 26
  • Why would you need to know where these data come from? – cmbarbu Apr 14 '15 at 16:56
  • I assume you mean a bug in `Polygon`, or its documentation. But I see no `Polygon` call in your example. First, your example starts with R data for which we don't know where it comes from, then you directly assign values to slots, which is not recommended (meaning: at your own risk). – Edzer Pebesma Apr 15 '15 at 09:01
  • I would be glad not to manipulate directly slots but what is the recommended procedure to convert a polygon into a hole? – cmbarbu Apr 15 '15 at 09:15
  • Also the two polygons are just subsets of a SpatialPolygonsDataFrame, imported with "rgdal" from a kml generated in Google Earth. – cmbarbu Apr 15 '15 at 09:22
  • recommended is using the sp constructor functions `Polygon`, `Polygons`, `SpatialPolygons` etc, as the documentation sais. Please publish your kml and the procedure you used to read it in R. Is your hole a hole on google earth? – Edzer Pebesma Apr 15 '15 at 11:25
  • Maybe we can continue the discussion in the R public chat room: http://chat.stackoverflow.com/rooms/25312/r-public – cmbarbu Apr 15 '15 at 11:39