R相交多个(2+)重叠多边形

R intersecting multiple (2+) overlapping polygons

我在使用 st_intersection 时遇到一些问题。我正在尝试从公交车站缓冲区中交叉多边形以准备区域插值。这是数据: 这是数据: https://realtime.commuterpage.com/rtt/public/utility/gtfs.aspx

这是我的代码:

ART2019Path <- file.path(GTFS_path, "2019-10_Arlington.zip")
ART2019GTFS <- read_gtfs(ART2019Path)
ART2019StopLoc <- stops_as_sf(ART2019GTFS$stops) ### Make a spatial file for stops
ART2019Buffer <- st_buffer(ART2019StopLoc, dist = 121.92) ### Make buffer with 400ft (121.92m) radius

它创建的内容类似于下图(使用 mapview 创建);如您所见,有多个重叠缓冲区。

我尝试使用以下方法与多边形相交:

BufferIntersect <- st_intersection(ART2019Buffer, ART2019Buffer)
BufferIntersect <- st_make_valid(BufferIntersect) ### Fix some of the polygons that didn't quite work

但它只与两层多边形相交,这意味着仍然存在重叠。如何使所有缓冲区相交?

我在这里看过类似的问题:Loop to check multiple polygons overlap in r SF package

但是没有答案。

其中一条评论建议使用以下链接:

https://r-spatial.org/r/2017/12/21/geoms.html

https://r-spatial.github.io/sf/reference/geos_binary_ops.html#details-1

但我无法工作。任何帮助将不胜感激。


编辑

针对一些评论的几点澄清。

  1. 我对公交车站缓冲区内每个唯一多边形的面积感兴趣,因为我将在区域插值中使用这些多边形和人口普查数据来估计可以进入公交车站的人口

  2. 400 英尺步行距离是公交车站可达性的标准做法

像这样的东西对我有用,尽管有点慢。在这里,我遍历每个停止缓冲区和 运行 包含所有其他停止缓冲区的对象的交集过程 不包括该停止缓冲区 .

library(sf)
library(tidyverse)

df<-read.csv("YOUR_PATH/google_transit/stops.txt")

# Read data
ART2019StopLoc <- st_as_sf(df, coords=c('stop_lon', 'stop_lat'))
ART2019StopLoc <- st_set_crs(ART2019StopLoc, value=4326)

# Make buffer
ART2019Buffer <- st_buffer(ART2019StopLoc, dist=121.92)

# Create empty data frame to store results
results <- data.frame()

# Loop through each stop and intersect with other stops
for(i in 1:nrow(ART2019Buffer)) {
  
  # Subset to stop of interest
  stop <- ART2019Buffer[i,]

  # Subset to all other stops excl. stop of interest
  stop_check <- ART2019Buffer[-i,]
  
  # Intersect and make valid
  stop_intersect <- st_intersection(stop, stop_check) %>% 
    st_make_valid()
  
  # Create one intersected polygon
  stop_intersect <- st_combine(stop_intersect) %>% 
    st_as_sf() %>% 
    mutate(stop_name=stop$stop_name)
  
  # Combine into one results object
  results <- rbind(results, stop_intersect)
  print(i)
  
}

ggplot() +
  geom_sf(data=ART2019Buffer %>% filter(stop_name %in% results$stop_name),
          fill='gray70') +
  geom_sf(data=results, aes(fill=stop_name), alpha=0.5)

下图显示了前 8 个停靠点的结果。灰色圆圈是原始停止缓冲区,彩色缓冲区显示与相邻缓冲区的交集。

听起来您只需要缓冲区,但不必处理所有重叠部分。一个人在一个 bus-stop 或三个人的 400 英尺范围内并不重要,对吧?

如果是这样,您可以使用 st_union 函数将缓冲区“混合”在一起。

library(tidytransit)
library(sf)
library(mapview)
library(ggplot2)

# s2 true allows buffering in meters, s2 off later speeds things up
sf::sf_use_s2(TRUE)
ART2019Path <- file.path("/your/file/path/")
ART2019GTFS <- read_gtfs(ART2019Path)
ART2019StopLoc <- stops_as_sf(ART2019GTFS$stops) ### Make a spatial file for stops
ART2019Buffer <- st_buffer(ART2019StopLoc, dist = 121.92) ### Make buffer with 400ft (121.92m) radius

# might be needed due to some strange geometries in buffer, and increase speed
sf::sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off

# MULTIPOLYGON  sfc object covering only the buffered areas,
#  there are no 'overlaps'.
buff_union <- st_union(st_geometry(ART2019Buffer))
#> although coordinates are longitude/latitude, st_union assumes that they are planar

buff_union
#> Geometry set for 1 feature 
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -77.16368 ymin: 38.83828 xmax: -77.04768 ymax: 38.9263
#> Geodetic CRS:  WGS 84
#> MULTIPOLYGON (((-77.08604 38.83897, -77.08604 3...

# Non-overlapping buffer & stops
ggplot() + 
  geom_sf(data = buff_union, fill = 'blue', alpha = .4) +
  geom_sf(data = ART2019StopLoc, color = 'black') + 
  coord_sf(xlim = c(-77.09, -77.07),
           ylim = c(38.885, 38.9)) 

# Overlapping buffer & stops
ggplot() +
  geom_sf(data = ART2019Buffer, fill = 'blue', alpha = .4) +
  geom_sf(data = ART2019StopLoc, color = 'black') +
  coord_sf(xlim = c(-77.09, -77.07),
           ylim = c(38.885, 38.9)) 

# Back to original settings
sf::sf_use_s2(TRUE)
#> Spherical geometry (s2) switched on

reprex package (v2.0.1)

于 2022-04-18 创建