warpToReference: warp raster data to match a reference raster file.

View source: R/warpToReference.R

warpToReferenceR Documentation

warp raster data to match a reference raster file.

Description

Function to reproject or resample the data in source into a new file at destination with the same extent, projection, cellsize, and alignment as reference, and (optionally) with cells outside the polygons within clip set to no data.

Usage

warpToReference(
  source,
  destination,
  reference,
  clip,
  method = "near",
  overwrite = TRUE,
  overlay = FALSE,
  type,
  compression,
  bigtiff = FALSE
)

Arguments

source

(character) path to a raster file containing data to be warped (reprojected and or resampled)

destination

(character) path to a .tif file to be created

reference

(character) path to a reference raster that defines the desired extent, projection, and cellsize.

clip

(optional, character) path to a polygon file (typically a shapefile) if supplied cells that fall outside of the polygon will be assigned NA.

method

(Character) the resampling method one of "near", "bilinear", "cubic", "cubicspline", "lanczos", "average", "mode", "max", "min", "med", "q1", or "q3" passed to gdalwarp see: http://www.gdal.org/gdalwarp.html for more details.

overwrite

(optional, logical) if TRUE (the default) than a preexisting destination file be overwritten. If FALSE it will throw an error if the file already exists.

overlay

(optional, logical) if TRUE then add the data in source to a preexisting destination grid in which case reference is ignored. This can be used to combine tiled rasters into a single grid (likely with a separate clip file for each tile).

type

(optional, character) if not set than both the data type and the no data value in the destination file will match the values in the source file. If set then the data will be encoded with the selected type and the no data value will be set to the lowest possible value for signed types and the highest possible value for unsigned types.

compression

(optional, character) can be set to 'LZW' or 'DEFLATE' in which case the gdal_warp creation option -co compress=(compression) will be used if creating a new file.

bigtiff

(optional) defaults to FALSE, can be set to TRUE, 'YES', 'NO', 'IF_NEEDED', or 'IF_SAFER' and is passed to the gdalwarp BIGTIFF creation option. See https://gdal.org/drivers/raster/gtiff.html. TRUE and FALSE are mapped to 'YES' and 'NO' respectively. This argument is only relevant when creating new files.

Details

This is a convenience wrapper to gdalwarp that facilitates making raster data align with other raster data while attempting to set many options to sensible defaults. gdalUtilities::gdalwarp() provides direct access to all the options.

Clipping and reprojecting in a single step may result in slight imperfections in the NA footprint in the final grid; some cells along the edge may end up with NA despite being centered within the polygon or vice +versa. I think this is because gdalwarp may be masking based on the center of the source pixels rather than the destination pixels. For that reason if you are both reprojecting/resampling and clipping to a polygon I recommend calling this function twice. In the first call omit the clip argument. It will reproject and resample. Then in the second call pass in the output of first and use clip.

Value

warpToReference creates a new raster file at destination matching the extent and cellsize of reference but returns nothing.


ethanplunkett/rasterPrep documentation built on Sept. 17, 2024, 1:05 p.m.