makeGrid: Make partitioning grid

View source: R/makeGrid.R

makeGridR Documentation

Make partitioning grid

Description

Generate a partitioning grid for a single raster source representing regional elevations. Smaller partitioning grids (i.e. a greater value of nx * ny) results in a greater number of saved files and a greater number of read-write operations in future operations, but reduces the amount of memory employed.

Usage

makeGrid(
  dem,
  nx,
  ny,
  path = NA,
  sources = FALSE,
  extension = NULL,
  proj = NULL,
  prefix = "SECTOR_",
  crop = TRUE,
  zoom = 13,
  var = "z",
  overlap = 0.005
)

Arguments

dem

One of either a single character string, SpatVector (polygon), SpatialPolygons*, SpatRaster, or Raster

If character, SpatRaster, Raster, such an object or (filepath to one) containing the elevations for the maximum possible extent imaginable for a study. Note that SpatRaster and Raster only work rasters that have been read in, not those that exist exclusively in the memory. If you have just generated the raster and it is in memory, export it first with writeRaster then use the filepath string as dem or re-import it with rast before using the SoatRaster object.

If SpatVector or SpatialPolygons*, the extent of possible movement.

nx

The integer-number of columns in the output grid

ny

The integer-number of rows in the output grid

path

(Optional) The filepath or URL of the source DEM. Ignored if dem is of class raster or SpatRaster. If a SpatVector or SpatialPolygons is provided but no path, getMap will use get_elev_raster to download topographic data.

sources

Logical. Should source information be saved as attributes to the grid for use in getMap and defineWorld? Default is sources = FALSE.

extension

A character vector representing the extension of the source path. Required only if sources = TRUE and the extension is not apparent from the URL stored in the var column.

proj

A crs or something coercible to it representing the desired output projection. Default is the input raster's projection.

prefix

A character string containing the prefix to name individual sectors. Default is prefix = "SECTOR_"

crop

Logical. If TRUE (the default), the output polygons will be cropped by the original dem (if SpatVector or SpatialPolygons*), or by the area covered by non-NA cells (if raster or SpatRaster).

zoom

Considered only if var = 'z' and no data source is set. The zoom level to be downloaded. See documentation for the z parameter in get_elev_raster for further information. Default is 13, but see documentation for the z parameter in get_elev_raster.

var

If the polygons point to a data source, what will be the variable name in the internal GIS? Default is 'z' for elevation.

overlap

How much should adjacent polygons overlap to ensure there's contiguity between different tiles? Default is overlap = 0.005.

Value

Polygons of class SpatVector representing the individual sectors ('tiles'), with a dataframe containing three columns: the "TILEID", the raster's filepath, and a dummy column indicating that the grid was made using the makeGrid function. This will be necessary for future functions. The object MUST be stored on the disk, it should not be stored in the memory

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)


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