简体   繁体   English

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

[英]R intersecting multiple (2+) overlapping polygons

I am having some issues with st_intersection.我在使用 st_intersection 时遇到了一些问题。 I am trying to intersect polygons from bus stop buffers in preparation for areal interpolation.我正在尝试从公交车站缓冲区中交叉多边形以准备区域插值。 Here are the data: Here is the data: https://realtime.commuterpage.com/rtt/public/utility/gtfs.aspx这是数据:这是数据: https://realtime.commuterpage.com/rtt/public/utility/gtfs.aspx

Here is my code:这是我的代码:

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

It creates something that looks like the image below (created using mapview);它创建的内容类似于下图(使用 mapview 创建); as you can see, there are multiple overlapping buffers.如您所见,有多个重叠缓冲区。

在此处输入图像描述

I tried intersecting the polygons using the following:我尝试使用以下方法与多边形相交:

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

But it only intersects two layers of polygons, meaning there is still overlap.但它只与两层多边形相交,这意味着仍然存在重叠。 How do I make all buffers intersect?如何使所有缓冲区相交?

I have looked at similar questions on here like this: Loop to check multiple polygons overlap in r SF package我在这里看过类似的问题: Loop to check multiple polygons overlap in r SF package

But there is no answer.但是没有答案。

One of the comments suggested the following links:其中一条评论建议使用以下链接:

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

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

But I can't get either to work.但我无法工作。 Any help would be greatly appreciated.任何帮助将不胜感激。


Edit编辑

Couple of clarifying points in response to some comments.针对一些评论的几个澄清点。

  1. I am interested in the area of each unique polygon within bus stop buffers as I will be using these polygons in an areal interpolation with census data to estimate population with access to bus stops我对公交车站缓冲区内每个唯一多边形的面积感兴趣,因为我将在带有人口普查数据的区域插值中使用这些多边形来估计可以进入公交车站的人口

  2. 400ft walking distance is standard practice for bus stop accessibility 400 英尺步行距离是公交车站可达性的标准做法

It sounds like you just want the buffer(s), but without having to deal with all of the overlapping sections.听起来您只需要缓冲区,但不必处理所有重叠部分。 It doesn't matter if a person is within 400ft of one bus-stop or three, right?一个人在一个或三个公共汽车站的 400 英尺范围内并不重要,对吧?

If so, you can use the st_union function to "blend" the buffers together.如果是这样,您可以使用st_union function 将缓冲区“混合”在一起。

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

Created on 2022-04-18 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-04-18

Something like this works for me, albeit a bit slowly.这样的事情对我有用,尽管有点慢。 Here I loop through each stop buffer and run the intersection process on an object containing all other stop buffers excluding that stop buffer .在这里,我循环遍历每个停止缓冲区,并在 object 上运行交集过程,其中包含除该停止缓冲区之外的所有其他停止缓冲区

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)

The plot below shows the results for the first 8 stops.下面的 plot 显示了前 8 站的结果。 The gray circles are the original stop buffers and the colored buffers show the intersection with adjacent buffers.灰色圆圈是原始停止缓冲区,彩色缓冲区显示与相邻缓冲区的交集。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM