这不使用tigris,而是sf::st_within()用于检查重叠区域的点数据框。
我在tidycensus这里使用将加利福尼亚大片的地图导入 R。
library(sf)
ca <- tidycensus::get_acs(state = "CA", geography = "tract",
variables = "B19013_001", geometry = TRUE)
Run Code Online (Sandbox Code Playgroud)
现在来模拟一些数据:
bbox <- st_bbox(ca)
my_points <- data.frame(
x = runif(100, bbox[1], bbox[3]),
y = runif(100, bbox[2], bbox[4])
) %>%
# convert the points to same CRS
st_as_sf(coords = c("x", "y"),
crs = st_crs(ca))
Run Code Online (Sandbox Code Playgroud)
我在这里做了 100 分以ggplot()获得结果,但是 1e6 的重叠计算速度很快,在我的笔记本电脑上只有几秒钟。
my_points$tract <- as.numeric(st_within(my_points, ca)) # this is fast for 1e6 points
Run Code Online (Sandbox Code Playgroud)
结果:
head(my_points) # tract is the row-index for overlapping census tract record in 'ca'
# but part would take forever with 1e6 points
library(ggplot2)
ggplot(ca) +
geom_sf() +
geom_sf(data = my_points, aes(color = is.na(tract)))
Run Code Online (Sandbox Code Playgroud)