Get Census Tract from Lat/Lon using tigris

半世苍凉 提交于 2021-01-29 07:26:08

问题


I have a relatively large number of coordinates for which I'd like to get the census tract (in addition to the FIPS code). I know that I can look up individual lat/lon pairs using call_geolocator_latlon (as done here), but this seems impractical for my purposes as the function issues a single call to the census bureaus' API, and I imagine would take a very long time to run on my ~200,000 pairs.

Is there a faster way to do this, perhaps by downloading shapefiles for each state using the block_groups function and mapping from lat/lon to census tract from there?


回答1:


This doesn't use tigris, but utilizes sf::st_within() to check a data frame of points for overlapping tracts.

I'm using tidycensus here to get a map of California's tracts into R.

library(sf)

ca <- tidycensus::get_acs(state = "CA", geography = "tract",
              variables = "B19013_001", geometry = TRUE)

Now to sim some data:

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))

I'm doing 100 points here to be able to ggplot() the results, but the overlap calculation for 1e6 is fast, only a few seconds on my laptop.

my_points$tract <- as.numeric(st_within(my_points, ca)) # this is fast for 1e6 points

The results:

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)))




回答2:


Great answer above. To get Census tract IDs you could also use st_join(). NAs for the tract IDs are those points that are within California's bounding box but don't intersect the state itself.

library(tigris)
library(tidyverse)
library(sf)

ca_tracts <- tracts("CA", class = "sf") %>%
  select(GEOID, TRACTCE)

bbox <- st_bbox(ca_tracts)

my_points <- data.frame(
  x = runif(200000, bbox[1], bbox[3]),
  y = runif(200000, bbox[2], bbox[4])
) %>%
  # convert the points to same CRS
  st_as_sf(coords = c("x", "y"),
           crs = st_crs(ca_tracts))

my_points_tract <- st_join(my_points, ca_tracts)

> my_points_tract
Simple feature collection with 200000 features and 2 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: -124.4819 ymin: 32.52888 xmax: -114.1312 ymax: 42.0095
epsg (SRID):    4269
proj4string:    +proj=longlat +datum=NAD83 +no_defs
First 10 features:
         GEOID TRACTCE                   geometry
1  06025012400  012400 POINT (-114.6916 33.42711)
2         <NA>    <NA> POINT (-118.4255 41.81896)
3  06053990000  990000 POINT (-121.8154 36.22736)
4  06045010200  010200 POINT (-123.6909 39.70572)
5         <NA>    <NA> POINT (-116.9055 37.93532)
6  06019006405  006405  POINT (-119.511 37.09383)
7  06049000300  000300  POINT (-120.7215 41.3392)
8         <NA>    <NA> POINT (-115.8916 39.32392)
9  06023990100  990100 POINT (-124.2737 40.14106)
10 06071008901  008901  POINT (-117.319 35.62759)


来源:https://stackoverflow.com/questions/52248394/get-census-tract-from-lat-lon-using-tigris

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!