首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R简化shapefile

R简化shapefile
EN

Stack Overflow用户
提问于 2014-01-08 00:12:16
回答 1查看 3.9K关注 0票数 3

我有一个亚马逊大河的shapefile。仅shapefile就有37.9MB,加上属性表,就达到了42.1MB。我正在生成所有巴西亚马逊的PNG图像,每个都是1260x940像素,而shapefile中的所有这些数据只会减慢每个地图的绘制速度,所以我想简化它。

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

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-01-08 00:43:25

非常全面的解决方案:http://www.r-bloggers.com/simplifying-polygon-shapefiles-in-r/

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

代码语言:javascript
复制
area <- lapply(rivers@polygons, function(x) sapply(x@Polygons, function(y) y@area))

rivers是R中的shapefile对象。

然后计算出较大的多边形并保留它们:

代码语言:javascript
复制
    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()函数将完成此任务:

代码语言:javascript
复制
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))
      }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20976449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档