簡體   English   中英

如何根據R中的條件合並shapefile中包含的要素

[英]How to merge features contained in a shapefile based on a condition in R

我附上了一個帶有 4 個特征的 shapefile( sample ),每個特征在字段type都有gapoutin (請參閱屬性表)。 我想將特征gap特征與具有最大面積的相鄰多邊形合並(在這種情況下, gap將與in合並)。 我的最終 shapefile 將具有 3 個特征(沒有gap )。 我如何在 R 中做到這一點?

在 ArcGIS 中,有一個用於合並要素的直接工具。 我想知道我們如何在 R 中做到這一點。

這是shapefile的鏈接這是一個示例 shapefile,其中有 4 個特征

這是相應 shapefile 的圖例

這是 shapefile 的屬性表

我已經使用rgeos庫來找出具有最大面積的相鄰多邊形。 這是我的代碼。 我不知道如何將此功能與gap功能合並。

library(rgeos)
adj_mat <- gTouches(shp, byid=TRUE)
a <- adj_mat[which(shp@data$type=="gap"),]
area <- shp@data$SHAPE_Area[which(a=="TRUE")]
final_matching_id <- which(area==max(area))
f_gap <- shp@data[final_matching_id+1,]
f_gap

  OBJECTID SHAPE_Leng SHAPE_Area type
3       13   1.527046 0.09469124   in

gUnionspRbind可能會解決您的問題。

merged_d <- gUnion(shp[which(shp@data$type=="gap"),], shp[final_matching_id+1,])
not_related_d <- shp[c(1:4)[-c(which(shp@data$type=="gap"), final_matching_id+1)],]

shp2 <- spRbind(merged_d, not_related_d)

plot(shp2, col = 2:4)

在此處輸入圖片說明

暫無
暫無

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

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