【问题标题】:R simplify shapefileR简化形状文件
【发布时间】:2014-01-07 16:12:15
【问题描述】:

我有一个带有亚马逊大河的 shapefile。单独的 shapefile 有 37.9 MB,连同属性表一起达到 42.1 MB。我正在生成所有巴西亚马逊的 PNG 图像,每个 1260x940 像素,shapefile 中的所有这些数据只会减慢每张地图的绘制速度,所以我想简化它。

gSimplify 函数,在 rgeos 包中,似乎只是简化每个多边形,而不是去除较小的多边形。我尝试了 0.1 和 1000 的容差,并且总是得到长度(shp@polygons)相同的值:27633。最终的绘图需要几乎相同的时间来绘制。我需要一个函数,我告诉它最终的栅格将是 1260x940 像素,因此它可以删除每个不必要的点。有这样的功能吗?

提前致谢。

【问题讨论】:

  • 这不是 R 解决方案,但我推荐 mapshaper.org。很容易加载 shapefile 并尝试不同级别的简化,然后重新保存。我发现这对于加快 R 中的地图绘制非常有用。
  • 谢谢@Andy!它就像一个魅力。更改简化级别并实时查看结果非常好。但我仍然想使用 R 来做到这一点,因为问题仍然存在(有一天该网站可能会变得遥不可及)。
  • 不用担心@Rodrigo,你的愿望是可以理解的,我在 4 年前也有类似的感觉,并试图找到一个好的 R 解决方案,最终放弃了。在那段时间里,mapshaper 变得更好了。如果您确实找到了一个好的 R 解决方案,我很乐意听到。

标签: r shapefile simplify


【解决方案1】:

这里有相当全面的解决方案:http://www.r-bloggers.com/simplifying-polygon-shapefiles-in-r/

总而言之,您需要获取多边形的面积:

area <- lapply(rivers@polygons, function(x) sapply(x@Polygons, function(y) y@area))

你在 R 中的 shapefile 对象在哪里。

然后你找出大多边形并保留它们:

    sizeth <- 0.001 #size threshold of polygons to be deleted
    mainPolys <- lapply(area, function(x) which(x > sizeth))

    rivers@data <- rivers@data[-c(1:2),] 
    rivers@polygons <- rivers@polygons[-c(1:2)] 
    rivers@plotOrder <- 1:length(rivers@polygons)
    mainPolys <- mainPolys[-c(1:2)]

    for(i in 1:length(mainPolys)){   if(length(mainPolys[[i]]) >= 1 &&
    mainPolys[[i]][1] >= 1){
         rivers@polygons[[i]]@Polygons <- rivers@polygons[[i]]@Polygons[mainPolys[[i]]]
         rivers@polygons[[i]]@plotOrder <- 1:length(rivers@polygons[[i]]@Polygons)   } }

这可能还不够好,您可能不想删除任何多边形,在这种情况下,shapefiles 包中的 dp() 函数可以解决问题:

res <- 0.01 #the argument passed to dp() which determines extent of simplification. Increase or decrease as required to simplify more/less    
for(i in 1:length(rivers@polygons)){
      for(j in 1:length(rivers@polygons[[i]]@Polygons)){
        temp <- as.data.frame(rivers@polygons[[i]]@Polygons[[j]]@coords)
        names(temp) <- c("x", "y")
        temp2 <- dp(temp, res)
        rivers@polygons[[i]]@Polygons[[j]]@coords <- as.matrix(cbind(temp2$x, temp2$y))
      }
    }

【讨论】:

  • 感谢您的回答,carnust,但根本不清楚。 rivers@data
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2012-09-27
  • 2018-07-05
相关资源
最近更新 更多