gpt4 book ai didi

R 简化 shapefile

转载 作者:行者123 更新时间:2023-12-02 01:54:27 28 4
gpt4 key购买 nike

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

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

提前致谢。

最佳答案

这里有相当全面的解决方案: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))
}
}

关于R 简化 shapefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20976449/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com