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

cropInputs(
  x,
  studyArea,
  rasterToMatch,
  verbose = getOption("reproducible.verbose", 1),
  ...
)

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

# S3 method for spatialClasses
cropInputs(
  x,
  studyArea = NULL,
  rasterToMatch = NULL,
  verbose = getOption("reproducible.verbose", 1),
  extentToMatch = NULL,
  extentCRS = NULL,
  useGDAL = getOption("reproducible.useGDAL", TRUE),
  ...
)

# S3 method for sf
cropInputs(
  x,
  studyArea = NULL,
  rasterToMatch = NULL,
  verbose = getOption("reproducible.verbose", 1),
  extentToMatch = NULL,
  extentCRS = NULL,
  ...
)

Arguments

x

A Spatial*, sf, or Raster* object.

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 x before masking. Currently, this function will not reproject the x. Optional in postProcess.

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.

verbose

Numeric, -1 silent (where possible), 0 being very quiet, 1 showing more messaging, 2 being more messaging, etc. Default is 1. Above 3 will output much more information about the internals of Caching, which may help diagnose Caching challenges. Can set globally with an option, e.g., options('reproducible.verbose' = 0) to reduce to minimal

...

Passed to raster::crop

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

useGDAL

Logical or "force". Defaults to getOption("reproducible.useGDAL" = TRUE). If TRUE, then this function will use gdalwarp only when not small enough to fit in memory (i.e., if the operation fails the raster::canProcessInMemory(x, 3) test). Using gdalwarp will usually be faster than raster::projectRaster, the function used if this is FALSE. Since since the two options use different algorithms, there may be different projection results. "force" will cause it to use GDAL regardless of the memory test described here.

Examples

# Add a study area to Crop and Mask to # Create a "study area" library(sp) library(raster) ow <- setwd(tempdir()) # make a SpatialPolygon coords1 <- structure(c(-123.98, -117.1, -80.2, -100, -123.98, 60.9, 67.73, 65.58, 51.79, 60.9), .Dim = c(5L, 2L)) Sr1 <- Polygon(coords1) Srs1 <- Polygons(list(Sr1), "s1") shpEcozone <- SpatialPolygons(list(Srs1), 1L) crs(shpEcozone) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" # make a "study area" that is subset of larger dataset coords <- structure(c(-118.98, -116.1, -99.2, -106, -118.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) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" ########## shpEcozonePostProcessed <- postProcess(shpEcozone, studyArea = StudyArea)
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
#> cropping ...
#> useGDAL is TRUE, but problem is small enough for RAM; skipping GDAL; useGDAL = 'force' to override
#> Checking for errors in SpatialPolygon
#> Found no errors.
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Checking for errors in SpatialPolygon
#> Found no errors.
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
#> reprojecting ...
#> Checking for errors in SpatialPolygon
#> Found no errors.
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
#> Checking for errors in SimpleFeature
#> Found no errors.
#> maskInputs with sf class objects is still experimental
#> intersecting ...
#> Checking for errors in SimpleFeature
#> Found no errors.
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> dist is assumed to be in decimal degrees (arc_degrees).
#> Skipping writeOutputs; filename2 is NULL
# Try manually, individual pieces shpEcozoneReprojected <- projectInputs(shpEcozone, StudyArea)
#> reprojecting ...
shpEcozoneCropped <- cropInputs(shpEcozone, StudyArea)
#> cropping ...
#> useGDAL is TRUE, but problem is small enough for RAM; skipping GDAL; useGDAL = 'force' to override
#> Checking for errors in SpatialPolygon
#> Found no errors.
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
shpEcozoneClean <- fixErrors(shpEcozone)
#> Checking for errors in SpatialPolygon
#> Found no errors.
shpEcozoneMasked <- maskInputs(shpEcozone, StudyArea)
#> Checking for errors in SimpleFeature
#> Found no errors.
#> maskInputs with sf class objects is still experimental
#> intersecting ...
#> Checking for errors in SimpleFeature
#> Found no errors.
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> dist is assumed to be in decimal degrees (arc_degrees).
setwd(ow)