我正在尝试使用 sf 图书馆和传单来查找哪些市议会区代表纽约市的哪些邮政编码。我有两个议会区和邮政编码的空间多边形。我的问题是,每当我尝试找到多边形之间的交集时,我都会得到与地区边界重叠的额外邮政编码。该地区没有任何部分属于边界之外的邮政编码,但使用空间交叉点包括那些毗邻邮政编码的区域。
是否有一个空间操作函数可以仅返回多边形的交集,而不返回多边形仅共享边界的位置?
这是我的问题的视觉效果(请参阅邮政编码(灰色)和议会区(黑色)的重叠边框:
我的代码:
library(sf)
#NYC Zip Codes
nyc_zips <- read_sf('ZIP_CODE_040114.shp')
#Left joining zip code boundaries with total count of drivers in each zip
ny_zips_drivers <- left_join(nyc_zips, driver_zips, by = 'ZIPCODE')
#City Council
cc_shp <- read_sf('nycc_22a/nycc.shp')
#Spatial Intersection of council districts and zip codes
cc_intersection <- st_intersection(x = cc_shp, y = ny_zips_drivers)
model
的sf::st_intersection()
论点很有用。它仅适用于未投影的 CRS(在投影 CRS 的上下文中将使用 GEOS,它在不包含此功能的DE-9IM的上下文中工作)。
它可用于指定多边形是否包含(模型=“封闭”)或不包含(模型=“开放”)其边界。
举个例子,考虑这段代码,它构建在众所周知且深受喜爱的
nc.shp
之上,随 {sf}
一起提供:
library(sf)
library(dplyr)
shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package
# will S2 be used? at all?? If not >> st_transform to EPSG:4326 as a good default
st_is_longlat(shape)
# [1] TRUE
# County Mecklenburg (as in Charlotte of Mecklenburg-Strelitz)
strelitz <- shape[shape$NAME == 'Mecklenburg', ]
# closed behaviour - including borders = co. Mecklenburg + all its NC neighbours
st_intersection(shape, strelitz, model = "closed") %>%
pull(NAME)
# [1] "Iredell" "Lincoln" "Mecklenburg" "Cabarrus" "Gaston" "Union"
# open behaviour - excluding borders = co. Mecklenburg only
st_intersection(shape, strelitz, model = "open") %>%
pull(NAME)
# [1] "Mecklenburg"