将多边形添加到SpatialPolygonsDataFrame

时间:2020-01-27 14:49:42

标签: r polygon spatial

如何在SpatialPolygonsDataFrame中添加多边形?

示例:下面的脚本将创建一个SpatialPolygonsDataFrame。我想添加一个在现有多边形周围包含一个大正方形的多边形。

   library(rgdal)
   dsn <- system.file("vectors", package = "rgdal")[1]
   Scotland <- readOGR(dsn=dsn , layer="scot_BNG")
   plot(Scotland)

enter image description here

首选最终结果:

enter image description here

重要的是,矩形成为SpatialPolygonsDataFrame的一部分。由于我必须对数据框进行一些计算。因此,手动添加正方形的可视层是不够的。

谢谢!

1 个答案:

答案 0 :(得分:2)

下面的代码创建一个包围原始空间多边形的矩形,并将其作为空间多边形添加到原始形状。

library(rgdal)
library(rgeos)

dsn <- system.file("vectors", package = "rgdal")[1]
Scotland <- readOGR(dsn=dsn , layer="scot_BNG")

# change the width parameter to make the rectangle the desired size
# this results in an extent object that surrounds the original shape
eScotland <- extent(gBuffer(Scotland, width = 50000))
# turn the extent into a Spatial Polygon
pScotland <- as(eScotland, 'SpatialPolygons')
crs(pScotland) <- crs(Scotland)
newScotland <- bind(pScotland, Scotland)
plot(newScotland)

enter image description here