[英]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
都有gap
、 out
、 in
(请参阅属性表)。 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的链接
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.