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, ...)
in_rast | A |
---|---|
in_projobj |
|
out_res |
|
crop |
|
pix_buff |
|
resamp_meth |
|
out_type |
|
out_format |
|
compression |
|
out_file |
|
warp_args | Additional parameters to be passed to |
overwrite |
|
verbose |
|
... | Other arguments (None currently implemented) |
a Raster
object, or the path of the file where the reprojected input
was saved
http://www.gdal.org/gdalwarp.html
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)#>#> 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)#>plot_rast_gg(out_rep, palette_name = "Greys", scalebar = FALSE, direction = -1) + ggplot2::geom_sf(data = my_vect, fill = "transparent", color = "red")#># reproject on projection of a different spatial object/file and crop on # its extent out_cropped <- reproj_rast(in_rast, my_vect, crop = TRUE)#>#>plot_rast_gg(out_cropped, scalebar = FALSE, palette_name = "Greys", direction = -1) + ggplot2::geom_sf(data = my_vect, fill = "transparent", color = "red")#>