elevationus
: An R Package to Retrieve Elevation Data for U.S. Geographies
The new elevationus package retrieves elevation data for U.S. geographies such as states, counties and tracts. It returns:
Gridded elevation data at your choice of spatial resolution.
The mean elevation for the entire geography.
The elevation at the center of population (the population-weighted centroid) for a given geography.
I wanted to solve a few related problems with this package.
First, I needed to quickly and easily retrieve elevation for specific geographies in the United States. Given a state, county or tract FIPS code, I wanted a function that would quickly return elevation data in various formats for that geography.
Second, most of my work is at the intersection of environment and human behavior. For that reason, I often need the typical elevation at the place in a geography where most people live, not just the mean elevation or the elevation at the geographic center. This is a crucial distinction. Case in point: I live in a county with a mountain. But most residents live in the nearby valley. The elevation at the center of population -- the place where most people live -- is significantly lower than the mean elevation for my entire county.
Finally, I needed a package that could handle batch requests; e.g., give me the elevation at the population centroid for all counties in a state.
To address these challenges, I've developed elevationus, an R package designed to retrieve and analyze elevation data for U.S. geographies, including states, counties, and tracts. Built upon the tigris and elevatr packages, elevationus offers a streamlined workflow for accessing gridded elevation data and integrating it with statistical geographies. The package automates spatial joins, simplifying the process for users.
Retrieves Elevation Data: Obtain gridded raster elevation data for specified U.S. geographies.
Calculates Mean Elevation: Compute the average elevation across a given geographic area.
Determines Population-Weighted Elevation: Find the elevation at the population-weighted centroid, offering insights into where residents are predominantly located.
The core functions of elevationus include:
get_elevation_data()
: Takes a state, county, or tract FIPS code and returns a map, a gridded raster of elevation data, raw elevation data, and an estimate of mean elevation across the geography.
get_elevation_data_batch()
: Accepts a state FIPS code and returns elevation data for counties or tracts within that state, including both the mean elevation for each substate geography and the elevation at the center of population.
By automating the spatial join processes, elevationus ensures that users can focus on analysis rather than data wrangling.
You can install elevationus from Github.
install.packages("devtools")
library(devtools)
install_github("marsha5813/elevationus")
library(elevationus)
elev <- get_elevation_data(level = "state", geoid = "41")
print(paste("Mean elevation:",elev$elevation_mean,"meters"))
elev$map
[1] "Mean elevation: 1073 meters"
This time, we'll increase the resolution to 5 arc seconds with the z = 10
argument.
elev <- get_elevation_data(level = "county", geoid = "24021", z = 10)
print(paste("Mean elevation:",elev$elevation_mean,"meters"))
elev$map
"Mean elevation: 183 meters"
The 'z' argument ranges from 0 to 16 and is passed to elevatr::get_elev_raster().
You can check the tilezen documentation for what the zoom levels mean. Or reference this handy table:
zoom | meters at equator | arc seconds | nominal arc degrees minutes seconds | source | nominal ground units |
---|---|---|---|---|---|
0 | 156543.0 | 5071.0 |
1.5 arc degrees | ||
1 | 78271.5 | 2535.5 |
40 arc minutes | ||
2 | 39135.8 | 1267.8 |
20 arc minutes | ||
3 | 19567.9 | 633.9 |
10 arc minutes | ||
4 | 9783.9 | 316.9 |
5 arc minutes | ||
5 | 4892.0 | 158.5 |
2.5 arc minutes | ||
6 | 2446.0 | 79.2 |
1 arc minutes | ETOPO1 |
2.5 km |
7 | 1223.0 | 39.6 |
30 arc seconds | GMTED2010 |
1km (not used) |
8 | 611.5 | 19.8 |
15 arc seconds | GMTED2010 |
500m (not used) |
9 | 305.7 | 9.9 |
7.5 arc seconds | GMTED2010 |
250m |
10 | 152.9 | 5.0 |
5 arc seconds | ||
11 | 76.4 | 2.5 |
3 arc seconds | Canada | 90m |
12 | 38.2 | 1.2 |
1 arc seconds | SRTM , Canada |
30m |
13 | 19.1 | 0.6 |
2/3 arc seconds | ||
14 | 9.6 | 0.3 |
1/3 arc seconds | 3DEP , Austria, Australia, New Zealand, Norway |
10m |
15 | 4.8 | 0.2 |
1/5 arc seconds | Mexico, Arctic | |
16 | 2.4 | 0.1 |
1/9 arc seconds | 3DEP , United Kingdom |
3m |
elev <- get_elevation_data(level = "tract", geoid = "24510200100", z = 10)
print(paste("Mean elevation:",elev$elevation_mean,"meters"))
elev$map
"Mean elevation: 51 meters"
Notice how pixelated this is! Let's dial up the spatial resolution. Note the z = 14
argument, which corresponds to 1/3 arc seconds.
elev <- get_elevation_data(level = "tract", geoid = "24510200100", z = 14)
elev$map
This uses the get_elevation_batch()
function
get_elevation_data_batch(level = "county", state = "24")
GEOID | NAMELSAD | STATE_NAME | elevation_popcenter | elevation_mean | |
---|---|---|---|---|---|
1 | 24001 | Allegany County | Maryland | 505 | 382 |
2 | 24003 | Anne Arundel County | Maryland | 30 | 29 |
3 | 24005 | Baltimore County | Maryland | 138 | 130 |
4 | 24009 | Calvert County | Maryland | 45 | 27 |
5 | 24011 | Caroline County | Maryland | 13 | 15 |
... |
If you wanted all the tracts in a single state, you'd switch the level argument to level = "tract"
.
What if you want to grab data for every county or tract in the U.S.? It's possible! It just takes a while to download.
# We'll be using purrr to iterate over states install.packages("purrr") library(purrr) # Get vector of state fips codes stfips <- read.csv("https://gist.githubusercontent.com/marsha5813/14036adfbb6094f7fa3b25ee48299786/raw/75c53d82fc8998dc3f8dbd98f31e29fcbe50f1a3/stfips", colClasses = "character") # Pull data for each state and return as a single dataframe el <- map_dfr(stfips$fips, ~get_elevation_data_batch(level = "tract", state = .x))
elevationus
bridges the gap between elevation data and population distribution, providing researchers and analysts with tools to make more informed decisions. By considering the elevation at population-weighted centroids, we can gain a clearer understanding of how elevation impacts communities, leading to more accurate assessments in fields ranging from climate science to urban planning.
For more information and to access the package, visit the elevationus GitHub repository.