【发布时间】:2017-03-30 15:22:18
【问题描述】:
使用可用的 shapefile here 我正在尝试两个合并苏丹和南苏丹的多边形,以便在 2010 年获得苏丹的边界。使 shapefile 在 R 中可用的代码是
library(rgdal)
africa <- readOGR(dsn = "Data/Shapes", layer = "AfricanCountries")
class(africa)
[1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
我尝试了不同的软件包和解决方案,例如 raster::intersect、rgeos::gIntersect 或 maptools::unionSpatialPolygons。我总是得到一个空间对象,该对象丢失了属于两个多边形或根本没有联合的数据。
到目前为止,我有以下似乎不是很方便的解决方案:
# Seperate Polygons and Data
sp_africa <- SpatialPolygons(africa@polygons, proj4string = CRS(proj4string(africa)))
dat_africa <- africa@data
# Delete South Sudan from dataframe
dat_africa <- dat_africa[-which(dat_africa$COUNTRY == "South Sudan"),]
# Add row.names to data according to polygon ID'S
rownames(dat_africa) <- dat_africa$OBJECTID
# Get all ID's
allIDs <- africa$OBJECTID
# Get the ID for Sudan, Primary Land (so we only merge the
# Sudan main land (no islands) with the South Sudan main land
sudanID <- africa[which(africa$COUNTRY == "Sudan" & africa$Land_Type == "Primary land"),]$OBJECTID
# Change ID of South Sudan to that of Sudan
allIDs[which(africa$COUNTRY == "South Sudan")] <- sudanID
# Now unite polygons and afterwards merge polygons with data
tmp <- unionSpatialPolygons(sp_africa, IDs = allIDs)
africa2 <- SpatialPolygonsDataFrame(tmp, data = dat_africa)
如果有更简单、更方便的方法,我会很高兴知道。
【问题讨论】: