r - 保留完全或部分在一个更大的多边形内的多边形,没有负缓冲区

标签 r sf

我想保留构成更大人口普查地理区域的人口普查区域。这些区域应该完全适合更大的地理范围。

我可以用负缓冲区来做到这一点,但是有更好的方法吗?

library(tidyverse)
library(sf)
library(tidycensus)
library(tigris)
library(tmap)

balt <- places("Maryland",
               year = 2018,
               class = "sf") %>% 
  filter(NAME == "Baltimore") %>% 
  st_transform(4326)

balt_tracts <- get_acs(geography = "tract",
                       variables = "B01003_001E", # population
                       year = 2018,
                       state = "Maryland",
                       survey = "acs5",
                       geometry = TRUE) %>% 
  st_transform(4326)

balt_tracts %>% 
  .[st_within(st_buffer(., -0.001),  
              balt) %>% lengths > 0,] %>% 
  qtm()

enter image description here

交叉路口会返回共享边缘的较大地理区域之外的区域,这是我不想要的。

balt_tracts[balt,] %>% 
  qtm()

enter image description here

编辑

我正在寻找一种方法来获取 st_overlaps() 的结果并仅保留边界内的那些多边形 (balt)。

balt_tracts[st_overlaps(balt_tracts, balt) %>% lengths > 0,] %>% 
  tm_shape() + tm_polygons(col = "GEOID")

enter image description here

我注意到的一件事是 st_touches() 只返回三个多边形。这是一个精度问题,还是意味着人口普查区边界实际上并未与较大多边形的边界完全对齐?

 balt_tracts[st_touches(balt_tracts, balt) %>% lengths > 0,] %>% 
   tm_shape() + tm_polygons(col = "GEOID")

enter image description here

最佳答案

在您的具体示例中,您应该只向 get_acs() 提供一个 county 参数,因为巴尔的摩市是一个独立的实体:

library(tidyverse)
library(sf)
library(tidycensus)
library(tigris)
library(tmap)

balt_tracts <- get_acs(geography = "tract",
                       variables = "B01003_001E", # population
                       year = 2018,
                       state = "Maryland",
                       county = "Baltimore city",
                       survey = "acs5",
                       geometry = TRUE) 

qtm(balt_tracts)

enter image description here

但是,您提出的更广泛的问题是一个值得提出的问题。如果您知道您的几何图形完全对齐(就像在同一年使用 tigris/tidycensus 时一样,前提是 cb = TRUEcb = FALSE 一致使用),您可以使用 sf::st_filter()st_within 空间谓词来进行空间子集化。例如,假设我们想要获取巴尔的摩都市区(完全位于马里兰州)的人口普查区:

balt_metro <- core_based_statistical_areas(cb = TRUE, 
                                           year = 2018, 
                                           class = "sf") %>%
  filter(str_detect(NAME, "Baltimore"))

balt_metro_tracts <- tracts("MD", cb = TRUE, 
                            year = 2018, class = "sf") %>%
  st_filter(balt_metro, .predicate = st_within)

ggplot() + 
  geom_sf(data = balt_metro_tracts, fill = "white") + 
  geom_sf(data = balt_metro, fill = NA, color = "red")

enter image description here

关于r - 保留完全或部分在一个更大的多边形内的多边形,没有负缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62667218/

相关文章:

r - 按固定距离缩小 SF 多边形

r - 如何使用 ggplot2 中的 shapefile 从第一个图中保留 scale_fill_color

r - tidyr::complete 带有可变长度的列名向量

r - `dist`中的 `st_buffer`参数默认设置为什么单位?

r - 如何在 Shiny 应用程序的 selectModUI 中更新传单 map ?

r - 在空间范围内创建规则多边形网格,旋转给定角度

r - 根据行中的 NA 选择数据框中的列

R:如何计算 rpart 树的敏感性和特异性

R 编程 - 子集时间特定数据

r - 使用 ggplot 绘制颜色取决于类别的时间序列