Skip to contents

Get Counties boundaries via API, to map them

Usage

shapes_counties_from_countyfips(
  countyfips = "10001",
  outFields = c("NAME", "FIPS", "STATE_ABBR", "STATE_NAME"),
  myservice =
    c(paste0("https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/",
    "USA_Boundaries_2022/FeatureServer/2/query"),
    paste0("https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/",
    "USA_Counties_and_States_with_PR/FeatureServer/0/query"), "cartographic", "tiger")[3],
  use_local = TRUE
)

Arguments

countyfips

FIPS codes as 5-character strings (or numbers) in a vector as from fips_counties_from_state_abbrev("DE")

outFields

can be "*" for all, or can be just some variables like SQMI, POPULATION_2020, etc., or none

myservice

URL of feature service to get shapes from or "cartographic" or "tiger" to use approx or slow/accurate bounds from tidycensus and tigris packages. Note that "cartographic" (the default) is normally served from the counties_shapefile dataset built into this package, without downloading - see use_local.

use_local

if TRUE (the default) and myservice is "cartographic", County boundaries come from the counties_shapefile dataset built into this package instead of being downloaded, which avoids a network round-trip (and avoids needing a CENSUS_API_KEY) for every County map or report. Downloading is still used as a fallback for any FIPS not found in that dataset. Set FALSE to force downloading. Ignored when myservice is "tiger" or a feature-service URL, since those request different boundaries.

Value

spatial object via sf::st_read()

Details

Used sf::read_sf(), which is an alias for sf::st_read() but with some modified default arguments. read_sf is quiet by default/ does not print info about data source, and read_sf returns an sf-tibble rather than an sf-data.frame

But note the tidycensus and tigris R packages can more quickly get county boundaries for mapping.