2014-01-07 22 views
3

我有一個amazonic大河流的shapefile。單獨的shapefile具有37.9 MB,與屬性表一起高達42.1 MB。我生成所有巴西亞馬遜的PNG圖像,每個像素爲1260x940像素,shapefile中的所有這些數據只會減慢每張地圖的繪製速度,所以我想簡化它。R簡化shapefile

gSimplify函數,在rgeos包中,似乎只是簡化每個多邊形,而不是擺脫較小的。我嘗試了0.1和1000的容差,並且總是得到長度(shp @ polygons)相同的值:27633.最終的繪圖幾乎需要同一時間繪製。我需要一個函數,我告訴最終的光柵將是1260x940像素,因此它可以刪除每個不必要的點。有沒有這樣的功能?

在此先感謝。

+3

它不是的R解決方案,但我會推薦mapshaper.org。非常容易加載一個shape文件,並進行不同級別的簡化實驗,然後重新保存。我發現這對於加速R中的地圖繪製非常有用。 – Andy

+0

謝謝@Andy!它像一個魅力。改變簡化級別並實時查看結果真的很不錯。但我仍然想用R來做到這一點,因爲問題仍然存在(並且有一天該網站可能無法到達)。 – Rodrigo

+0

不用擔心@羅德里戈,你的願望是可以理解的,4年前我也有同感,並試圖找到一個好的R解決方案,最終放棄。 mapshaper在這段時間裏變得更好了。如果你確實找到了一個好的R解決方案,我會很樂意聽到。 – Andy

回答

2

相當全面的解決方案在這裏:http://www.r-bloggers.com/simplifying-polygon-shapefiles-in-r/

總之,你需要讓你的多邊形區域:

area <- lapply([email protected], function(x) sapply([email protected], function(y) [email protected])) 

江河是您在R.

shape文件對象

然後你找出大的多邊形,並保留它們:

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

    [email protected] <- [email protected][-c(1:2),] 
    [email protected] <- [email protected][-c(1:2)] 
    [email protected] <- 1:length([email protected]) 
    mainPolys <- mainPolys[-c(1:2)] 

    for(i in 1:length(mainPolys)){ if(length(mainPolys[[i]]) >= 1 && 
    mainPolys[[i]][1] >= 1){ 
     [email protected][[i]]@Polygons <- [email protected][[i]]@Polygons[mainPolys[[i]]] 
     [email protected][[i]]@plotOrder <- 1:length([email protected][[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([email protected])){ 
     for(j in 1:length([email protected][[i]]@Polygons)){ 
     temp <- as.data.frame([email protected][[i]]@Polygons[[j]]@coords) 
     names(temp) <- c("x", "y") 
     temp2 <- dp(temp, res) 
     [email protected][[i]]@Polygons[[j]]@coords <- as.matrix(cbind(temp2$x, temp2$y)) 
     } 
    } 
+0

感謝您的回答,carnust,但一點都不清楚。河流@數據< - 河流@數據[-c(1:2),]這將刪除前兩條河流數據。我的shapefile有27633塊。他說:「當我們看mainPolys時,我們發現前兩個更大的多邊形分組中沒有多大的多邊形。」他是怎麼看到這個的?謝謝。 – Rodrigo

相關問題