Skip to content

Commit

Permalink
Merge pull request #150 from walkerke/v2
Browse files Browse the repository at this point in the history
merge tigris v2.0 into master
  • Loading branch information
walkerke authored Oct 12, 2022
2 parents ddf97d2 + 61feb9f commit f39e6b9
Show file tree
Hide file tree
Showing 55 changed files with 153 additions and 229 deletions.
11 changes: 4 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: tigris
Type: Package
Title: Load Census TIGER/Line Shapefiles
Version: 1.6.1
Date: 2022-06-03
Version: 2.0
Date: 2022-10-11
Authors@R: c(
person(given="Kyle", family="Walker", email="kyle@walker-data.com", role=c("aut", "cre")),
person(given="Bob", family="Rudis", email="bob@rudis.net", role="ctb")
Expand All @@ -23,16 +23,13 @@ Suggests:
ggthemes,
leaflet,
knitr,
rgeos,
tidycensus
tidycensus,
sp
Imports:
stringr,
magrittr,
rgdal,
sp,
utils,
rappdirs,
maptools,
httr,
uuid,
sf,
Expand Down
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ export(voting_districts)
export(zctas)
import(dplyr)
import(httr)
import(maptools)
import(rappdirs)
import(rgdal)
import(sf)
import(sp)
import(utils)
import(uuid)
importFrom(dplyr,mutate)
Expand Down
14 changes: 7 additions & 7 deletions R/enumeration_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ counties <- function(state = NULL, cb = FALSE, resolution = '500k', year = NULL,

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -236,7 +236,7 @@ tracts <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -417,7 +417,7 @@ school_districts <- function(state = NULL, type = 'unified',

if (is.null(year)) {

year = getOption("tigris_year", 2020)
year = getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -525,7 +525,7 @@ block_groups <- function(state = NULL, county = NULL, cb = FALSE, year = NULL, .

if (is.null(year)) {

year = getOption("tigris_year", 2020)
year = getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -704,7 +704,7 @@ zctas <- function(cb = FALSE, starts_with = NULL, year = NULL, state = NULL, ...

if (is.null(year)) {

year = getOption("tigris_year", 2020)
year = getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -849,7 +849,7 @@ blocks <- function(state, county = NULL, year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -976,7 +976,7 @@ county_subdivisions <- function(state, county = NULL, cb = FALSE, year = NULL, .

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down
106 changes: 21 additions & 85 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,12 @@ load_tiger <- function(url,

}

if (class == "sp") {

obj <- readOGR(dsn = cache_dir, layer = shape, encoding = "UTF-8",
verbose = FALSE, stringsAsFactors = FALSE)

if (is.na(proj4string(obj))) {

proj4string(obj) <- CRS("+proj=longlat +datum=NAD83 +no_defs")

}

} else if (class == "sf") {

obj <- st_read(dsn = cache_dir, layer = shape,
quiet = TRUE, stringsAsFactors = FALSE)

if (is.na(st_crs(obj)$proj4string)) {
obj <- st_read(dsn = cache_dir, layer = shape,
quiet = TRUE, stringsAsFactors = FALSE)

st_crs(obj) <- "+proj=longlat +datum=NAD83 +no_defs"
if (is.na(st_crs(obj)$proj4string)) {

}
st_crs(obj) <- "+proj=longlat +datum=NAD83 +no_defs"

}

Expand All @@ -211,28 +196,12 @@ load_tiger <- function(url,
shape <- gsub(".zip", "", tiger_file)
shape <- gsub("_shp", "", shape) # for historic tracts

obj <- st_read(dsn = tmp, layer = shape,
quiet = TRUE, stringsAsFactors = FALSE)

if (class == "sp") {

obj <- readOGR(dsn = tmp, layer = shape, encoding = "UTF-8",
verbose = FALSE, stringsAsFactors = FALSE)

if (is.na(proj4string(obj))) {

proj4string(obj) <- CRS("+proj=longlat +datum=NAD83 +no_defs")

}

} else if (class == "sf") {
if (is.na(st_crs(obj)$proj4string)) {

obj <- st_read(dsn = tmp, layer = shape,
quiet = TRUE, stringsAsFactors = FALSE)

if (is.na(st_crs(obj)$proj4string)) {

st_crs(obj) <- "+proj=longlat +datum=NAD83 +no_defs"

}
st_crs(obj) <- "+proj=longlat +datum=NAD83 +no_defs"

}

Expand Down Expand Up @@ -261,24 +230,25 @@ load_tiger <- function(url,
obj$STATEFP <- obj$ST
}

return(obj)

if (class == "sp") {
warning(stringr::str_wrap("Spatial* (sp) classes are no longer formally supported in tigris as of version 2.0. We strongly recommend updating your workflow to use sf objects (the default in tigris) instead.", 50), call. = FALSE)
return(sf::as_Spatial(obj))
} else {
return(obj)
}
}

#' Easily merge a data frame to a spatial data frame
#'
#' The pages of StackOverflow are littered with questions about how to merge a regular data frame to a
#' spatial data frame in R. The \code{merge} function from the sp package operates under a strict set of
#' assumptions, which if violated will break your data. This function wraps a couple StackOverflow answers
#' I've seen that work in a friendlier syntax.
#' This function should be considered deprecated. Please update your workflow to use sf objects and dplyr's \code{*_join()} family of functions instead.
#'
#' @param spatial_data A spatial data frame to which you want to merge data.
#' @param data_frame A regular data frame that you want to merge to your spatial data.
#' @param by_sp The column name you'll use for the merge from your spatial data frame.
#' @param by_df The column name you'll use for the merge from your regular data frame.
#' @param by (optional) If a named argument is supplied to the by parameter, geo_join will assume that the join columns in the spatial data and data frame share the same name.
#' @param how The type of join you'd like to perform. The default, 'left', keeps all rows in the spatial data frame, and returns NA for unmatched rows. The alternative, 'inner', retains only those rows in the spatial data frame that match rows from the target data frame.
#' @return a \code{SpatialXxxDataFrame} object
#' @return a joined spatial/data frame object
#' @export
#' @examples \dontrun{
#'
Expand Down Expand Up @@ -503,20 +473,13 @@ list_counties <- function(state) {
#' @return one combined Spatial object
#' @export
#' @examples \dontrun{
#' library(sp)
#' library(rgeos)
#' library(maptools)
#' library(maps)
#' library(tigris)
#'
#' me_ctys <- list_counties("me")
#' aw <- lapply(me_ctys$county_code[1:3], function(x) {
#' area_water("Maine", x)
#' })
#' tmp <- rbind_tigris(aw)
#' tmp_simp <- gSimplify(tmp, tol=1/200, topologyPreserve=TRUE)
#' tmp_simp <- SpatialPolygonsDataFrame(tmp_simp, tmp@@data)
#' plot(tmp_simp)
#' }) %>%
#' rbind_tigris()
#' }

rbind_tigris <- function(...) {
Expand Down Expand Up @@ -567,34 +530,7 @@ rbind_tigris <- function(...) {
"SpatialPixelsDataFrame", "SpatialPointsDataFrame",
"SpatialPolygonsDataFrame")) {

if (length(obj_classes) == 1 &
!any(sapply(obj_attrs, is.null)) &
length(obj_attrs_u)==1) {

el_nams <- names(elements)

if (is.null(el_nams)) {
el_nams <- rep(NA, length(elements))
}

el_nams <- ifelse(el_nams == "", NA, el_nams)

el_nams <- sapply(el_nams, function(x) {
ifelse(is.na(x), gsub("-", "", UUIDgenerate(), fixed=TRUE), x)
})

tmp <- lapply(1:(length(elements)), function(i) {
elem <- elements[[i]]
spChFIDs(elem, sprintf("%s%s", el_nams[i], rownames(elem@data)))
})

tmp <- Reduce(spRbind, tmp)
attr(tmp, "tigris") <- obj_attrs_u
return(tmp)

} else {
stop("Objects must all be Spatial*DataFrame objects and all the same type of tigris object.", call.=FALSE)
}
stop("Spatial* classes are no longer supported in tigris as of version 2.0. You will need to install an earlier version of tigris with `remotes::install_version()`.")

} else if ("sf" %in% obj_classes) {

Expand Down Expand Up @@ -761,11 +697,11 @@ erase_water <- function(input_sf,
#' `@inheritSection load_tiger_doc_template Additional Arguments`
#'
#' @param ... arguments to be passed to internal function `load_tiger`, which is not exported. See Additional Arguments.
#' @param year the data year; defaults to 2020
#' @param year the data year; defaults to 2021
#'
#' @section Additional Arguments:
#' Additional arguments that can be passed in `...` are:
#' * `class` Desired class of return object: `"sf"` (the default) or `"sp"`.
#' * `class` Desired class of return object: `"sf"` (the default) or `"sp"`. sp classes should be considered deprecated as of tigris version 2.0, but legacy support is still available.
#' * `progress_bar` If set to `FALSE`, do not display download progress bar (helpful for R Markdown documents). Defaults to `TRUE`.
#' * `keep_zipped_shapefile` If set to `TRUE`, do not delete zipped shapefile (stored in temporary directory or `TIGRIS_CACHE_DIR`
#' depending on the configuration of global option `"tigris_use_cache"`). Defaults to `FALSE`.
Expand Down
4 changes: 2 additions & 2 deletions R/landmarks.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ military <- function(year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -79,7 +79,7 @@ landmarks <- function(state, type = "point", year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down
6 changes: 3 additions & 3 deletions R/legislative.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ congressional_districts <- function(state = NULL, cb = FALSE, resolution = '500k

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand All @@ -45,7 +45,7 @@ congressional_districts <- function(state = NULL, cb = FALSE, resolution = '500k
call. = FALSE)
}

if (year %in% 2018:2021) {
if (year %in% 2018:2022) {
congress <- "116"
} else if (year %in% 2016:2017) {
congress <- "115"
Expand Down Expand Up @@ -141,7 +141,7 @@ state_legislative_districts <- function(state= NULL, house = "upper",

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down
10 changes: 5 additions & 5 deletions R/metro_areas.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ core_based_statistical_areas <- function(cb = FALSE, resolution = '500k', year =

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -92,7 +92,7 @@ urban_areas <- function(cb = FALSE, year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -147,7 +147,7 @@ combined_statistical_areas <- function(cb = FALSE, resolution = '500k', year = N

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -199,7 +199,7 @@ metro_divisions <- function(year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -260,7 +260,7 @@ new_england <- function(type = 'necta', cb = FALSE, year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down
6 changes: 3 additions & 3 deletions R/national.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ regions <- function(resolution = '500k', year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -76,7 +76,7 @@ divisions <- function(resolution = '500k', year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down Expand Up @@ -130,7 +130,7 @@ nation <- function(resolution = '5m', year = NULL, ...) {

if (is.null(year)) {

year <- getOption("tigris_year", 2020)
year <- getOption("tigris_year", 2021)

message(sprintf("Retrieving data for the year %s", year))

Expand Down
Loading

0 comments on commit f39e6b9

Please sign in to comment.