我用你的问题试着使用新的库进行了一些操作...
获取英国地图并定义随机点
library(raster)
library(sf)
library(ggplot2)
library(dplyr)
library(tidyr)
library(forcats)
library(purrr)
GBR <- getData(name = "GADM", country = "GBR", level = 1)
GBR_sf <- st_as_sf(GBR)
pts <- matrix(c(-0.4966766, -2.0772529, -3.8437793,
51.91829, 52.86147, 56.73899), ncol = 2)
pts_sf <- st_sfc(st_multipoint(pts), crs = 4326) %>%
st_sf() %>%
st_transform(27700)
ggplot() +
geom_sf(data = GBR_sf) +
geom_sf(data = pts_sf, colour = "red")
![数据地图](https://istack.dev59.com/HbnfT.webp)
计算缓冲区域
我们为每个缓冲距离创建一个 multipolygons
列表。由于缓冲距离是基于坐标系的比例尺来计算的,因此点数据集必须使用投影坐标(这里是墨卡托)。
dists <- seq(5000, 150000, length.out = 5)
pts_buf <- purrr::map(dists, ~st_buffer(pts_sf, .)) %>%
do.call("rbind", .) %>%
st_cast() %>%
mutate(
distmax = dists,
dist = glue::glue("<{dists/1000} km"))
ggplot() +
geom_sf(data = GBR_sf) +
geom_sf(data = pts_buf, fill = "red",
colour = NA, alpha = 0.1)
![缓冲重叠](https://istack.dev59.com/dOxaA.webp)
去除重叠
缓冲区域重叠。在上图中,更强烈的红色是由于透明红色的多层重叠造成的。让我们去掉重叠部分。我们需要从较大的区域中删除尺寸较小的缓冲区。然后,我需要将最小的区域再次添加到列表中。
pts_holes <- purrr::map2(tail(1:nrow(pts_buf),-1),
head(1:nrow(pts_buf),-1),
~st_difference(pts_buf[.x,], pts_buf[.y,])) %>%
do.call("rbind", .) %>%
st_cast() %>%
select(-distmax.1, -dist.1)
pts_holes_tot <- pts_holes %>%
rbind(filter(pts_buf, distmax == min(dists))) %>%
arrange(distmax) %>%
mutate(dist = forcats::fct_reorder(dist, distmax))
ggplot() +
geom_sf(data = GBR_sf) +
geom_sf(data = pts_holes_tot,
aes(fill = dist),
colour = NA) +
scale_fill_brewer(direction = 2)
![Buffer with holes - donut polygons](https://istack.dev59.com/zxoom.webp)
删除海洋区域
如果您只想在陆地上查找邻近区域,则需要删除位于海洋中的缓冲区域。使用相同投影的多边形
之间进行交集计算。我之前对英国地图进行了联合处理。
GBR_sf_merc <- st_transform(st_union(GBR_sf), 27700)
pts_holes_uk <- st_intersection(pts_holes_tot,
GBR_sf_merc)
ggplot() +
geom_sf(data = GBR_sf) +
geom_sf(data = pts_holes_uk,
aes(fill = dist),
colour = NA) +
scale_fill_brewer(direction = 2)
这是使用
sf
、
ggplot2
和其他几个库创建的最终接近度地图...
rgeos
函数gBuffer()
来完成此操作。这份文档提供了一个很好的例子:http://www.nickeubank.com/wp-content/uploads/2015/10/RGIS2_MergingSpatialData_part2_GeometricManipulations.html - tktk234