简体   繁体   English

如何根据R中的条件合并shapefile中包含的要素

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

I have attached a shapefile ( sample ) with 4 features and each of them have gap , out , in in the field type (Refer to the attribute table).我附上了一个带有 4 个特征的 shapefile( sample ),每个特征在字段type都有gapoutin (请参阅属性表)。 I would like to merge feature gap feature with the adjacent polygon which has the maximum area (In this case the gap would be merged with in ).我想将特征gap特征与具有最大面积的相邻多边形合并(在这种情况下, gap将与in合并)。 My final shapefile would have 3 features (without gap ).我的最终 shapefile 将具有 3 个特征(没有gap )。 How do I do this in R?我如何在 R 中做到这一点?

In ArcGIS, there is a direct tool to merge feature.在 ArcGIS 中,有一个用于合并要素的直接工具。 I wanted to know how we do this in R.我想知道我们如何在 R 中做到这一点。

Here is the link to the shapefile 这是shapefile的链接这是一个示例 shapefile,其中有 4 个特征

这是相应 shapefile 的图例

这是 shapefile 的属性表

I have used rgeos library to figure out the adjacent polygon with the maximum area.我已经使用rgeos库来找出具有最大面积的相邻多边形。 This is my code.这是我的代码。 I can't figure out how to merge this feature with the gap feature.我不知道如何将此功能与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

gUnion and spRbind maybe solve your problem. 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