Data set details

Data set description: Elevation data in Switzerland
Source: USGS Elevation Point Query Service and Amazon Web Services Terrain Tiles
Details on the retrieved data: Elevation in Switzerland along with the data on the administrative areas for the same country.
Spatial and temporal resolution: Elevation data with different zoom levels (see details)

Downloading elevation data with the elevatr package

This tutorial gives you a brief understanding of how to use the elevatr package for a standardized access to the elevation data from the web. The packages uses two endpoints to access its data from:

Installing the package

The elevtr package can be directly downloaded from CRAN as follows.

install.packages("elevatr")

Loading the elevatr package:

library(elevatr)

Functions available

Currently, there are two functions in this package which help users access elevation web services, namely, get_elev_point() and get_elev_raster().

The get_elev_point() function gets the point elevations using the USGS Elevation Point Query Services (for United states only) and AWS Terrain Tiles (for all global elevation data).

  • Input: This function accepts a data frame of longitude and latitude values (x and y), a SpatialPoints/SpatialPointsDataFrame, or a simple feature object (sf). It has a source argument src which indicates which API to use, either "eqps" or "aws".
  • Output: produces either a SpatialPointsDataFrame or Simple Feature object, depending on the class of input locations.

The get_elev_point() function can be used as follows:

ll_proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
elev <- get_elev_point(pt_df, prj = ll_proj)

library(kableExtra)
elev %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover"))
elevation elev_units coords.x1 coords.x2
NA meters -71.82791 40.25963
243.79 meters -71.76843 43.05586
73.74 meters -71.37858 42.52338
86.83 meters -71.10417 42.65332
391.93 meters -71.56750 43.91346

The get_elev_raster() function helps users get elevation data as a raster from the AWS Open Data Terrain Tiles. The source data are global and also contain the estimations for depth for oceans.

  • Input: This takes in a data frame of longitude and latitude values (x and y) or any sp or raster object. It has a z argument to determine the zoom or resolution of the raster (1 to 14). It also has a clip argument to determine clipping of returned DEM. Options are "tile" which is the default value and returns the full tiles, "bbox" which returns the DEM clipped to the bounding box of the original locations, or "locations" if the spatial data in the input locations should be used to clip the DEM.

  • Output: Returns a raster object of the elevation tiles that cover the bounding box of the input spatial data.

We can use the get_elev_raster() function to obtain the elevation data of Switzerland, and plot it together with the country boundaries obtained by using the rgeoboundaries package as follows.

# remotes::install_github("wmgeolab/rgeoboundaries")
# install.packages(c("ggplot2", "raster", "elevatr", "sf", "viridis"))
library(ggplot2)
library(raster)
library(elevatr)
library(rgeoboundaries)
library(sf)
library(viridis)

swiss_bound <- geoboundaries("Switzerland")
elevation_data <- get_elev_raster(locations = swiss_bound, z = 9, clip = "locations")

elevation_data <- as.data.frame(elevation_data, xy = TRUE)
colnames(elevation_data)[3] <- "elevation"
# remove rows of data frame with one or more NA's,using complete.cases
elevation_data <- elevation_data[complete.cases(elevation_data), ]

ggplot() +
  geom_raster(data = elevation_data, aes(x = x, y = y, fill = elevation)) +
  geom_sf(data = swiss_bound, color = "white", fill = NA) +
  coord_sf() +
  scale_fill_viridis_c() +
  labs(title = "Elevation in Switzerland", x = "Longitude", y = "Latitude", fill = "Elevation (meters)")

We can also get the different administrative areas of the country Switzerland, by using the ne_states function from the rnaturalearth package. And the get_elev_raster function, you can get the elevation data. The following code does the same:

NOTE: increasing or decreasing the z argument will make the map zoom in and out on the country by setting the appropriate z-axis value.

# install.packages("rnaturlaearth")
# remotes::install_github("ropensci/rnaturalearthhires")

library(rnaturalearth)
library(rnaturalearthhires)

sf_swiss <- ne_states(country = "switzerland", returnclass = "sf")

elevation_1 <- elevatr::get_elev_raster(locations = sf_swiss, z = 7, clip = "locations")
cropped_elev <- crop(elevation_1, sf_swiss)
elevate <- as.data.frame(cropped_elev, xy = TRUE)

colnames(elevate)[3] <- "elevation_value"
elevate <- elevate[complete.cases(elevate), ]

ggplot() +
  # geom_sf(data = st_as_sfc(st_bbox(elevation_1)),color = "grey", fill = "grey",alpha = 0.05) +
  geom_raster(data = elevate, aes(x = x, y = y, fill = elevation_value)) +
  geom_sf(data = sf_swiss, color = "white", fill = NA) +
  coord_sf(xlim = c(5.3, 10.8), ylim = c(45.5, 47.8)) +
  scale_fill_viridis_c() +
  labs(title = "Elevation in Switzerland", x = "Longitude", y = "Latitude", fill = "Elevation (meters)")

References


Last updated: 2021-11-30
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/elevation.Rmd

Tutorial was complied using: (click to expand)
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] rnaturalearthhires_0.2.0  rnaturalearth_0.1.0      
##  [3] viridis_0.6.2             viridisLite_0.4.0        
##  [5] sf_1.0-3                  rgeoboundaries_0.0.0.9000
##  [7] raster_3.5-2              sp_1.4-5                 
##  [9] elevatr_0.4.1             kableExtra_1.3.4         
## [11] forcats_0.5.1             stringr_1.4.0            
## [13] dplyr_1.0.7               purrr_0.3.4              
## [15] readr_2.0.2               tidyr_1.1.4              
## [17] tibble_3.1.5              ggplot2_3.3.5            
## [19] tidyverse_1.3.1          
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.0           lubridate_1.8.0    webshot_0.5.2      progress_1.2.2    
##  [5] httr_1.4.2         tools_4.1.1        backports_1.3.0    bslib_0.3.1       
##  [9] utf8_1.2.2         rgdal_1.5-27       R6_2.5.1           KernSmooth_2.23-20
## [13] rgeos_0.5-8        DBI_1.1.1          colorspace_2.0-2   withr_2.4.2       
## [17] gridExtra_2.3      tidyselect_1.1.1   prettyunits_1.1.1  curl_4.3.2        
## [21] compiler_4.1.1     progressr_0.9.0    cli_3.1.0          rvest_1.0.2       
## [25] xml2_1.3.2         labeling_0.4.2     triebeard_0.3.0    sass_0.4.0        
## [29] scales_1.1.1       classInt_0.4-3     proxy_0.4-26       rappdirs_0.3.3    
## [33] systemfonts_1.0.3  digest_0.6.28      rmarkdown_2.11     svglite_2.0.0     
## [37] pkgconfig_2.0.3    htmltools_0.5.2    dbplyr_2.1.1       fastmap_1.1.0     
## [41] highr_0.9          rlang_0.4.11       readxl_1.3.1       rstudioapi_0.13   
## [45] httpcode_0.3.0     farver_2.1.0       jquerylib_0.1.4    generics_0.1.1    
## [49] jsonlite_1.7.2     magrittr_2.0.1     s2_1.0.7           Rcpp_1.0.7        
## [53] munsell_0.5.0      fansi_0.5.0        lifecycle_1.0.1    terra_1.4-11      
## [57] stringi_1.7.5      yaml_2.2.1         grid_4.1.1         crayon_1.4.2      
## [61] lattice_0.20-44    haven_2.4.3        hms_1.1.1          knitr_1.36        
## [65] pillar_1.6.4       codetools_0.2-18   wk_0.5.0           crul_1.1.0        
## [69] reprex_2.0.1       glue_1.5.0         evaluate_0.14      hoardr_0.5.2      
## [73] modelr_0.1.8       urltools_1.7.3     vctrs_0.3.8        tzdb_0.1.2        
## [77] cellranger_1.1.0   gtable_0.3.0       assertthat_0.2.1   cachem_1.0.6      
## [81] xfun_0.26          broom_0.7.9        countrycode_1.3.0  e1071_1.7-9       
## [85] class_7.3-19       memoise_2.0.0      units_0.7-2        ellipsis_0.3.2

Corrections: If you see mistakes or want to suggest changes, please create an issue on the source repository or submit a pull request
Contributions: If you want to contribute or collaborate on the project, please see the guidelines for collaborating
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.