Introduction to elevatr

Elevation data is used for a wide array of applications, including, for example, visualization, hydrology, and ecological modelling. Gaining access to these data in R has not had a single interface, is made available through functions across many packages, or requires local access to the data. This is no longer required as a variety of APIs now exist that provide programmatic access to elevation data. The elevatr package was written to standarize access to elevation data from web APIs. This introductory vignette provides details on how to use elevatr to access elevation data and provides a bit of detail on the source data it accesses.

There are currently two endpoints that elevatr accesses. For point elevation data it uses USGS Elevation Point Query Service and to access raster elevation data (e.g. a DEM) it uses the Amazon Web Services Terrain Tiles.

Get Point Elevation Data

Point elevation is accessed from get_elev_point(). This function takes either a data.frame with x (longitude) and y (latitude) locations as the first two columns or a SpatialPoints/SpatialPointsDataFrame as input and then fetches the reported elevation for that location. As mentioned there is one service that provides this information. Details are provided below.

USGS Elevation Point Query Service

The USGS Elevation Point Query Service is accessible from elevatr. It is only available for the United States (including Alaska and Hawaii). Points that fall within the United States but are not on land return a value of zero. Points outside the United States boundaries return a value of -1000000.

Using get_elev_point() to Access The USGS Elevation Point Query Service

Usage of get_elev_point() requires an input SpatialPoints, SpatialPointsDataFrame, or a two-column data frame with column one containing the x (e.g. longitude) coordinates and the second column containing the y coordinates (e.g. latitude). The source data are global and also include estimates of depth for oceans.

Example usage of each is included below. For these examples, we can create a dataset to use.

# Create an example data.frame
set.seed(65.7)
examp_df <- data.frame(x = runif(10, min = -73, max = -71), y = runif(10, min = 41, 
    max = 45))
prj_dd <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

# Create and example data.frame with additional columns
cats <- data.frame(category = c("H", "H", "L", "L", "L", "M", "H", "L", "M", 
    "M"))

examp_df2 <- data.frame(examp_df, cats)

# Create an example SpatialPoints
examp_sp <- SpatialPoints(examp_df, proj4string = CRS(prj_dd))

# Create an example SpatialPointsDataFrame
examp_spdf <- SpatialPointsDataFrame(examp_sp, proj4string = CRS(prj_dd), data = cats)

If a data frame is used it may have additional columns beyond the first two, which must contain the coordinates. The additional columns, along with the returned elevation, will be part of the output SpatialPointsDataFrame. Similarly, an elevation column is added to the data slot of a SpatialPointsDataFrame.

The USGS Elevation Point Query Service returns a single point at a time. The implemntation in get_elev_point() will loop through each point, thus can be slow for large number of requests.

Accessing data from this service is done by setting the src to "epqs". No API key is required and there are no rate limits.

df_elev_epqs <- get_elev_point(examp_df, prj = prj_dd, src = "epqs")
## Note: Elevation units are in &units=Meters 
## Note:. The coordinate reference system is:
##  +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data.frame(df_elev_epqs)
##    elevation elev_units         x        y optional
## 1     581.26     meters -72.76862 44.57522     TRUE
## 2      79.47     meters -71.62394 41.71462     TRUE
## 3     100.78     meters -72.65873 42.38664     TRUE
## 4     312.80     meters -72.91463 44.74525     TRUE
## 5      16.03     meters -71.78052 41.42377     TRUE
## 6     106.22     meters -72.46585 42.34894     TRUE
## 7     183.05     meters -72.85153 42.02864     TRUE
## 8     133.55     meters -71.95443 41.83915     TRUE
## 9       0.00     meters -71.41128 41.48341     TRUE
## 10    139.90     meters -71.26831 43.12234     TRUE
df2_elev_epqs <- get_elev_point(examp_df2, prj = prj_dd, src = "epqs")
## Note: Elevation units are in &units=Meters 
## Note:. The coordinate reference system is:
##  +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data.frame(df2_elev_epqs)
##    category elevation elev_units         x        y optional
## 1         H    581.26     meters -72.76862 44.57522     TRUE
## 2         H     79.47     meters -71.62394 41.71462     TRUE
## 3         L    100.78     meters -72.65873 42.38664     TRUE
## 4         L    312.80     meters -72.91463 44.74525     TRUE
## 5         L     16.03     meters -71.78052 41.42377     TRUE
## 6         M    106.22     meters -72.46585 42.34894     TRUE
## 7         H    183.05     meters -72.85153 42.02864     TRUE
## 8         L    133.55     meters -71.95443 41.83915     TRUE
## 9         M      0.00     meters -71.41128 41.48341     TRUE
## 10        M    139.90     meters -71.26831 43.12234     TRUE
sp_elev_epqs <- get_elev_point(examp_sp, src = "epqs")
## Note: Elevation units are in &units=Meters 
## Note:. The coordinate reference system is:
##  +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
sp_elev_epqs
## class       : SpatialPointsDataFrame 
## features    : 10 
## extent      : -72.91463, -71.26831, 41.42377, 44.74525  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 
## variables   : 2
## names       : elevation, elev_units 
## min values  :      0.00,     meters 
## max values  :    581.26,     meters
spdf_elev_epqs <- get_elev_point(examp_spdf, src = "epqs")
## Note: Elevation units are in &units=Meters 
## Note:. The coordinate reference system is:
##  +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
spdf_elev_epqs
## class       : SpatialPointsDataFrame 
## features    : 10 
## extent      : -72.91463, -71.26831, 41.42377, 44.74525  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 
## variables   : 3
## names       : category, elevation, elev_units 
## min values  :        H,      0.00,     meters 
## max values  :        M,    581.26,     meters

Get Raster Elevation Data

While point elevations are useful, they will not provide the information required for most elevation based analysis such as hydrologic modeling, viewsheds, etc. To do that requires a raster digital elevation model (DEM). There are several sources for digital elevation models such as the Shuttle Radar Topography Mission (SRTM), the USGS National Elevation Dataset (NED), Global DEM (GDEM), and others. Each of these DEMs has pros and cons for their use. Prior to its closure in January of 2018, Mapzen combined several of these sources to create a synthesis elevation product that utilizes the best available elevation data for a given region at given zoom level. Additionally, the elevation data are enhanced with the inclusion of bathymetry in oceans from ETOPO1. Although closed, these data compiled by Mapzen are made available through two separate APIs: the Nextzen Terrain Tile Service and the Terrain Tiles on Amazon Web Services. Only the Amazon tiles are currently accessible via elevatr.

The input for get_elev_raster() is a data.frame with x (longitude) and y (latitude) locations as the first two columns, any sp object, or any raster object and it returns a RasterLayer of the tiles that overlap the bounding box of the input. If multiple tiles are retrieved, the resultant output is a merged Raster Layer. Details for each service and their usage via get_elev_raster() are provided below.

Using get_elev_raster() to access the Terrain Tiles on AWS.

As mentioned a data frame with x and y columns, a sp object, or a raster object needs be the input and the src needs to be set to “mapzen” (this is the default).

There is no difference in using the sp and raster input data types. The data frame requires a prj. We show examples using a SpatialPolygonsDataFrame and a data frame. The zoom level (z) defaults to 9 (a trade off between resolution and time for download), but different zoom levels are often desired. For example:

# SpatialPolygonsDataFrame example
data(lake)
elevation <- get_elev_raster(lake, z = 9)
## Merging DEMs
## Reprojecting DEM to original projection
## Note: Elevation units are in meters.
## Note: The coordinate reference system is:
##  +proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
plot(elevation)
plot(lake, add = TRUE)