longLatRasts {enmSdmX}R Documentation

Generate rasters with cell values equal to cell longitude or latitude

Description

This function generates a raster stack with two rasters, one with cell values equal to the cell's longitude and the other with cell values equal to the cell's latitude.

Usage

longLatRasts(x, m = TRUE, filePath = NULL, ...)

Arguments

x

SpatRaster object. The output will have the same resolution, extent, and coordinate projection system as x.

m

Any of:

  • TRUE (default): Calculate longitude and latitude only for cells that are not NA.

  • FALSE: Calculate longitude and latitude for all cells.

  • A SpatRaster object: Force any cells that are NA in this raster to also be NA in the output.

filePath

String or NULL. If a string, then this is the path (not including file name) to which to write the raster stack with longitude/latitude rasters. If NULL then no file is written.

...

Arguments to pass to writeRaster (if filePath is not NULL).

Value

Object of class SpatRaster.

Examples


library(terra)

# generate long/lat rasters for the world
x <- rast() # raster with 1 deg resolution and extent equal to entire world
x[] <- 1:ncell(x)
longLat <- longLatRasts(x)
plot(longLat)

# demonstrate masking
# randomly force some cells to NA
v <- 1:ncell(x)
n <- 10000
v[sample(v, n)] <- NA
x[] <- v
longLatTRUE <- longLatRasts(x, m = TRUE)
longLatFALSE <- longLatRasts(x, m = FALSE)
rasts <- c(x, longLatTRUE, x, longLatFALSE)
names(rasts) <- c('x', 'long_m_TRUE', 'lat_m_TRUE',
	'x', 'long_m_FALSE', 'lat_m_FALSE')
plot(rasts)


[Package enmSdmX version 1.1.2 Index]