terrainr makes it easy to retrieve elevation and base map image tiles for areas of interest within the United States from the National Map family of APIs, and then process that data into larger, joined images or crop it into tiles that can be imported into the Unity 3D rendering engine.
There are three main utilities provided by terrainr. First, users are able to download data from the National Map via the
get_tiles function, downloading data tiles for the area represented by an
library(terrainr) library(sf) # Optional way to display a progress bar while your tiles download: library(progressr) handlers("progress") handlers(global = TRUE) location_of_interest <- tmaptools::geocode_OSM("Hyampom California")$coords location_of_interest <- data.frame( x = location_of_interest[["x"]], y = location_of_interest[["y"]] ) location_of_interest <- st_as_sf( location_of_interest, coords = c("x", "y"), crs = 4326 ) location_of_interest <- set_bbox_side_length(location_of_interest, 8000) output_tiles <- get_tiles(location_of_interest, services = c("elevation", "ortho"), resolution = 30 # pixel side length in meters )
Once downloaded, these images are in standard GeoTIFF or PNG formats and can be used as expected with other utilities:
Finally, terrainr helps you visualize this data, both natively in R via the new
library(ggplot2) ggplot() + geom_spatial_rgb(data = output_tiles[["ortho"]], aes(x = x, y = y, r = red, g = green, b = blue)) + coord_sf(crs = 4326) + theme_void()
As well as with the Unity 3D rendering engine, allowing you to fly or walk through your downloaded data sets in 3D and VR:
with_progress( # When not specifying resolution, default is 1m pixels output_tiles <- get_tiles(location_of_interest, services = c("elevation", "ortho")) ) merged_dem <- merge_rasters(output_tiles[["elevation"]], tempfile(fileext = ".tif")) merged_ortho <- merge_rasters(output_tiles[["ortho"]], tempfile(fileext = ".tif")) make_manifest(output_tiles$elevation, output_tiles$ortho)
We can then import these tiles to Unity (following the Import Vignette) to create:
The more time intensive processing steps can all be monitored via the progressr package, so you’ll be more confident that your computer is still churning along and not just stalled out. For more information, check out the introductory vignette and the guide to importing your data into Unity!
The following datasets can currently be downloaded using
(All descriptions above taken from the National Map API descriptions.)
You can install terrainr from CRAN via:
Or, if you want the newest patches and features, you can install the development version of terrainr from GitHub with:
# install.packages("devtools") devtools::install_github("ropensci/terrainr")
Be aware that the development version is not stable, and features that haven’t been published on CRAN may change at any time!
Please note that this package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.