使用孔将ESRI shapefile栅格化,但是将孔槽设置为FALSE [英] rasterize ESRI shapefile with holes but FALSE hole slots

查看:84
本文介绍了使用孔将ESRI shapefile栅格化,但是将孔槽设置为FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用readOGR()读入的shapefile(显示北海不同的沉积物类别).它有一个 许多多边形中有很多应该是"的孔,但是使用rasterize()确实可以消除所有孔,因为它们的孔槽中没有将它们标记为TRUE.使用rasterize(...,fun='first')失败.但是,QGIS很好地显示了这些漏洞.另外,over()可以正确地评估例如孔中的字段值,这可能是利用了插槽绘图顺序"的优势,这就是为什么我想出了类似以下内容的原因:

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
}

绕过栅格化,但是需要50天才能完成.

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

所以这是我的问题:

有人经历过类似的事情并找到了解决方法吗?

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

(我希望包含示例数据,但dput()不能通过SpatialPolygons吗?!?)

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

推荐答案

是.由于没有正确指定孔,因此我也遇到了同样的问题,即无法正确地对孔进行栅格化.对于我一直在使用的形状文件,第一个多边形始终是主要多边形,第二个到最后一个是孔.如果不是您这种情况,则可能不适用于您的情况.这是我编写的将所有非第一个多边形更改为Hole = T的代码:

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

这篇关于使用孔将ESRI shapefile栅格化,但是将孔槽设置为FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆