Reproject a raster "R" object or file to a different reference system. The function is a simple wrapper around gdalwarp with additional checks on inputs allowing to specify the output projection in several ways: 1: passing a valid proj4 string (e.g., reproj_rast(in_rast, "+init=epsg:4326") 2: passing a numeric or character that can be interpreted as an EPSG code (e.g., reproj_rast(in_vect, 4326)); 3: passing the name of a valid R vector or raster object: (e.g.,reproj_rast(in_vect, rast_obj), with rast_obj being an existing R object; 4: passing the path to a valid vector or raster file: EPSG code (e.g.,reproj_rast(in_vect, "D:/Temp/myfile.tif")

The reprojected raster is written to a temporary "GTiff" file within R tempdir to allow accessing it immediately from R, unless a specific output file name is provided with the out_file argument.

reproj_rast(in_rast, in_projobj, out_res = NULL, crop = FALSE,
  pix_buff = 1, resamp_meth = "near", out_type = "rastobject",
  out_format = "GTiff", compression = "LZW", out_file = NULL,
  warp_args = NULL, overwrite = FALSE, verbose = TRUE, ...)

Arguments

in_rast

A *Raster object, or the path to a raster file

in_projobj

R object or filename from which the output projection should be derived (see @description)

out_res

numeric (1 | 2) desired resolution for the output in X and Y (in measure units of the OUTPUT projection). If a 1-element array is passed, the same resolution is used for X and Y. If NULL, the output resolution is set automatically by gdalwarp based on input/output projections and the resolution of the input, Default: NULL

crop

logical If TRUE, and in_projobj corresponds to a spatial object or filename, the output is also cropped on the extent of in_projobj, Default: FALSE

pix_buff

numeric Dimension of a buffer around the extent to be added to it to guarantee that all the area of in_projobj is preserved in the reprojected dataset, Default: 1 (ignored if crop == FALSE)

resamp_meth

character ["near", "bilinear", "cubic", "cubicspline", "lanczos", "average", "mode"] Resampling method to be used by gdalwarp (See http://www.gdal.org/gdalwarp.html), Default: 'near'

out_type

character ["rastfile", "rastobject"] If "rastfile", and out_file is not NULL, the function returns the name of the saved raster. Otherwise, it returns the reprojected raster as a *Raster object

out_format

character Format to be used to save the reprojected raster, Default: 'GTiff'

compression

character ["NONE" | "PACKBITS" | "LZW" | "DEFLATE"] Compression method to be used to save the raster if out_format is "GTiff", Default: 'LZW'

out_file

character Path where the reprojected vector should be saved. If NULL, the reprojected raster saved on R temporary folder, and will not be accessible after closing the session, Default: NULL

warp_args

Additional parameters to be passed to gdalwarp, Default: NULL (Not currently implemented)

overwrite

logical If TRUE, overwrite existing files, Default: FALSE

verbose

logical If FALSE, suppress processing messages, Default: TRUE

...

Other arguments (None currently implemented)

Value

a Raster object, or the path of the file where the reprojected input was saved

See also

http://www.gdal.org/gdalwarp.html

Examples

library(sprawl.data) library(ggplot2) # reproject a raster based on an output proj4string in_file <- system.file("extdata/OLI_test", "oli_multi_1000_b2.tif", package = "sprawl.data") out_proj <- "+init=epsg:3035" reproj_rast(in_file, out_proj)
#> reproj_rast --> Reprojecting `in_file` to +init=epsg:3035 +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> class : RasterLayer #> dimensions : 1006, 993, 998958 (nrow, ncol, ncell) #> resolution : 30.02464, 30.02464 (x, y) #> extent : 4151395, 4181210, 2606588, 2636793 (xmin, xmax, ymin, ymax) #> coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs #> data source : /tmp/Rtmpzfhi9J/file785240140a22.tif #> names : oli_multi_1000_b2 #>
# reproject on projection of a different spatial object/file in_rast <- read_rast(in_file) my_vect <- get(load(system.file("extdata", "Lake.RData", package = "sprawl.data"))) out_rep <- reproj_rast(in_rast, my_vect)
#> reproj_rast --> Reprojecting `in_rast` to +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
plot_rast_gg(out_rep, palette_name = "Greys", scalebar = FALSE, direction = -1) + ggplot2::geom_sf(data = my_vect, fill = "transparent", color = "red")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
# reproject on projection of a different spatial object/file and crop on # its extent out_cropped <- reproj_rast(in_rast, my_vect, crop = TRUE)
#> reproj_rast --> Reprojecting `in_rast` to +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#> crop_rast --> Reprojecting extent of: . to: in_proj
plot_rast_gg(out_cropped, scalebar = FALSE, palette_name = "Greys", direction = -1) + ggplot2::geom_sf(data = my_vect, fill = "transparent", color = "red")
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.