简体   繁体   English

R 如何合并具有多个多边形的 shapefile 中的多边形特征? (可重现的代码示例)

[英]R How do I merge polygon features in a shapefile with many polygons? (reproducible code example)

How do I merge polygon features in a shapefile with many polygons?如何在具有多个多边形的 shapefile 中合并多边形要素?

rbind and union just combine the rows of the shapefile features, they don't actually merge the polygons themselves. rbind 和 union 只是组合 shapefile 特征的行,它们实际上并没有合并多边形本身。

Desired result in example below:以下示例中的预期结果:

How do I get the below shapefile with duplicated ID_2 to merge to a single polygon in sptemp?如何获得以下具有重复 ID_2 的 shapefile 以合并到 sptemp 中的单个多边形?

Example below of GADM level 2 of Ethiopia has first two rows of the shapefile ID_2 column duplicated (value=1).以下埃塞俄比亚 GADM 级别 2 的示例具有重复的 shapefile ID_2 列的前两行(值 = 1)。 I'd like sptemp with 79 features combining the first two rows that are the ones with duplicated ID_2.我想要具有 79 个特征的 sptemp,将前两行与重复的 ID_2 相结合。 A plot of sptemp[1,] would bring up where current sptemp[1,] and sptemp2[2,] without the boundaries between the duplicated, ie the polygons are merged too. sptemp[1,] 的绘图将显示当前 sptemp[1,] 和 sptemp2[2,] 之间没有重复边界的地方,即多边形也被合并。

Example Code:示例代码:

Download, unzip, and load into R GADM file for Ethiopia level 2 (899kb to working directory):下载、解压缩并加载到埃塞俄比亚 2 级的 R GADM 文件(899kb 到工作目录):

library(curl)
library(rgdal)

curl_download("http://biogeo.ucdavis.edu/data/gadm2.8/shp/ETH_adm_shp.zip",
              destfile=paste0("gadmETH.zip"),
              quiet=FALSE)

unzip(paste0("gadmETH.zip"), exdir="gadmETH", overwrite=FALSE) 


###Load shapefile
sptemp <- readOGR(dsn="gadmETH", layer="ETH_adm2")

The ID_2 column for the first two polygons is duplicated前两个多边形的 ID_2 列是重复的

###You'll see in the first two rows ID_2 is duplicated
df.sptemp <- as.data.frame(sptemp)

View(sptemp)

###I can't just delete one because they are separate polygons 
plot(sptemp[1,], col="blue")
plot(sptemp[2,], add=TRUE, col="red" )

Note This method uses st_union , which combines all the 'multipolygons' into single polygons.注意此方法使用st_union ,它将所有“多边形”组合成单个多边形。 This may not be your actual desired result.这可能不是您实际想要的结果。


If you use library(sf) as opposed to sp (it's the successor to sp ), you can use st_union to join geometries.如果您使用library(sf)而不是sp (它是sp的继承者),则可以使用st_union连接几何。

You can do this inside a dplyr pipe sequence too.您也可以在dplyr管道序列中执行此操作。

library(sf)
sptemp <- sf::st_read(dsn = "~/Desktop/gadmETH/", layer = "ETH_adm2")

library(dplyr)

sptemp %>% 
    group_by(ID_2) %>%
    summarise(geometry = sf::st_union(geometry)) %>%
    ungroup()

# Simple feature collection with 79 features and 1 field
# geometry type:  GEOMETRY
# dimension:      XY
# bbox:           xmin: 33.00154 ymin: 3.398823 xmax: 47.95823 ymax: 14.84548
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
# # A tibble: 79 x 2
#      ID_2                       geometry
#     <dbl>         <sf_geometry [degree]>
#   1    1. POLYGON ((38.85556 8.938293...
#   2    2. POLYGON ((42.15579 12.72123...
#   3    3. POLYGON ((40.17299 14.49028...
#   4    4. POLYGON ((41.11739 10.93207...
#   5    5. POLYGON ((40.61546 12.7958,...
#   6    6. POLYGON ((40.25209 11.24655...
#   7    7. POLYGON ((36.35452 12.04507...
#   8    8. POLYGON ((40.11263 10.87277...
#   9    9. POLYGON ((37.39736 11.60206...
#   10   10. POLYGON ((38.48427 12.32812...
#  # ... with 69 more rows

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历 R 中单个 shapefile 中的多个重叠多边形以获取每个多边形的区域统计信息? - How can I iterate through multiple overlapping polygons in a single shapefile in R to get zonal statistics of each polygon? 如何从 R 中的 shapefile 中提取特征样本? - How do I take a sample of features from a shapefile in R? 如何确保R / Rcpp代码可重现(“可分发”)? - How do I ensure R / Rcpp code is reproducible (“distributable”)? 如何根据R中的条件合并shapefile中包含的要素 - How to merge features contained in a shapefile based on a condition in R 如何在R中将多边形shapefile的许多字段自动转换为栅格 - How to automatically convert many fields of a polygon shapefile to raster in R 如何使用 R markdown 在不同的页面上引用 R 代码(块)(文本中提供的可重复示例) - How can I refer to an R code (chunk) on a different page using R markdown (reproducible example provided in the text) 如何通过 R 中的另一个多边形 shapefile 剪辑多边形 shapefile? - How to clip a polygon shapefile by another polygon shapefile in R? 如何在R或ArcGIS中处理多边形shapefile中的“孤立孔”? - How can I handle an “orphaned hole” in a polygon shapefile in R or ArcGIS? 如何在r中的shapefile中循环遍历多边形? - How to loop through polygons in a shapefile in r? R ggplot2与shapefile和csv数据合并以填充特定的多边形 - R ggplot2 merge with shapefile and csv data to fill polygons - specific
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM