我有一个亚马逊大河的shapefile。仅shapefile就有37.9MB,加上属性表,就达到了42.1MB。我正在生成所有巴西亚马逊的PNG图像,每个都是1260x940像素,而shapefile中的所有这些数据只会减慢每个地图的绘制速度,所以我想简化它。
rgeos包中的gSimplify函数似乎只简化了每个多边形,并没有去掉较小的多边形。我尝试了它的公差为27633和1000,我总是得到长度(shp@polygons)相同的值:1000。而绘制最终的图所需的时间几乎相同。我需要一个函数,我告诉它,最终的光栅将是1260x940像素,这样它就可以删除所有不必要的点。有没有一个函数可以做到这一点?
提前谢谢。
发布于 2014-01-08 00:43:25
非常全面的解决方案:http://www.r-bloggers.com/simplifying-polygon-shapefiles-in-r/
总而言之,您需要获取多边形的面积:
area <- lapply(rivers@polygons, function(x) sapply(x@Polygons, function(y) y@area))rivers是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))
}
}https://stackoverflow.com/questions/20976449
复制相似问题