向SpatialPolygonsDataFrame添加多边形 [英] Adding a Polygon to a SpatialPolygonsDataFrame
问题描述
如何在SpatialPolygonsDataFrame中添加多边形?
How to add an polygon to a SpatialPolygonsDataFrame?
示例:下面的脚本将创建一个SpatialPolygonsDataFrame.我想添加一个多边形,该多边形由围绕现有多边形的大正方形组成.
Example: The script underneath will create a SpatialPolygonsDataFrame. I would like to add a polygon which consists of a large square around the existing polygons.
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
Scotland <- readOGR(dsn=dsn , layer="scot_BNG")
plot(Scotland)
首选最终结果:
重要的是,矩形必须成为SpatialPolygonsDataFrame的一部分.由于我必须对数据框进行一些计算.因此,手动添加正方形的可视层是不够的.
It is important that the rectangle becomes part of the SpatialPolygonsDataFrame. Since I have to do some calculation of the dataframe. So manually adding a visual layer of a square is insufficient.
谢谢!
推荐答案
下面的代码创建一个包围原始空间多边形的矩形,并将其作为空间多边形添加到原始形状.
The following code creates a rectangle that encloses the original spatial polygons and adds this as a spatial polygon to the original shape.
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)
这篇关于向SpatialPolygonsDataFrame添加多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!