0
votes

I have a shapefile (showing different sediment classes in the northsea) read in with readOGR(). It has a lot of " what should be" holes in many polygons, but using rasterize() does eliminate all the holes since they are not marked as TRUE in their hole-slots. Used rasterize(...,fun='first') with no success. Nevertheless, QGIS shows the holes all nicely. Also, over() correctly evaluates the field values, e.g., in a hole, probably taking advantage of the slot "plot order" which is why I came up with something like:

for (i in 1:ncell(raster)){
    coo<-xyFromCell(raster,i,spatial=T)
    col<-colFromX(ra,coo@coords[1,1])
    row<-rowFromY(ra,coo@coords[1,2])
    proj4string(coo)<-proj4string(shape)
    n<-over(coo,shape)
    raster[col,row]<-n$Prime_FOLK
}

to bypass rasterize, but it will take 50days to be done.

So here' my question:

Has anyone experienced something similar and found a workaround for it?

(I would have liked include example data, but dput()fails on SpatialPolygons?!?)

1

1 Answers

1
votes

Yes. I have the same problem with not getting the holes to rasterize properly, because they are not specified correctly. For the shape files I've been using, the first polygon is always the main polygon and the second through the last are the holes. If this isn't the case for you this may not work for your situation. Here is the code I wrote to change all the non-first polygons into holes=T:

## poly.dat is the SpatialPolygonsDataFrame

fix.holes<-function(poly.dat){
  n.poly.all<-numeric()
  for (k in 1:nrow(poly.dat@data)){
    n.poly.all[k]<-length(poly.dat@polygons[[k]]@Polygons)
  }
  has.hole<-which(n.poly.all>1)
  n.poly<-n.poly.all[has.hole]

  for (k in 1:length(has.hole)){
    for (m in 2:n.poly[k]){
      poly.dat@polygons[[has.hole[k]]]@Polygons[[m]]@hole<-T
    }
  }
  return(poly.dat)
}