Skip to content

Commit

Permalink
fix geometry issue for 2020 ZCTAs
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerke committed May 27, 2023
1 parent 418dc32 commit bb8109f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 20 additions & 4 deletions R/census.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ get_decennial <- function(geography,

if (geography == "zcta") geography <- "zip code tabulation area"

if (geography == "zip code tabulation area" && !is.null(state)) {
geography <- "zip code tabulation area (or part)"
}

if (year == 2020 && sumfile == "pl" && geography == "zip code tabulation area") {
stop("ZCTAs are not available in the 2020 PL file.", call. = FALSE)
}

if (geography == "zip code tabulation area" && !is.null(state)) {
geography <- "zip code tabulation area (or part)"
}

# if (geography == "zip code tabulation area" && is.null(state)) {
# stop("ZCTA data for the decennial Census is only available by state from tidycensus.",
# call. = FALSE)
Expand Down Expand Up @@ -377,6 +377,15 @@ get_decennial <- function(geography,

}

# For ZCTAs, strip the state code from GEOID (issue #338 and #358)
# Should only happen if the GEOID is 7 characters
if (geography == "zip code tabulation area (or part)" && year == 2020 && unique(nchar(dat2$GEOID)) == 7) {
dat2 <- dat2 %>%
dplyr::mutate(
GEOID = stringr::str_sub(GEOID, start = 3L)
)
}

if (!is.null(summary_var)) {

sumdat <- suppressMessages(try(load_data_decennial(geography, summary_var, key, year,
Expand All @@ -387,6 +396,13 @@ get_decennial <- function(geography,
sumfile = "sf3", state, county, show_call = show_call)))
}

if (geography == "zip code tabulation area (or part)" && year == 2020 && unique(nchar(dat2$GEOID)) == 7) {
dat2 <- dat2 %>%
dplyr::mutate(
GEOID = stringr::str_sub(GEOID, start = 3L)
)
}

dat2 <- dat2 %>%
inner_join(sumdat, by = "GEOID") %>%
rename("summary_value" = !! summary_var,
Expand Down
6 changes: 6 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ use_tigris <- function(geography, year, cb = TRUE, resolution = "500k",
year <- 2020
}

# We can't pull ZCTA shapes by state for 2020 but this is
# an available hierarchy in the DHC. So set state to NULL.
if (year == 2020) {
state <- NULL
}

z <- zctas(cb = cb, starts_with = starts_with, year = year,
class = "sf", state = state, ...)

Expand Down

0 comments on commit bb8109f

Please sign in to comment.