gpt4 book ai didi

r - 如何仅向外缓冲一组具有公共(public)边的多边形(从边缘,而不是内部 "frontiers")

转载 作者:行者123 更新时间:2023-12-05 03:43:48 26 4
gpt4 key购买 nike

我正在使用爱尔兰共和国各县的 map 执行此操作,找到 here (直接下载)。

我想做的是将每个县的海岸向海延伸 1500 米,但不移动县与县之间的边界。

编辑:我还需要将沿海县的县界向大海延伸,以满足新的、延伸的“海岸线”(根据 mrhellmann 问题添加)

到目前为止我的代码:

  1. 加载包和数据

    library(tidyverse)
    library(sf)
    library(ggplot2)

    ireland <- st_read("~Census2011_Admin_Counties_generalised20m.shp")
    st_crs(ireland) # units are m
  2. 缓冲区

    ireland_buf <- ireland %>% 
    st_buffer(1500)
  3. 检查边界如何移动

    ggplot() + 
    geom_sf(data = ireland_buf, col = "red", fill = "lightgray") +
    geom_sf(data = ireland, col = "black", fill = NA) +
    theme_bw()

enter image description here

所以,基本上,红线对应于缓冲 map ,黑线对应于原始 map 。我已经在海岸线上获得了预期的效果,但是县边界会根据县向内或向外移动

最佳答案

使用基础 shapefile,您可以通过组合、缓冲、空间采样和创建 voronoi 多边形来创建一些其他对象。通过连接和交叉这些,您可以到达那里的大部分路径。

以下示例的问题在于它缓冲到北部边界的相邻陆地,一些靠得很近的岛屿可能会导致非常小的问题。

library(sf)
library(ggplot2)
library(dplyr)
library(tmap) # for "MAP_COLORS"


path <- 'Census2011_Admin_Counties_generalised20m.shp'
ireland <- read_sf(path)

base_crs <- st_crs(ireland)

#remove county lines, keeps only an outline of the area
ireland_combined <- st_combine(ireland)
# add buffer, fortunately the base CRS is in meters
ireland_buffered <- st_buffer(ireland_combined, 1500)
# polygon(s) of only the buffer
buffer_only <- st_difference(ireland_buffered, ireland_combined)

# sample points inside the buffer for voronoi polygons
# type can be random, hexagonal, or regular.
# increase size for accuracy, decrease for speed.
sampled_points <- st_sample(buffer_only, size = 5000, type = 'hexagonal')

#Create voronoi polygons from sampled points
voronoi <- sampled_points %>%
st_union() %>%
st_voronoi() %>%
st_cast()

# Crop voronoi polygons to buffer area only
# plot 2 below shows voronoi polygons extend much too far for this case
voronoi_buffer <-
st_intersection(
st_make_valid(voronoi),
buffer_only
) %>%
st_as_sf()

# Join polygons by proximity to counties in base shapefile
voronoi_joined <- st_join(
voronoi_buffer,
ireland,
join = st_nearest_feature
)

# join voronoi polygons by countyname
vj_summarized <- voronoi_joined %>%
group_by(COUNTYNAME) %>%
summarize()

#mapview was having trouble with this county name
vj_summarized$COUNTYNAME[6] <- 'Dun Laoghaire-Rathdown'
ireland$COUNTYNAME[20] <- 'Dun Laoghaire-Rathdown'

#rbind sea buffer & land, then join with group_by & summarize
buffer_and_land <- rbind(ireland %>% select(COUNTYNAME), vj_summarized) %>%
group_by(COUNTYNAME) %>%
summarise()


#plots
p1 <- ggplot() +
geom_sf(data = ireland, fill = NA) +
geom_sf(data = ireland_buffered, fill = NA, alpha = .4, color = 'green') +
ggtitle(label = "Ireland counties & 1500m buffer")

p2 <- ggplot() +
geom_sf(data = voronoi, alpha = .2) +
geom_sf(data = voronoi_buffer, alpha = .2, fill = NA, color = 'turquoise') +
ggtitle(label = 'Voronoi & buffer')

# Counties & buffers joined & colored
p4 <- tm_shape(st_geometry(buffer_and_land)) +
tm_polygons(col = "MAP_COLORS") +
tm_shape(st_geometry(ireland)) +
tm_polygons(border.col = 'black', col = NA, alpha = 0)

p1

绿色区域显示缓冲区和缓冲到边界土地的问题。您可以通过使用整个岛屿的 shapefile 来避免这种情况。

p2

显示完整的 voronoi。绿松石感兴趣的领域。

p4

Buffer 与县相连,由县名填充。县边界以黑色覆盖。

其中一个较困惑区域的特写,增加采样点的数量可能会有所帮助。 enter image description herereprex package 创建于 2021-03-22 (v1.0.0)

关于r - 如何仅向外缓冲一组具有公共(public)边的多边形(从边缘,而不是内部 "frontiers"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66641381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com