terrainr makes it easy to identify your area of interest from point data, retrieve geospatial data (including orthoimagery and DEMs) 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 rendering engine.

At the absolute simplest level, terrainr provides a convenient and consistent API to downloading data from the National Map.

simulated_data <-  data.frame(id = seq(1, 100, 1),
                              lat = runif(100, 44.04905, 44.17609), 
                              lng = runif(100, -74.01188, -73.83493))

bbox <- get_coord_bbox(lat = simulated_data$lat, lng = simulated_data$lng) 
output_tiles <- get_tiles(bbox = bbox,
                          services = c("elevation", "ortho"))
# output_tiles is now a list of two vectors pointing to the elevation and 
# orthoimagery tiles we just downloaded -- here we're displaying the first
# of the ortho tiles

Once downloaded, these images are in standard GeoTIFF or PNG formats and can be used as expected with other utilities:


Additionally, terrainr provides functions to transform these tiles into RAW images ready to be imported into the Unity rendering engine, allowing you to fly or walk through your downloaded data sets in 3D or VR:

merged_dem <- tempfile(fileext = ".tif")
merged_ortho <- tempfile(fileext = ".tif")
# we can call these vectors by name instead of position, too

mapply(function(x, y) raster_to_raw_tiles(input_file = x, 
                                          output_prefix = tempfile(), 
                                          side_length = 4097, 
                                          raw = y),
       c(merged_dem, merged_ortho),
       c(TRUE, FALSE))

# With about ten minutes of movie magic (loading the files into Unity), 
# we can turn that into:

terrainr also includes functionality to merge and crop the files you’ve downloaded, and to resize your area of interest so you’re sure to download exactly the area you want. Additionally, 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 hung. For more information, check out the introductory vignette and the guide to importing your data into Unity.

Available Datasets

The following datasets can currently be downloaded using get_tiles or hit_national_map_api:

  • 3DEPElevation: The USGS 3D Elevation Program (3DEP) Bare Earth DEM.
  • USGSNAIPPlus: National Agriculture Imagery Program (NAIP) and high resolution orthoimagery (HRO).
  • nhd: A comprehensive set of digital spatial data that encodes information about naturally occurring and constructed bodies of surface water (lakes, ponds, and reservoirs), paths through which water flows (canals, ditches, streams, and rivers), and related entities such as point features (springs, wells, stream gauges, and dams).
  • govunits: Major civil areas for the Nation, including States or Territories, counties (or equivalents), Federal and Native American areas, congressional districts, minor civil divisions, incorporated places (such as cities and towns), and unincorporated places.
  • contours: The USGS Elevation Contours service.
  • geonames: Information about physical and cultural geographic features, geographic areas, and locational entities that are generally recognizable and locatable by name.
  • NHDPlus_HR: A comprehensive set of digital spatial data comprising a nationally seamless network of stream reaches, elevation-based catchment areas, flow surfaces, and value-added attributes.
  • structures: The name, function, location, and other core information and characteristics of selected manmade facilities.
  • transportation: Roads, railroads, trails, airports, and other features associated with the transport of people or commerce.
  • wbd: Hydrologic Unit (HU) polygon boundaries for the United States, Puerto Rico, and the U.S. Virgin Islands.

(All descriptions above taken from the National Map API descriptions.)


You can install the development version of terrainr from GitHub with:

# install.packages("devtools")

Code of Conduct

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.