r - 将多边形添加到 SpatialPolygonsDataFrame
问题描述
如何将多边形添加到 SpatialPolygonsDataFrame?
示例:下面的脚本将创建一个 SpatialPolygonsDataFrame。我想添加一个由现有多边形周围的大正方形组成的多边形。
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
Scotland <- readOGR(dsn=dsn , layer="scot_BNG")
plot(Scotland)
首选的最终结果:
矩形成为 SpatialPolygonsDataFrame 的一部分很重要。因为我必须对数据框进行一些计算。所以手动添加一个正方形的可视层是不够的。
谢谢!
解决方案
以下代码创建一个包围原始空间多边形的矩形,并将其作为空间多边形添加到原始形状中。
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)
推荐阅读
- javascript - TypeError: undefined is not a function (评估'this.state.list.map')
- java - 文本文件包含字符串和数字。如何找到与字符串关联的最大数字?
- java - 从字符串变量中计算 indexOf 个字符
- java - java - 如何使用Java中的注释相关参数检查自定义运行时异常属性
- python - PySpark 的无法解释的行为和删除空值
- swift3 - UIAlert 在呈现后立即被解雇。迅速
- sql - 触发器计数行给出 ORA-04091:表正在变异,触发器/函数可能看不到它
- javascript - 设置 Javascript 文件不是 Rails 服务器的服务器
- android - 无法让服务在 Android 中运行
- python - 了解python函数;返回值