getMap: Download or crop necessary DEMs

View source: R/getMap.R

getMapR Documentation

Download or crop necessary DEMs

Description

A function that checks if the DEMs for a given set of sectors exist in the workspace, and if not downloads them or crops them from a larger file

Usage

getMap(
  tiles,
  polys,
  tile_id = "TILEID",
  vals = "location",
  z_min = NULL,
  filt = 0,
  verbose = FALSE,
  dir = tempdir()
)

Arguments

tiles

A character vector–such as the output to whichTiles—containing the unique tile IDs for sectors that should be in the workspace.

polys

A polygon of class SpatVector representing the partitioning grid for the maximum possible area, in the same format as the output of the makeGrid function.

tile_id

a character string representing the name of the column in the polys polygon containing the unique Tile IDs. Default is tile_id = 'TILEID'

vals

A character string or a SpatRast or Raster* object. Optional if the polys polygon is the output of the makeGrid function as the default is the character string 'location'. If no DEM was provided when makeGrid was initially run (i.e. polys$location == NA), then the function will use get_elev_raster to download data. If not from makeGrid, the vals parameter should be set to the column name containing the URL or filepath to the DEM for that sector.

z_min

The minimum allowable elevation. Useful if DEM source includes ocean bathymetry as does the SRTM data from AWS. Default is z_min = NULL, but set to 0 for SRTM data.

filt

Numeric. Size of moving window to apply a low-pass filter. Default is filt = 0.

verbose

Should the number of remaining sectors be printed? Default is FALSE

dir

A filepath to the directory being used as the workspace. Default is tempdir() but unless the analyses will only be performed a few times it is highly recommended to define a permanent workspace.

Value

Function does not return any objects, but sets up the workspace such that the necessary DEM files are downloaded/cropped and accessible.

Examples

# Generate a DEM, export it
n <- 5
dem <- expand.grid(list(x = 1:(n * 100),
                        y = 1:(n * 100))) / 100
dem <- as.data.table(dem)
dem[, z := 250 * exp(-(x - n/2)^2) + 
      250 * exp(-(y - n/2)^2)]
dem <- rast(dem)
ext(dem) <- c(10000, 20000, 30000, 40000)
crs(dem) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"

dir <- tempdir()
writeRaster(dem, paste0(dir,"/DEM.tif"),overwrite=TRUE)


# Import raster, get the grid
dem <- rast(paste0(dir,"/DEM.tif"))
grid <- makeGrid(dem = dem, nx = n, ny = n, sources = TRUE)


# Generate five random points that fall within the grid
points <- data.table(x = runif(5, ext(dem)[1], ext(dem)[2]),
                     y = runif(5, ext(dem)[3], ext(dem)[4]))
               
                           
# Run whichTiles and getMap to prepare appropriate sector files
tile_list <- whichTiles(region = points, polys = grid) 
getMap(tiles = tile_list, polys = grid, dir = dir)

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.