| postProcess {reproducible} | R Documentation |
Generic function to post process objects
Description
The method for GIS objects (terra Spat* & sf classes) will
crop, reproject, and mask, in that order.
This is a wrapper for cropTo(), fixErrorsIn(),
projectTo(), maskTo() and writeTo(),
with a required amount of data manipulation between these calls so that the crs match.
Usage
postProcess(x, ...)
## S3 method for class 'list'
postProcess(x, ...)
## Default S3 method:
postProcess(x, ...)
Arguments
x |
A GIS object of postProcessing,
e.g., Spat* or sf*. This can be provided as a
|
... |
Additional arguments passed to methods. For |
Value
A GIS file (e.g., RasterLayer, SpatRaster etc.) that has been
appropriately cropped, reprojected, masked, depending on the inputs.
Post processing sequence
If the rasterToMatch or studyArea are passed, then
the following sequence will occur:
Fix errors
fixErrorsIn(). Currently only errors fixed are forSpatialPolygonsusingbuffer(..., width = 0).Crop using
cropTo()Project using
projectTo()Mask using
maskTo()Determine file name
determineFilename()Write that file name to disk, optionally
writeTo()
NOTE: checksumming does not occur during the post-processing stage, as
there are no file downloads. To achieve fast results, wrap
prepInputs with Cache
Backwards compatibility with rasterToMatch and/or studyArea arguments
For backwards compatibility, postProcess will continue to allow passing
rasterToMatch and/or studyArea arguments. Depending on which of these
are passed, different things will happen to the targetFile located at filename1.
See Use cases section in postProcessTo() for post processing behaviour with
the new from and to arguments.
If targetFile is a raster (Raster*, or SpatRaster) object:
rasterToMatch | studyArea | Both | |
extent | Yes | Yes | rasterToMatch |
resolution | Yes | No | rasterToMatch |
projection | Yes | No* | rasterToMatch* |
alignment | Yes | No | rasterToMatch |
mask | No** | Yes | studyArea** |
*Can be overridden with useSAcrs.
**Will mask with NAs from rasterToMatch if maskWithRTM.
If targetFile is a vector (Spatial*, sf or SpatVector) object:
rasterToMatch | studyArea | Both | |
extent | Yes | Yes | rasterToMatch |
resolution | NA | NA | NA |
projection | Yes | No* | rasterToMatch* |
alignment | NA | NA | NA |
mask | No | Yes | studyArea |
*Can be overridden with useSAcrs
See Also
prepInputs
Examples
if (requireNamespace("terra", quietly = TRUE) && requireNamespace("sf", quietly = TRUE)) {
library(reproducible)
od <- setwd(tempdir2())
# download a (spatial) file from remote url (which often is an archive) load into R
# need 3 files for this example; 1 from remote, 2 local
dPath <- file.path(tempdir2())
remoteTifUrl <- "https://github.com/rspatial/terra/raw/master/inst/ex/elev.tif"
localFileLuxSm <- system.file("ex/luxSmall.shp", package = "reproducible")
localFileLux <- system.file("ex/lux.shp", package = "terra")
# 1 step for each layer
# 1st step -- get study area
studyArea <- prepInputs(localFileLuxSm, fun = "terra::vect") # default is sf::st_read
# 2nd step: make the input data layer like the studyArea map
# Test only relevant if connected to internet -- so using try just in case
elevForStudy <- try(prepInputs(url = remoteTifUrl, to = studyArea, res = 250,
destinationPath = dPath))
# Alternate way, one step at a time. Must know each of these steps, and perform for each layer
dir.create(dPath, recursive = TRUE, showWarnings = FALSE)
file.copy(localFileLuxSm, file.path(dPath, basename(localFileLuxSm)))
studyArea2 <- terra::vect(localFileLuxSm)
if (!all(terra::is.valid(studyArea2))) studyArea2 <- terra::makeValid(studyArea2)
tf <- tempfile(fileext = ".tif")
download.file(url = remoteTifUrl, destfile = tf, mode = "wb")
Checksums(dPath, write = TRUE, files = tf)
elevOrig <- terra::rast(tf)
elevForStudy2 <- terra::project(elevOrig, terra::crs(studyArea2), res = 250) |>
terra::crop(studyArea2) |>
terra::mask(studyArea2)
isTRUE(all.equal(studyArea, studyArea2)) # Yes!
# sf class
studyAreaSmall <- prepInputs(localFileLuxSm)
studyAreas <- list()
studyAreas[["orig"]] <- prepInputs(localFileLux)
studyAreas[["reprojected"]] <- projectTo(studyAreas[["orig"]], studyAreaSmall)
studyAreas[["cropped"]] <- suppressWarnings(cropTo(studyAreas[["orig"]], studyAreaSmall))
studyAreas[["masked"]] <- suppressWarnings(maskTo(studyAreas[["orig"]], studyAreaSmall))
# SpatVector-- note: doesn't matter what class the "to" object is, only the "from"
studyAreas <- list()
studyAreas[["orig"]] <- prepInputs(localFileLux, fun = "terra::vect")
studyAreas[["reprojected"]] <- projectTo(studyAreas[["orig"]], studyAreaSmall)
studyAreas[["cropped"]] <- suppressWarnings(cropTo(studyAreas[["orig"]], studyAreaSmall))
studyAreas[["masked"]] <- suppressWarnings(maskTo(studyAreas[["orig"]], studyAreaSmall))
if (interactive()) {
par(mfrow = c(2,2));
out <- lapply(studyAreas, function(x) terra::plot(x))
}
setwd(od)
}