簡體   English   中英

如何在R中用多邊形裁剪WorldMap?

[英]How to clip WorldMap with polygon in R?

我使用 R 包柵格從www.GADM.org導入了一個世界地圖數據集。 我想將它剪輯到我創建的多邊形上以減小地圖的大小。 我可以檢索數據,我可以創建多邊形沒有問題,但是當我使用“gIntersection”命令時,我收到一條模糊的錯誤消息。

關於如何剪輯我的世界地圖數據集的任何建議?

library(raster)
library(rgeos)

## Download Map of the World ##
WorldMap <- getData('countries')

## Create the clipping polygon
clip.extent <- as(extent(-20, 40, 30, 72), "SpatialPolygons")
proj4string(clip.extent) <- CRS(proj4string(WorldMap))

## Clip the map
EuropeMap <- gIntersection(WorldMap, clip.extent, byid = TRUE)

錯誤信息:

RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, "rgeos_intersection") 中的錯誤:幾何集合可能不包含其他幾何集合此外:警告消息:在 RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, "rgeos_intersection") 和 spge:有不同的 proj4 字符串

你不需要使用 PBS(我已經通過艱難的方式學到了這一點,因為@flowla 發布的r-sig-geo鏈接是我最初發布的一個問題!)。 這段代碼展示了如何在 rgeos 中完成這一切,感謝 Roger Bivand 發布的各種不同的帖子 這將是更規范的子集化方式,無需強制轉換為 PolySet 對象。

錯誤消息的原因是您不能對 SpatialPolygons 的集合進行 gIntersection,您需要單獨執行它們。 使用gIntersects找出您想要的gIntersects 然后我使用gIntersection每個國家/地區多邊形進行子集gIntersection 我在將 SpatialPolygons 對象列表傳遞回 SpatialPolygons 時遇到了問題,這會將裁剪后的 shapefile 轉換為 SpatialPolygons,這是因為並非所有裁剪的對象都屬於SpatialPolygons class 一旦我們排除了這些,一切正常。

# This very quick method keeps whole countries
gI <- gIntersects(WorldMap, clip.extent, byid = TRUE )
Europe <- WorldMap[which(gI), ]
plot(Europe)


#If you want to crop the country boundaries, it's slightly more involved:
# This crops countries to your bounding box
gI <- gIntersects(WorldMap, clip.extent, byid = TRUE)
out <- lapply(which(gI), function(x){ 
        gIntersection(WorldMap[x,], clip.extent)
   })

# But let's look at what is returned
table(sapply(out, class))
#   SpatialCollections    SpatialPolygons 
#                    2                 63 


# We want to keep only objects of class SpatialPolygons                 
keep <- sapply(out, class)
out <- out[keep == "SpatialPolygons"]


# Coerce list back to SpatialPolygons object
Europe <- SpatialPolygons(lapply(1:length(out), function(i) {
          Pol <- slot(out[[i]], "polygons")[[1]]
          slot(Pol, "ID") <- as.character(i)
          Pol
   }))

plot(Europe)

在此處輸入圖片說明

如果可以,我建議您查看naturalearthdata 他們有高質量的 shapefile,這些文件會保持最新狀態,並且會不斷檢查錯誤(因為它們是開源的,如果您發現錯誤,請通過電子郵件發送給他們)。 國家邊界位於文化按鈕下。 您會發現它們也更輕巧,您可以選擇適合您需要的分辨率。

稍微中間步驟怎么樣? 我主要從R-sig-Geo 中采用了以下代碼,我認為它應該可以解決問題。 您將需要“maptools”和“PBSmapping”軟件包,因此請確保已安裝它們。 這是我的代碼:

# Required packages
library(raster)
library(maptools)
library(PBSmapping)

# Download world map
WorldMap <- getData('countries')
# Convert SpatialPolygons to class 'PolySet'
WorldMap.ps <- SpatialPolygons2PolySet(WorldMap)
# Clip 'PolySet' by given extent
WorldMap.ps.clipped <- clipPolys(WorldMap.ps, xlim = c(-20, 40), ylim = c(30, 72))
# Convert clipped 'PolySet' back to SpatialPolygons
EuropeMap <- PolySet2SpatialPolygons(WorldMap.ps.clipped, close_polys=TRUE)

我剛剛測試了它,它沒有任何問題。 不過,將 SpatialPolygons 轉換為 PolySet 需要一些計算時間。

干杯,弗洛里安

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM