Skip to contents

Download shapefiles based on FIPS codes of States, Counties, Cities/CDPs, Tracts, or Blockgroups

Usage

shapes_from_fips(
  fips,
  myservice_blockgroup =
    "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Boundaries_2022/FeatureServer/5/query",
  myservice_tract =
    "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Boundaries_2022/FeatureServer/4/query",
  myservice_place = "tiger",
  myservice_county = "cartographic"
)

Arguments

fips

vector of one or more Census FIPS codes such as from name2fips()

myservice_blockgroup

URL of feature service to get shapes from, or "cartographic" or "tiger" to use approx or slow/accurate bounds from tidycensus and tigris packages.

myservice_tract

URL of feature service to get shapes from, or "cartographic" or "tiger" to use approx or slow/accurate bounds from tidycensus and tigris packages.

myservice_place

only "tiger" is implemented

myservice_county

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 State bounds are built into this package as data so do not need to be downloaded from a service.

Value

spatial data.frame with one row per fips (assuming any fips are valid)

Details

when using tigris package ("tiger" as service-related parameter here), it uses the year that is the default in the version of the tigris package that is installed. You can use options(tigris_year = 2022) for example to specify it explicitly. This function also sets options(tigris_use_cache = TRUE), but each individual shapes_xyz_from_ function may not specify.

Examples

 # shp2 = shapes_from_fips("10001", "10005")

 fipslist = list(
  statefips = name2fips(c('DE', 'RI')),
  countyfips = fips_counties_from_state_abbrev(c('DE')),
  cityfips = name2fips(c('chelsea,MA', 'st. john the baptist parish, LA')),
  tractfips = substr(blockgroupstats$bgfips[300:301], 1, 12),
  bgfips = blockgroupstats$bgfips[300:301]
  )
#>   query     fullname fips fipstype
#> 1    DE     Delaware   10    state
#> 2    RI Rhode Island   44    state
#> 
#> 
#> 
#> Exact match, or Cities/CDPs showing any multiple possible matches, etc. (excluding if no match):
#> 
#>   eparegion ST stfips     countyname countyfips    placename    fips      query
#> 1         1 MA     25 Suffolk County      25025 Chelsea city 2513205 chelsea,MA
#>   multiple
#> 1         
#> 
#> 
#>                             query                        fullname    fips
#> 1                      chelsea,MA                Chelsea city, MA 2513205
#> 2 st. john the baptist parish, LA St. John the Baptist Parish, LA   22095
#>   fipstype
#> 1     city
#> 2   county
#> 
#> 
  shp <- list()
  # \donttest{
   for (i in seq_along(fipslist)) {
    shp[[i]] <- shapes_from_fips(fipslist[[i]])
    print(shp[[i]])
    # mapfast(shp[[i]])
   }
#> Simple feature collection with 2 features and 15 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -75.78902 ymin: 38.45113 xmax: -71.08857 ymax: 42.0188
#> Geodetic CRS:  NAD83
#>    REGION DIVISION STATEFP  STATENS GEOID STUSPS         NAME LSAD MTFCC
#> 12      3        5      10 01779781    10     DE     Delaware   00 G4000
#> 6       1        1      44 01219835    44     RI Rhode Island   00 G4000
#>    FUNCSTAT      ALAND     AWATER    INTPTLAT     INTPTLON
#> 12        A 5046703789 1399207462 +38.9985661 -075.4416440
#> 6         A 2677763373 1323686975 +41.5964850 -071.5264901
#>                          geometry FIPS
#> 12 MULTIPOLYGON (((-75.50949 3...   10
#> 6  MULTIPOLYGON (((-71.67881 4...   44
#> Warning: need tidycensus package and census API key to use myservice = 'cartographic', so using default service instead
#> Reading layer `OGRGeoJSON' from data source 
#>   `https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Boundaries_2022/FeatureServer/2/query?where=FIPS%3D%2710001%27%20OR%20FIPS%3D%2710003%27%20OR%20FIPS%3D%2710005%27&outFields=NAME%2CFIPS%2CSTATE_ABBR%2CSTATE_NAME%2CPOP_SQMI&returnGeometry=true&f=geojson' 
#>   using driver `GeoJSON'
#> Simple feature collection with 3 features and 5 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -75.78902 ymin: 38.45118 xmax: -75.04871 ymax: 39.83952
#> Geodetic CRS:  WGS 84
#> Simple feature collection with 3 features and 5 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -75.78902 ymin: 38.45118 xmax: -75.04871 ymax: 39.83952
#> Geodetic CRS:  WGS 84
#>                NAME  FIPS STATE_ABBR STATE_NAME POP_SQMI
#> 1       Kent County 10001         DE   Delaware    314.8
#> 2 New Castle County 10003         DE   Delaware   1342.2
#> 3     Sussex County 10005         DE   Delaware    263.5
#>                         geometry
#> 1 MULTIPOLYGON (((-75.31239 3...
#> 2 MULTIPOLYGON (((-75.53853 3...
#> 3 MULTIPOLYGON (((-75.07994 3...
#> Error in shapes_from_fips(fipslist[[i]]): This dataset contains more than one type of FIPS code. Analysis can only be run on datasets with one type of FIPS codes.
  # }