rasterize ESRI shapefile with holes but FALSE hole slots

半腔热情 提交于 2019-12-20 05:52:40

问题


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:


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


来源:https://stackoverflow.com/questions/12250471/rasterize-esri-shapefile-with-holes-but-false-hole-slots

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