The method for spatialObjects (Raster* and Spatial*) will crop, reproject, and mask, in that order. This function is a wrapper for cropInputs, fixErrors, projectInputs, maskInputs and writeOutputs, with a decent amount of data manipulating between these calls so that the crs match.

This function can be used to crop or reproject module inputs from raw data.

A simple wrapper around the various different tools for these GIS types.

This function can be used to mask module inputs from raw data.

If filename2 is logical, then the cropped/masked raster will be written to disk with the original targetFile name, with "Small" prefixed to the basename(targetFilename). If a character string, it will be the path of the saved raster. It will be tested whether it is an absolute or relative path and used as is if absolute or prepended with destinationPath if relative.

Can be used to write prepared inputs on disk.

# S3 method for spatialObjects
postProcess(pp, filename1 = NULL, studyArea = NULL,
  rasterToMatch = NULL, overwrite = TRUE, useSAcrs = FALSE,
  useCache = getOption("reproducible.useCache", FALSE), ...)

cropInputs(ci, studyArea, rasterToMatch, ...)

# S3 method for default
cropInputs(ci, studyArea, rasterToMatch, ...)

# S3 method for spatialObjects
cropInputs(ci, studyArea, rasterToMatch = NULL,
  extentToMatch = NULL, extentCRS = NULL, ...)

projectInputs(pi, targetCRS, ...)

# S3 method for Raster
projectInputs(pi, targetCRS = NULL, rasterToMatch = NULL,
  ...)

# S3 method for sf
projectInputs(pi, targetCRS, ...)

# S3 method for Spatial
projectInputs(pi, targetCRS, ...)

maskInputs(mi, studyArea, ...)

# S3 method for Raster
maskInputs(mi, studyArea, rasterToMatch, maskWithRTM = FALSE,
  ...)

# S3 method for Spatial
maskInputs(mi, studyArea, ...)

determineFilename(filename2 = TRUE, filename1 = NULL,
  destinationPath = NULL, ...)

writeOutputs(wo, filename2, overwrite, ...)

# S3 method for Raster
writeOutputs(wo, filename2, overwrite = FALSE, ...)

# S3 method for Spatial
writeOutputs(wo, filename2, overwrite = FALSE, ...)

# S3 method for sf
writeOutputs(wo, filename2, overwrite = FALSE, ...)

# S3 method for default
writeOutputs(wo, filename2, ...)

Arguments

pp

A Spatial*, sf, or Raster* object.

filename1

Character string giving the file path of the input object, if it has one. This is then used if filename2 is specified here as TRUE (the default, and passed to determineFilename) to name the output file, where the resulting post-processed filename will be .prefix(basename(filename1), "Small"). Mostly used by prepInputs, where filename2 is missing.

studyArea

SpatialPolygons* object used for masking and possibly cropping if no rasterToMatch is provided. If not in same CRS, then it will be spTransformed to CRS of ci before masking. Currently, this function will not reproject the ci. Optional in postProcess. postProcess.spatialObjects

rasterToMatch

Template Raster* object used for cropping (so extent should be the extent of desired outcome) and reprojecting (including changing the resolution and projection). See details in postProcess.spatialObjects.

overwrite

Logical. Should file being written overwrite an existing file if it exists.

useSAcrs

Logical. If FALSE, the default, then the desired projection will be taken from rasterToMatch or none at all. If TRUE, it will be taken from studyArea.

useCache

Passed to Cache in various places. Default FALSE

...

Additional arguments passed to cropInputs, projectInputs, maskInputs, determineFilename, and writeOutputs. These then pass ... into other functions, like writeRaster, or sf::st_write. This might include potentially important arguments like datatype, format. Also passed to projectRaster, with likely important arguments such as method = "bilinear". See details.

... passed to:

FunctionArgumentscropInputscrop
projectInputsprojectRastermaskInputsfastMask or intersect
fixErrorsbufferwriteOutputswriteRaster or shapefile
determineFilenameFunction
* Can be overridden with useSAcrs ** Will mask with NAs from rasterToMatch if maskWithRTM
ci

A Spatial*, sf, or Raster* object.

extentToMatch

Optional. Can pass an extent here and a crs to extentCRS instead of rasterToMatch. These will override rasterToMatch, with a warning if both passed.

extentCRS

Optional. Can pass a crs here with an extent to extentTomatch instead of rasterToMatch

pi

A Raster*, Spatial* or sf object

targetCRS

The CRS of pi at the end of this function (i.e., the goal)

mi

A Raster* object

maskWithRTM

Logical. If TRUE, then the default,

filename2

Logical or character string (a file path) for the output file, after post processing, if saving is desired. See details.

destinationPath

Optional. If filename2 is a relative file path, then this will be the directory of the resulting absolute file path.

wo

The object save to disk i.e., write outputs

Value

A file of the same type as starting, but with projection (and possibly other characteristics, including resolution, origin, extent if changed.

Details

If filename2 is logical, then the output filename will be "Small" prefixed to the basename(filename1). If a character string, it will be the path returned. It will be tested whether it is an absolute or relative path and used as is if absolute or prepended with destinationPath if provided, and if filename2 is relative.

Post processing sequence

If the rasterToMatch or studyArea are passed, then the following sequence will occur:

  1. Fix errors fixErrors. Currently only errors fixed are for SpatialPolygons using buffer(..., width = 0).

  2. Crop using cropInputs

  3. Project using projectInputs

  4. Mask using maskInputs

  5. Determine file name determineFilename

  6. Write that file name to disk, optionally writeOutputs

NOTE: checksumming does not occur during the post-processing stage, as there are no file downloads. To achieve fast results, wrap prepInputs with Cache

NOTE: sf objects are still very experimental.

File naming

Post processing addresses several scenarios, and depending on which scenario, file names are subtly different. For example, Raster objects may have file-backed data, and so possess a file name, whereas Spatial objects do not. Similarly, there may or may not be a desire to write an object to disk after all post processing. This subtlety means that there are two file names that may be at play: the "input" file name (filename1), and the "output" filename (filename2). When this is used only with post process, it is straight forward. However, when postProcess is used within a prepInputs call, the filename1 file is actually the file name of the downloaded file (i.e., what name to give to the downloaded object) and the filename2 is the file name of the of post-processed file.

Passing rasterToMatch and/or studyArea

Depending on which of these were passed, different things will happen to the targetFile located at filename1.

If targetFile is a Raster* object:

rasterToMatchstudyAreaBoth
extentYesYesrasterToMatch
resolutionYesNorasterToMatch
projectionYesNo*rasterToMatch*
alignmentYesNorasterToMatch
maskNo**YesstudyArea**
rasterToMatchstudyArea
* Can be overridden with useSAcrs ** Will mask with NAs from rasterToMatch if maskWithRTM

If targetFile is a Spatial* object:

rasterToMatchstudyAreaBoth
extentYesYesrasterToMatch
resolutionNANANA
projectionYesNo*rasterToMatch*
alignmentNANANA
maskNoYesstudyArea
rasterToMatchstudyArea
* Can be overridden with useSAcrs

Examples

# download a zip file from internet, unzip all files, load as shapefile, Cache the call dPath <- file.path(tempdir(), "ecozones") shpEcozone <- prepInputs(destinationPath = dPath, url = "http://sis.agr.gc.ca/cansis/nsdb/ecostrat/zone/ecozone_shp.zip")
#> Preparing:
#> Downloading http://sis.agr.gc.ca/cansis/nsdb/ecostrat/zone/ecozone_shp.zip ...
#> | | | 0% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========= | 14% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |=============== | 21% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |======================= | 32% | |======================= | 33% | |======================= | 34% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 62% | |============================================ | 63% | |============================================ | 64% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================= | 94% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
#> From:ecozone_shp.zip Extracting all files: ecozones.dbf, ecozones.prj, ecozones.sbn, ecozones.sbx, ecozones.shp, ecozones.shx
#> targetFile was not specified. Trying raster::shapefile on ecozones.shp. If that is not correct, please specify different targetFile and/or fun.
#> Trying ecozones.shp with raster::shapefile.
#> No cacheRepo supplied. Using value in getOption('reproducible.cachePath')
#> No cacheRepo supplied. Using value in getOption('reproducible.cachePath')
#> Appending checksums to CHECKSUMS.txt. If you see this message repeatedly, #> you can specify targetFile (and optionally alsoExtract) so it knows #> what to look for.
#> Checking local files...
#> Finished checking local files.
#' # Add a study area to Crop and Mask to # Create a "study area" library(sp) library(raster) coords <- structure(c(-122.98, -116.1, -99.2, -106, -122.98, 59.9, 65.73, 63.58, 54.79, 59.9), .Dim = c(5L, 2L)) Sr1 <- Polygon(coords) Srs1 <- Polygons(list(Sr1), "s1") StudyArea <- SpatialPolygons(list(Srs1), 1L) crs(StudyArea) <- "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" ########## shpEcozonePostProcessed <- postProcess(shpEcozone, studyArea = StudyArea)
#> No cacheRepo supplied. Using value in getOption('reproducible.cachePath')
#> cropping ...
#> Checking for errors in SpatialPolygon
#> Found no errors.
#> No cacheRepo supplied. Using value in getOption('reproducible.cachePath')
#> No cacheRepo supplied. Using value in getOption('reproducible.cachePath')
#> intersecting ...
#> Checking for errors in studyArea
#> Found no errors.
#> Please provide filename2; will use: file4ca94063576a
# Try manually, individual pieces shpEcozoneCropped <- cropInputs(shpEcozone, StudyArea)
#> cropping ...
shpEcozoneClean <- fixErrors(shpEcozone)
#> Checking for errors in SpatialPolygon
#> Found no errors.
shpEcozoneMasked <- maskInputs(shpEcozone, StudyArea)
#> intersecting ...
#> Checking for errors in studyArea
#> Found no errors.