diff --git a/R/enumeration_units.R b/R/enumeration_units.R index f3576af..608971d 100644 --- a/R/enumeration_units.R +++ b/R/enumeration_units.R @@ -53,19 +53,9 @@ #' } counties <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL, ...) { - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) + check_tigris_resolution(resolution) - message(sprintf("Retrieving data for the year %s", year)) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) if (cb == TRUE) { @@ -86,12 +76,12 @@ counties <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL, if (year > 2013) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_county_%s.zip", - cyear, cyear, resolution) + year, year, resolution) } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/cb_%s_us_county_%s.zip", - cyear, cyear, resolution) + year, year, resolution) } @@ -104,15 +94,15 @@ counties <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL, if (year %in% c(2000, 2010)) { - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/%s/tl_2010_us_county%s.zip", - cyear, suf) + year, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/COUNTY/tl_%s_us_county.zip", - cyear, cyear) + year, year) } @@ -237,13 +227,7 @@ counties <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL, tracts <- function(state = NULL, county = NULL, cb = FALSE, resolution = "500k", year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } + year <- set_tigris_year(year, min_year = 1990) if ((resolution == "5m" && year < 2022) | (resolution == "5m" && !is.null(state))) { stop("`resolution = '5m'` for Census tracts is only available for the national Census tract CB file in years 2022 and later.", call. = FALSE) @@ -258,16 +242,14 @@ tracts <- function(state = NULL, county = NULL, cb = FALSE, resolution = "500k", call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } if (cb == TRUE) { if (year %in% c(1990, 2000)) { - suf <- substr(as.character(year), 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/PREVGENZ/tr/tr%sshp/tr%s_d%s_shp.zip", suf, state, suf) @@ -282,12 +264,12 @@ tracts <- function(state = NULL, county = NULL, cb = FALSE, resolution = "500k", if (year > 2013) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_tract_%s.zip", - as.character(year), as.character(year), state, resolution) + year, year, state, resolution) } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/cb_%s_%s_tract_500k.zip", - as.character(year), as.character(year), state) + year, year, state) } @@ -300,17 +282,15 @@ tracts <- function(state = NULL, county = NULL, cb = FALSE, resolution = "500k", if (year %in% c(2000, 2010)) { - cyear <- as.character(year) - - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/TRACT/%s/tl_2010_%s_tract%s.zip", - cyear, state, suf) + year, state, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/TRACT/tl_%s_%s_tract.zip", - as.character(year), as.character(year), state) + year, year, state) } @@ -422,13 +402,7 @@ tracts <- function(state = NULL, county = NULL, cb = FALSE, resolution = "500k", school_districts <- function(state = NULL, type = 'unified', cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year = getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } + year <- set_tigris_year(year) if (is.null(state)) { if (year > 2018 && cb == TRUE) { @@ -439,20 +413,7 @@ school_districts <- function(state = NULL, type = 'unified', call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - + state <- validate_state(state, allow_null = FALSE) } if (type == 'unified') { @@ -465,17 +426,15 @@ school_districts <- function(state = NULL, type = 'unified', stop("Invalid school district type. Valid types are 'unified', 'elementary', and 'secondary'.", call. = FALSE) } - cyear <- as.character(year) - - if (cb) { + if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_%s_500k.zip", - cyear, cyear, state, type) + year, year, state, type) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/%s/tl_%s_%s_%s.zip", - cyear, toupper(type), cyear, state, type) + year, toupper(type), year, state, type) } @@ -530,15 +489,7 @@ school_districts <- function(state = NULL, type = 'unified', #' } block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year = getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year, min_year = 1990) if (is.null(state)) { if (year > 2018 && cb == TRUE) { @@ -549,16 +500,14 @@ block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, . call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } if (cb == TRUE) { if (year %in% c(1990, 2000)) { - suf <- substr(as.character(year), 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/PREVGENZ/bg/bg%sshp/bg%s_d%s_shp.zip", suf, state, suf) @@ -573,12 +522,12 @@ block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, . if (year > 2013) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_bg_500k.zip", - cyear, cyear, state) + year, year, state) } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/cb_%s_%s_bg_500k.zip", - cyear, cyear, state) + year, year, state) } @@ -591,15 +540,15 @@ block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, . if (year %in% c(2000, 2010)) { - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/BG/%s/tl_2010_%s_bg%s.zip", - cyear, state, suf) + year, state, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/BG/tl_%s_%s_bg.zip", - cyear, cyear, state) + year, year, state) } @@ -708,14 +657,7 @@ block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, . #' #' } zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ...) { - - if (is.null(year)) { - - year = getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } + year <- set_tigris_year(year, min_year = 1990) if (year > 2020 && cb) { stop(sprintf("The Census Bureau has not yet released the CB ZCTA file for %s. Please use the argument `year = 2020` or `cb = FALSE` instead.", year), call. = FALSE) @@ -734,7 +676,7 @@ zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ... call. = FALSE) } - if (!is.null(state)) state <- validate_state(state) + state <- validate_state(state) cache <- getOption("tigris_use_cache") @@ -742,9 +684,7 @@ zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ... message("ZCTAs can take several minutes to download. To cache the data and avoid re-downloading in future R sessions, set `options(tigris_use_cache = TRUE)`") } - cyear <- as.character(year) - - if (cb) { + if (cb == TRUE) { if (year == 2000) { if (is.null(state)) { @@ -758,38 +698,38 @@ zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ... url <- "https://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_860_00_500k.zip" } else if (year >= 2020) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_zcta520_500k.zip", - cyear, cyear) + year, year) } else if (year < 2020) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_zcta510_500k.zip", - cyear, cyear) + year, year) if (year == 2013) url <- gsub("shp/", "", url) } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_zcta520_500k.zip", - cyear, cyear) + year, year) } } else { if (year >= 2020) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/ZCTA520/tl_%s_us_zcta520.zip", - cyear, cyear) + year, year) } else { if (year %in% c(2000, 2010)) { - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) if (is.null(state)) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/%s/tl_2010_us_zcta5%s.zip", - cyear, suf) + year, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/%s/tl_2010_%s_zcta5%s.zip", - cyear, state, suf) + year, state, suf) } } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/ZCTA5/tl_%s_us_zcta510.zip", - cyear, cyear) + year, year) } } @@ -861,14 +801,7 @@ zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ... #' } blocks <- function(state, county = NULL, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - - } + year <- set_tigris_year(year, min_year = 2000) if (length(county) > 1 && year < 2011) { p <- lapply(county, function(x) { @@ -879,21 +812,8 @@ blocks <- function(state, county = NULL, year = NULL, ...) { return(p) } - if (year < 2000) { - - fname <- as.character(match.call())[[1]] - - msg <- "Block data are not available for 1990." - - stop(msg, call. = FALSE) - - } - - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) - cyear <- as.character(year) if (year >= 2014) { @@ -901,31 +821,31 @@ blocks <- function(state, county = NULL, year = NULL, ...) { # New block logic for 2020 url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/TABBLOCK20/tl_%s_%s_tabblock20.zip", - cyear, cyear, state) + year, year, state) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/TABBLOCK/tl_%s_%s_tabblock10.zip", - cyear, cyear, state) + year, year, state) } } else if (year %in% 2011:2013) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/TABBLOCK/tl_%s_%s_tabblock.zip", - cyear, cyear, state) + year, year, state) } else if (year %in% c(2000, 2010)) { - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) if (!is.null(county)) { county <- validate_county(state, county) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/%s/tl_2010_%s%s_tabblock%s.zip", - cyear, state, county, suf) + year, state, county, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/%s/tl_2010_%s_tabblock%s.zip", - cyear, state, suf) + year, state, suf) } } else { @@ -988,30 +908,9 @@ blocks <- function(state, county = NULL, year = NULL, ...) { #' } county_subdivisions <- function(state, county = NULL, cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - - } - - if (year < 2010) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2010.", fname) - - stop(msg, call. = FALSE) - - } - - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + year <- set_tigris_year(year, min_year = 2010) - cyear <- as.character(year) + state <- validate_state(state, allow_null = FALSE) if (cb == TRUE) { @@ -1021,7 +920,7 @@ county_subdivisions <- function(state, county = NULL, cb = FALSE, year = NULL, . } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_cousub_500k.zip", - cyear, cyear, state) + year, year, state) if (year == 2013) url <- gsub("shp/", "", url) @@ -1033,7 +932,7 @@ county_subdivisions <- function(state, county = NULL, cb = FALSE, year = NULL, . url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/COUSUB/2010/tl_2010_%s_cousub10.zip", state) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/COUSUB/tl_%s_%s_cousub.zip", - cyear, cyear, state) + year, year, state) } diff --git a/R/helpers.R b/R/helpers.R index b5dd29e..4e24bb3 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -408,9 +408,7 @@ geo_join <- function(spatial_data, data_frame, by_sp, by_df, by = NULL, how = 'l #' } lookup_code <- function(state, county = NULL) { - state <- validate_state(state, .msg=FALSE) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE, .msg = FALSE) if (!is.null(county)) { @@ -460,9 +458,7 @@ tigris_type <- function(obj) { #' @export list_counties <- function(state) { - state <- validate_state(state, .msg=FALSE) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE, .msg = FALSE) vals <- fips_codes[fips_codes$state_code == state, c("county", "county_code")] vals$county <- gsub("\ County$", "", vals$county) @@ -624,7 +620,6 @@ rbind_tigris <- function(...) { #' for a given location. #' @param year The year to use for the water layer; defaults to 2021 unless the #' `tigris_year` option is otherwise set. -#' @inheritParams counties #' @return An output sf object representing the polygons in `input_sf` with #' water areas erased. #' @export @@ -647,16 +642,13 @@ rbind_tigris <- function(...) { #' } erase_water <- function(input_sf, area_threshold = 0.75, - year = NULL, - cb = TRUE) { + year = NULL) { if (!is_sf(input_sf)) { stop("The input dataset is not an sf object.", call. = FALSE) } - if (is.null(year)) { - year <- getOption("tigris_year", 2021) - } + year <- set_tigris_year(year, quiet = TRUE) # Define st_erase function internally st_erase <- function(x, y) { @@ -664,7 +656,7 @@ erase_water <- function(input_sf, } # Grab a dataset of counties that overlap the input sf object quietly - county_overlay <- tigris::counties(cb = cb, resolution = "500k", progress_bar = FALSE, + county_overlay <- tigris::counties(cb = TRUE, resolution = "500k", progress_bar = FALSE, year = year, filter_by = input_sf) %>% sf::st_transform(sf::st_crs(input_sf)) diff --git a/R/landmarks.R b/R/landmarks.R index af9d75a..4c1b1fb 100644 --- a/R/landmarks.R +++ b/R/landmarks.R @@ -19,27 +19,10 @@ military <- function(year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/MIL/tl_%s_us_mil.zip", - as.character(year), as.character(year)) + year, year) return(load_tiger(url, tigris_type = "military", ...)) @@ -77,37 +60,18 @@ military <- function(year = NULL, ...) { #' @export landmarks <- function(state, type = "point", year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) state <- validate_state(state) - cyear <- as.character(year) - if (type == "area") { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/AREALM/tl_%s_%s_arealm.zip", - cyear, cyear, state) + year, year, state) return(load_tiger(url, tigris_type = "area_landmark", ...)) } else if (type == "point") { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/POINTLM/tl_%s_%s_pointlm.zip", - cyear, cyear, state) + year, year, state) return(load_tiger(url, tigris_type = "point_landmark", ...)) } else { diff --git a/R/legislative.R b/R/legislative.R index 4f2d3d9..b1abb9f 100644 --- a/R/legislative.R +++ b/R/legislative.R @@ -32,13 +32,7 @@ #' } congressional_districts <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } + year <- set_tigris_year(year, min_year = 2010) if (year < 2013 && cb == TRUE) { stop("`cb = TRUE` for congressional districts is unavailable prior to 2013. Regular TIGER/Line files are available for 2010 through 2010 with `cb = FALSE`", @@ -59,33 +53,19 @@ congressional_districts <- function(state = NULL, cb = FALSE, resolution = '500k congress <- "111" } - if (year < 2010) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2010.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - cyear <- as.character(year) + check_tigris_resolution(resolution) if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_cd%s_%s.zip", - cyear, cyear, congress, resolution) + year, year, congress, resolution) if (year == 2013) url <- gsub("shp/", "", url) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/CD/tl_%s_us_cd%s.zip", - cyear, cyear, congress) + year, year, congress) } @@ -138,25 +118,7 @@ congressional_districts <- function(state = NULL, cb = FALSE, resolution = '500k #' } state_legislative_districts <- function(state= NULL, house = "upper", cb = FALSE, year = NULL, ...) { - - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (is.null(state)) { if (year > 2018 && cb == TRUE) { @@ -167,9 +129,7 @@ state_legislative_districts <- function(state= NULL, house = "upper", call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } if (!house %in% c("upper", "lower")) @@ -189,9 +149,6 @@ state_legislative_districts <- function(state= NULL, house = "upper", } - cyear <- as.character(year) - - if (cb == TRUE) { if (year == 2010) { @@ -205,7 +162,7 @@ state_legislative_districts <- function(state= NULL, house = "upper", } url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_%s_500k.zip", - cyear, cyear, state, type) + year, year, state, type) if (year == 2013) url <- gsub("shp/", "", url) @@ -213,10 +170,10 @@ state_legislative_districts <- function(state= NULL, house = "upper", if (year %in% c(2000, 2010)) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/%s/%s/tl_2010_%s_%s%s.zip", - toupper(type), cyear, state, type, substr(cyear, 3, 4)) + toupper(type), year, state, type, substr(year, 3, 4)) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/%s/tl_%s_%s_%s.zip", - cyear, toupper(type), cyear, state, type) + year, toupper(type), year, state, type) } } @@ -283,12 +240,10 @@ voting_districts <- function(state = NULL, county = NULL, cb = FALSE, year = 202 call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } - if (cb) { + if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ2020/shp/cb_2020_%s_vtd_500k.zip", state) @@ -297,7 +252,7 @@ voting_districts <- function(state = NULL, county = NULL, cb = FALSE, year = 202 if (is.null(county)) { return(vtds) } else { - county = validate_county(state, county) + county <- validate_county(state, county) vtds_sub <- vtds[vtds$COUNTYFP20 == county,] return(vtds_sub) } diff --git a/R/metro_areas.R b/R/metro_areas.R index 3b75523..4de2bbf 100644 --- a/R/metro_areas.R +++ b/R/metro_areas.R @@ -20,52 +20,33 @@ #' @export core_based_statistical_areas <- function(cb = FALSE, resolution = '500k', year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2010) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2010. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - cyear <- as.character(year) + year <- set_tigris_year(year, min_year = 2010) if (cb == TRUE) { if (year == 2010) { - if (resolution == "5m") stop("Available resolutions are '500k' and '20m'", call. = FALSE) + check_tigris_resolution(resolution, values = c("500k", "20m")) + url <- sprintf("https://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_310_m1_%s.zip", resolution) } else { + check_tigris_resolution(resolution) + url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_cbsa_%s.zip", - cyear, cyear, resolution) + year, year, resolution) if (year == 2013) url <- gsub("shp/", "", url) } } else { + check_tigris_resolution(resolution) if (year == 2010) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/CBSA/2010/tl_2010_us_cbsa10.zip") } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/CBSA/tl_%s_us_cbsa.zip", - cyear, cyear) + year, year) } } @@ -91,51 +72,31 @@ core_based_statistical_areas <- function(cb = FALSE, resolution = '500k', year = #' @export urban_areas <- function(cb = FALSE, year = NULL, criteria = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) + year <- set_tigris_year(year) - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - cyear <- as.character(year) - - - if (cb) { + if (cb == TRUE) { if (!is.null(criteria)) { stop("The `criteria` argument is not supported for cartographic boundary files", call. = FALSE) } url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_ua10_500k.zip", - cyear, cyear) + year, year) if (year == 2013) url <- gsub("shp/", "", url) } else { - if (!is.null(criteria) && criteria == "2020") { + if (!is.null(criteria) && criteria == 2020) { if (year != 2020) { stop("2020 criteria is only supported when `year` is set to 2020 at the moment.", call. = FALSE) } url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/UAC/tl_%s_us_uac20.zip", - cyear, cyear) + year, year) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/UAC/tl_%s_us_uac10.zip", - cyear, cyear) + year, year) } @@ -163,40 +124,19 @@ urban_areas <- function(cb = FALSE, year = NULL, criteria = NULL, ...) { #' @export combined_statistical_areas <- function(cb = FALSE, resolution = '500k', year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } + year <- set_tigris_year(year) - cyear <- as.character(year) + check_resolution(resolution) if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_csa_%s.zip", - cyear, cyear, resolution) + year, year, resolution) if (year == 2013) url <- gsub("shp/", "", url) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/CSA/tl_%s_us_csa.zip", - cyear, cyear) + year, year) } return(load_tiger(url, tigris_type="csa", ...)) @@ -215,29 +155,10 @@ combined_statistical_areas <- function(cb = FALSE, resolution = '500k', year = N #' @export metro_divisions <- function(year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/METDIV/tl_%s_us_metdiv.zip", - cyear, cyear) + year, year) return(load_tiger(url, tigris_type="metro", ...)) @@ -276,38 +197,19 @@ metro_divisions <- function(year = NULL, ...) { #' } new_england <- function(type = 'necta', cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) if (type == 'necta') { if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_necta_500k.zip", - cyear, cyear) + year, year) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/NECTA/tl_%s_us_necta.zip", - cyear, cyear) + year, year) } @@ -316,14 +218,14 @@ new_england <- function(type = 'necta', cb = FALSE, year = NULL, ...) { } else if (type == 'combined') { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/CNECTA/tl_%s_us_cnecta.zip", - cyear, cyear) + year, year) return(load_tiger(url, tigris_type = "cnecta", ...)) } else if (type == 'divisions') { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/NECTADIV/tl_%s_us_nectadiv.zip", - cyear, cyear) + year, year) return(load_tiger(url, tigris_type = "nectadiv", ...)) diff --git a/R/national.R b/R/national.R index 03e967d..7fda3a1 100644 --- a/R/national.R +++ b/R/national.R @@ -19,33 +19,12 @@ #' @export regions <- function(resolution = '500k', year = NULL, ...) { - if (is.null(year)) { + year <- set_tigris_year(year) - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - cyear <- as.character(year) + check_tigris_resolution(resolution) url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_region_%s.zip", - cyear, cyear, resolution) + year, year, resolution) rgns <- load_tiger(url, tigris_type = "region", ...) @@ -74,33 +53,12 @@ regions <- function(resolution = '500k', year = NULL, ...) { #' } divisions <- function(resolution = '500k', year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] + year <- set_tigris_year(year) - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - cyear <- as.character(year) + check_tigris_resolution(resolution) url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_division_%s.zip", - cyear, cyear, resolution) + year, year, resolution) div <- load_tiger(url, tigris_type = "division", ...) @@ -128,33 +86,12 @@ divisions <- function(resolution = '500k', year = NULL, ...) { #' } nation <- function(resolution = '5m', year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (!(resolution %in% c('5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '5m', and '20m'.", call. = FALSE) - } + year <- set_tigris_year(year) - cyear <- as.character(year) + check_tigris_resolution(resolution, values = c('5m', '20m')) url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_nation_%s.zip", - cyear, cyear, resolution) + year, year, resolution) nat <- load_tiger(url, tigris_type = "nation", ...) diff --git a/R/native.R b/R/native.R index c282d21..e151692 100644 --- a/R/native.R +++ b/R/native.R @@ -29,37 +29,14 @@ #' } native_areas <- function(cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) if (cb == TRUE) { - url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_aiannh_500k.zip", - cyear, cyear) - + year, year) } else { - url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/AIANNH/tl_%s_us_aiannh.zip", - cyear, cyear) - + year, year) } return(load_tiger(url, tigris_type = "native areas", ...)) @@ -93,26 +70,9 @@ native_areas <- function(cb = FALSE, year = NULL, ...) { #' } tribal_subdivisions_national <- function(cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) + year <- set_tigris_year(year) - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (cb) { + if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_aitsn_500k.zip", year, year) } else { @@ -125,8 +85,6 @@ tribal_subdivisions_national <- function(cb = FALSE, year = NULL, ...) { } } - - return(load_tiger(url, tigris_type = "tribal subdivisions", ...)) } @@ -148,37 +106,14 @@ tribal_subdivisions_national <- function(cb = FALSE, year = NULL, ...) { #' @export alaska_native_regional_corporations <- function(cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) if (cb == TRUE) { - url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_02_anrc_500k.zip", - cyear, cyear) - + year, year) } else { - url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/ANRC/tl_%s_02_anrc.zip", - cyear, cyear) - + year, year) } return(load_tiger(url, tigris_type = "ANRCs", ...)) @@ -222,34 +157,16 @@ alaska_native_regional_corporations <- function(cb = FALSE, year = NULL, ...) { #' } tribal_block_groups <- function(cb = FALSE, year = NULL, ...) { - if (is.null(year)) { + year <- set_tigris_year(year) - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } - - if (cb) { - url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_tbg_500k.zip") + if (cb == TRUE) { + url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_tbg_500k.zip", + year, year) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/TBG/tl_%s_us_tbg.zip", year, year) } - - return(load_tiger(url, tigris_type = "tribal block groups", ...)) } @@ -286,26 +203,9 @@ tribal_block_groups <- function(cb = FALSE, year = NULL, ...) { #' } tribal_census_tracts <- function(cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) + year <- set_tigris_year(year) - stop(msg, call. = FALSE) - - } - - if (cb) { + if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_ttract_500k.zip", year, year) } else { @@ -313,8 +213,6 @@ tribal_census_tracts <- function(cb = FALSE, year = NULL, ...) { year, year) } - - return(load_tiger(url, tigris_type = "tribal tracts", ...)) } diff --git a/R/places.R b/R/places.R index 71634a0..05fffec 100644 --- a/R/places.R +++ b/R/places.R @@ -25,24 +25,7 @@ places <- function(state = NULL, cb = FALSE, year = NULL, ...) { return(p) } - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (is.null(state)) { if (year > 2018 && cb == TRUE) { @@ -53,19 +36,15 @@ places <- function(state = NULL, cb = FALSE, year = NULL, ...) { call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } - cyear <- as.character(year) - if (cb == TRUE) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_place_500k.zip", - cyear, cyear, state) + year, year, state) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/PLACE/tl_%s_%s_place.zip", - cyear, cyear, state) + year, year, state) } return(load_tiger(url, tigris_type="place", ...)) diff --git a/R/pumas.R b/R/pumas.R index 5f8f3a7..f71d46e 100644 --- a/R/pumas.R +++ b/R/pumas.R @@ -40,24 +40,7 @@ #' } pumas <- function(state = NULL, cb = FALSE, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (is.null(state)) { if (year == 2019 && cb == TRUE) { @@ -68,13 +51,9 @@ pumas <- function(state = NULL, cb = FALSE, year = NULL, ...) { call. = FALSE) } } else { - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) } - cyear <- as.character(year) - if (year > 2021) { suf <- "20" } else { @@ -88,7 +67,7 @@ pumas <- function(state = NULL, cb = FALSE, year = NULL, ...) { } url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_%s_puma10_500k.zip", - cyear, cyear, state) + year, year, state) if (year == 2013) url <- gsub("shp/", "", url) @@ -96,7 +75,7 @@ pumas <- function(state = NULL, cb = FALSE, year = NULL, ...) { } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/PUMA/tl_%s_%s_puma%s.zip", - cyear, cyear, state, suf) + year, year, state, suf) } pm <- load_tiger(url, tigris_type = "puma", ...) diff --git a/R/states.R b/R/states.R index b08d115..bf91104 100644 --- a/R/states.R +++ b/R/states.R @@ -30,26 +30,15 @@ #' } states <- function(cb = FALSE, resolution = '500k', year = NULL, ...) { - if (!(resolution %in% c('500k', '5m', '20m'))) { - stop("Invalid value for resolution. Valid values are '500k', '5m', and '20m'.", call. = FALSE) - } - - if (is.null(year)) { - - year = getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - cyear <- as.character(year) + check_tigris_resolution(resolution) + year <- set_tigris_year(year) if (cb == TRUE) { if (year %in% c(1990, 2000)) { - suf <- substr(as.character(year), 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/PREVGENZ/st/st%sshp/st99_d%s_shp.zip", suf, suf) @@ -64,12 +53,12 @@ states <- function(cb = FALSE, resolution = '500k', year = NULL, ...) { if (year > 2013) { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_state_%s.zip", - cyear, cyear, resolution) + year, year, resolution) } else { url <- sprintf("https://www2.census.gov/geo/tiger/GENZ%s/shp/cb_%s_us_state_%s.zip", - cyear, cyear, resolution) + year, year, resolution) } } @@ -80,15 +69,15 @@ states <- function(cb = FALSE, resolution = '500k', year = NULL, ...) { if (year %in% c(2000, 2010)) { - suf <- substr(cyear, 3, 4) + suf <- substr(year, 3, 4) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER2010/STATE/%s/tl_2010_us_state%s.zip", - cyear, suf) + year, suf) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/STATE/tl_%s_us_state.zip", - cyear, cyear) + year, year) } diff --git a/R/transportation.R b/R/transportation.R index 7a9fd5f..c84130d 100644 --- a/R/transportation.R +++ b/R/transportation.R @@ -42,22 +42,7 @@ #' } roads <- function(state, county, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (length(county) > 1) { r <- lapply(county, function(x) { @@ -70,14 +55,10 @@ roads <- function(state, county, year = NULL, ...) { state <- validate_state(state) - county <- validate_county(state, county) - - if (is.null(state)) stop("Invalid state", call.=FALSE) - - if (is.null(county)) stop("Invalid county", call. = FALSE) + county <- validate_county(state, county, allow_null = FALSE) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/ROADS/tl_%s_%s%s_roads.zip", - as.character(year), as.character(year), state, county) + year, year, state, county) return(load_tiger(url, tigris_type="road", ...)) @@ -107,27 +88,10 @@ roads <- function(state, county, year = NULL, ...) { #' } primary_roads <- function(year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/PRIMARYROADS/tl_%s_us_primaryroads.zip", - as.character(year), as.character(year)) + year, year) return(load_tiger(url, tigris_type="primary_roads", ...)) @@ -163,31 +127,12 @@ primary_roads <- function(year = NULL, ...) { #' } primary_secondary_roads <- function(state, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) + year <- set_tigris_year(year) - stop(msg, call. = FALSE) - - } - - state <- validate_state(state) - - if (is.null(state)) stop("Invalid state", call.=FALSE) + state <- validate_state(state, allow_null = FALSE) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/PRISECROADS/tl_%s_%s_prisecroads.zip", - as.character(year), as.character(year), state) + year, year, state) return(load_tiger(url, tigris_type="prim_sec_roads", ...)) @@ -213,27 +158,10 @@ primary_secondary_roads <- function(state, year = NULL, ...) { #' } rails <- function(year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/RAILS/tl_%s_us_rails.zip", - as.character(year), as.character(year)) + year, year) return(load_tiger(url, tigris_type="rails", ...)) @@ -254,35 +182,14 @@ rails <- function(year = NULL, ...) { #' @export address_ranges <- function(state, county, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) state <- validate_state(state) - county <- validate_county(state, county) - - if (is.null(state)) stop("Invalid state", call.=FALSE) - - if (is.null(county)) stop("Invalid county", call. = FALSE) + county <- validate_county(state, county, allow_null = FALSE) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/ADDRFEAT/tl_%s_%s%s_addrfeat.zip", - as.character(year), as.character(year), state, county) + year, year, state, county) return(load_tiger(url, tigris_type="address_range", ...)) diff --git a/R/utils.R b/R/utils.R index 7f96b2e..2adbba0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -3,9 +3,12 @@ # returns NULL if input is NULL # returns valid state FIPS code if input is even pseud-valid (i.e. single digit but w/in range) # returns NULL if input is not a valid FIPS code -validate_state <- function(state, .msg=interactive()) { +validate_state <- function(state, allow_null = TRUE, .msg = interactive()) { - if (is.null(state)) return(NULL) + if (is.null(state)) { + if (allow_null) return(NULL) + stop("Invalid state", call. = FALSE) + } state <- tolower(str_trim(state)) # forgive white space @@ -64,13 +67,19 @@ validate_state <- function(state, .msg=interactive()) { # Some work on a validate_county function # # -validate_county <- function(state, county, .msg = interactive()) { +validate_county <- function(state, county, allow_null = TRUE, .msg = interactive()) { - if (is.null(state)) return(NULL) + if (is.null(state)) { + if (allow_null) return(NULL) + stop("Invalid state", call. = FALSE) + } - if (is.null(county)) return(NULL) + if (is.null(county)) { + if (allow_null) return(NULL) + stop("Invalid county", call. = FALSE) + } - state <- validate_state(state) # Get the state of the county + state <- validate_state(state, allow_null = allow_null) # Get the state of the county county_table <- fips_codes[fips_codes$state_code == state, ] # Get a df for the requested state to work with @@ -177,3 +186,88 @@ input_to_wkt <- function(input) { return(wkt_input) } + +#' Set default year and validate year for tigris function +#' +#' [set_tigris_year()] returns year as a character string. +#' +#' @param year Year to use for download. +#' @param default Default year to use if "tigris_year" option is not set. +#' @param min_year Minimum year. Varies by geography and data source. +#' @param quiet If `TRUE`, do not display message about the year when +#' downloading data. +#' @inheritParams source +#' @noRd +set_tigris_year <- function(year = NULL, + default = 2021, + min_year = 2011, + max_year = 2022, + quiet = FALSE) { + if (is.null(year)) { + year <- getOption("tigris_year", default) + + if (!quiet) { + message(sprintf("Retrieving data for the year %s", year)) + } + } + + year <- as.integer(year) + + check_tigris_year(year, min_year = min_year, max_year = max_year) + + year +} + +#' Check if year is valid +#' +#' @inheritParams base::match.call +#' @noRd +check_tigris_year <- function(year, + min_year = 2011, + max_year = 2021, + call = sys.call(sys.parent(2L))) { + if (length(year) > 1 || length(year) == 0 || nchar(year) != 4) { + stop( + "`year` must be a an integer or string with a single year.", + call. = FALSE + ) + } + + if ((year >= min_year) && year <= max_year) { + return(invisible(NULL)) + } + + msg <- "`%s` is not currently available for years prior to %s." + limit_year <- min_year + + if (year > max_year) { + msg <- "`%s` is not currently available for years after %s." + limit_year <- max_year + } + + fname <- as.character(match.call(call = call))[[1]] + + msg <- sprintf( + paste0( + msg, "\n", + "To request this feature, file an issue at https://github.com/walkerke/tigris/issues" + ), fname, limit_year + ) + + stop(msg, call. = FALSE) +} + + +#' Check if resolution is valid +#' +#' @noRd +check_tigris_resolution <- function(resolution, + values = c("500k", "5m", "20m")) { + if ((length(resolution) == 1) && (resolution %in% values)) { + return(invisible(NULL)) + } + + msg <- paste0("Invalid value for resolution. Valid values are ", format_vec(values)) + + stop(msg, call. = FALSE) +} diff --git a/R/water.R b/R/water.R index e8cc5fd..107fa83 100644 --- a/R/water.R +++ b/R/water.R @@ -25,24 +25,7 @@ #' } area_water <- function(state, county, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (length(county) > 1) { w <- lapply(county, function(x) { @@ -55,16 +38,10 @@ area_water <- function(state, county, year = NULL, ...) { state <- validate_state(state) - county <- validate_county(state, county) - - if (is.null(state)) stop("Invalid state", call.=FALSE) - - if (is.null(county) | length(county) > 1) stop("Invalid county", call. = FALSE) - - cyear <- as.character(year) + county <- validate_county(state, county, allow_null = FALSE) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/AREAWATER/tl_%s_%s%s_areawater.zip", - cyear, cyear, state, county) + year, year, state, county) return(load_tiger(url, tigris_type="area_water", ...)) @@ -99,24 +76,7 @@ area_water <- function(state, county, year = NULL, ...) { #' } linear_water <- function(state, county, year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - if (year < 2011) { - - fname <- as.character(match.call())[[1]] - - msg <- sprintf("%s is not currently available for years prior to 2011. To request this feature, - file an issue at https://github.com/walkerke/tigris.", fname) - - stop(msg, call. = FALSE) - - } + year <- set_tigris_year(year) if (length(county) > 1) { w <- lapply(county, function(x) { @@ -129,16 +89,10 @@ linear_water <- function(state, county, year = NULL, ...) { state <- validate_state(state) - county <- validate_county(state, county) - - if (is.null(state)) stop("Invalid state", call.=FALSE) - - if (is.null(county)) stop("Invalid county", call. = FALSE) - - cyear <- as.character(year) + county <- validate_county(state, county, allow_null = FALSE) url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/LINEARWATER/tl_%s_%s%s_linearwater.zip", - cyear, cyear, state, county) + year, year, state, county) return(load_tiger(url, tigris_type="linear_water", ...)) @@ -152,26 +106,16 @@ linear_water <- function(state, county, year = NULL, ...) { #' @family water functions coastline <- function(year = NULL, ...) { - if (is.null(year)) { - - year <- getOption("tigris_year", 2021) - - message(sprintf("Retrieving data for the year %s", year)) - - } - - cyear <- as.character(year) + year <- set_tigris_year(year) if (year > 2016) { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/COASTLINE/tl_%s_us_coastline.zip", - cyear, cyear) + year, year) } else { url <- sprintf("https://www2.census.gov/geo/tiger/TIGER%s/COAST/tl_%s_us_coastline.zip", - cyear, cyear) + year, year) } - - return(load_tiger(url, tigris_type="coastline", ...)) } diff --git a/man/erase_water.Rd b/man/erase_water.Rd index 91da342..e4f2352 100644 --- a/man/erase_water.Rd +++ b/man/erase_water.Rd @@ -4,7 +4,7 @@ \alias{erase_water} \title{Erase water area from an input polygon dataset} \usage{ -erase_water(input_sf, area_threshold = 0.75, year = NULL, cb = TRUE) +erase_water(input_sf, area_threshold = 0.75, year = NULL) } \arguments{ \item{input_sf}{An input sf object, ideally obtained with the tigris package @@ -18,9 +18,6 @@ for a given location.} \item{year}{The year to use for the water layer; defaults to 2021 unless the \code{tigris_year} option is otherwise set.} - -\item{cb}{If cb is set to TRUE, download a generalized (1:500k) -counties file. Defaults to FALSE (the most detailed TIGER file).} } \value{ An output sf object representing the polygons in \code{input_sf} with