首页 > 解决方案 > 将多边形添加到 SpatialPolygonsDataFrame

问题描述

如何将多边形添加到 SpatialPolygonsDataFrame?

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

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

在此处输入图像描述

首选的最终结果:

在此处输入图像描述

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

谢谢!

标签: rpolygonspatial

解决方案


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

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)

在此处输入图像描述


推荐阅读