-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_md_tigris.R
99 lines (88 loc) 路 3.9 KB
/
get_md_tigris.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#' Use package data or the {tigris} package to get Maryland data from the U.S. Census Bureau
#'
#' Get package data with Maryland counties, U.S. Congressional District, or
#' Census designated places by name or GeoID or use the tigris package to
#' download state data.
#'
#' @details Use {mapmaryland} packaged data or use tigris U.S. Census Bureau
#' API:
#'
#' Supported options that access data included with the {mapmaryland} package
#' include "counties", "census places", "congressional districts",
#' "legislative districts", and "water".
#'
#' Options that access data by calling one of several tigris functions for
#' downloading data from the U.S. Census Bureau API include "senate district",
#' "tracts", "block groups", "blocks", "pumas", "voting districts", "zctas",
#' "roads", "primary secondary roads", "area water", "linear water", and
#' "landmarks".
#'
#' tigris functions that do not use a "state" parameter (e.g.
#' [tigris::coastline] or [tigris::rails]) are not supported by this
#' function.
#'
#' @param name Name matching a value in the namelsad, namelsad, or geoid columns
#' of [md_counties], [md_census_places], [md_congressional_districts], or one
#' of the data sets available for download with {tigris}; Default: `NULL`
#' @param type Type of data to return, Default: "counties"; See details for
#' supported options.
#' @inheritParams format_md_sf
#' @param ... Additional parameters passed on to {tigris} functions.
#' @return A simple feature object matching the type provided.
#' @rdname get_md_tigris
#' @export
#' @importFrom tigris state_legislative_districts county_subdivisions block_groups blocks pumas voting_districts roads primary_secondary_roads area_water linear_water landmarks zctas
#' @importFrom janitor clean_names
get_md_tigris <- function(name = NULL, type = "counties", crs = getOption("mapmaryland.crs", default = 3857), erase_water = FALSE, ...) {
pkg_data <-
c("counties", "census places", "congressional districts", "water")
api_data <-
c(
"legislative districts", "senate district", "voting districts",
"tracts", "block groups", "blocks", "pumas", "zctas",
"roads", "primary secondary roads", "area water", "linear water", "landmarks"
)
type <-
match.arg(
tolower(type),
c("county", pkg_data, api_data)
)
state <- "MD"
data <-
switch(type,
"county" = md_counties,
"counties" = md_counties,
"census places" = md_census_places,
"congressional districts" = md_congressional_districts,
"water" = md_water,
"legislative districts" = tigris::state_legislative_districts(state = state, house = "lower", ...),
"senate districts" = tigris::state_legislative_districts(state = state, house = "upper", ...),
"county subdivisions" = tigris::county_subdivisions(state = state, ...),
"block groups" = tigris::block_groups(state = state, ...),
"blocks" = tigris::blocks(state = state, ...),
"pumas" = tigris::pumas(state = state, ...),
"voting districts" = tigris::voting_districts(state = state, ...),
"roads" = tigris::roads(state = state, ...),
"primary secondary roads" = tigris::primary_secondary_roads(state = state, ...),
"area water" = tigris::area_water(state = state, ...),
"linear water" = tigris::linear_water(state = state, ...),
"landmarks" = tigris::landmarks(state = state, ...),
"zctas" = tigris::zctas(state = state, ...)
)
if (type %in% api_data) {
data <- janitor::clean_names(data)
}
if (!is.null(name)) {
data <- data[lookup_tigris_name(name, data), ]
}
data <- format_md_sf(data, crs = crs, erase_water = erase_water, clean_names = FALSE)
return(data)
}
#' @noRd
lookup_tigris_name <- function(name, data = NULL) {
name <-
tolower(name)
lookup <-
((tolower(data[["name"]]) %in% name) | (tolower(data[["namelsad"]]) %in% name) | (data[["geoid"]] %in% name))
return(lookup)
}