project() changes the coordinate reference system (CRS) of a GRaster or GVector. It has three use cases:
xis aGRasterandyis aGRaster:xwill be projected to the CRS ofyand resampled to have the same resolution asy. If argumentalignisFALSE, then it will also be cropped to the extent ofy.xis aGRasterandyis aGVectoror a CRS string (typically in Well-Known Text format):xwill be projected to the CRS specified byyand resampled but not cropped.xis aGVectorandyis aGRaster,GVector, or CRS string: The vector will be projected to the CRS ofy.
Usage
# S4 method for class 'GRaster'
project(
x,
y,
align = FALSE,
method = NULL,
fallback = TRUE,
res = "fallback",
wrap = FALSE,
verbose = FALSE
)
# S4 method for class 'GVector'
project(x, y, wrap = FALSE)Arguments
- x
A
GRasterorGVectorto be projected.- y
A character or
GLocationobject (i.e., typically aGRasterorGVector): Used to set the focalGRasterorGVector's new CRS (and resolution and possibly extent, forGRasters).- align
Logical: If
FALSE(default), andxandyareGRasters, then the extent ofxwill be cropped to the extent ofy. IfTRUE, no cropping is performed.- method
Character or
NULL(forGRasters only): Method to use to conduct the transformation (rasters only). Partial matching is used.NULL(default): Automatically choose based on raster properties (nearfor categorical data,bilinearfor continuous data)."near": Nearest neighbor. Best for categorical data, and often a poor choice for continuous data. Ifdatatype()isinteger, this method will be used by default."bilinear": Bilinear interpolation (default for non-categorical data; uses weighted values from 4 cells)."bicubic": Bicubic interpolation (uses weighted values from 16 cells)."lanczos": Lanczos interpolation (uses weighted values from 25 cells).
Note #1: If
xandyareGRasters, andres = "terra", then the samemethodis used to resamplexto the resolution ofybefore projectingx.Note #2: Methods that use multiple cells will cause the focal cell to become
NAif there is at least one cell with anNAin the cells it draws from. TheseNAcells can often be filled using thefallbackargument.- fallback
Logical (for projecting
GRasters only): IfTRUE(default), then use "lower" resampling methods to fill inNAcells when a "higher" resampling method is used. For example, ifmethod = "bicubic",NAcells will be filled in using thebilinearmethod, except when that results inNAs, in which case thenearmethod will be used. Fallback causes fewer cells to revert toNAvalues, so may be better at capturing complex "edges" (e.g., coastlines). Fallback does increase processing time because each "lower" method must be applied, then results merged. Fallback is not used ifmethod = "near".- res
Character (for projecting
GRasters only): Method used to set the resolution of aGRasterin the new CRS. This can be one of three options. Partial matching is used and case ignored:"terra": This method creates an output raster that is as close as possible in values and resolution to the one thatterra::project()would create. However, for large rasters (i.e., many cells), this can fail becauseterra::project()encounters memory limits (it is used internally to create a template). This method resamples the focal raster in its starting CRS, then projects it to the destination CRS."template": This method can only be used ifyis aGRaster. The output will have the same resolution asyand possibly the same extent (depending on the value ofalign). However, unlike the"terra"method, cell values will not necessarily be as close as possible to whatterra::project()would generate (unlessmethod = "near"). Unlike the"terra"method, this method does not resample the focal raster in its starting CRS before projecting. For large rasters it will be faster than the"terra"method (especially if"method = "near"), and it should be less likely to fail because of memory limits.Two numeric values: Values for the new resolution (x- and y-dimensions).
"center": This method locates the centroid of the raster to be projected (in the same CRS as the original raster). It then creates four points north, south, east, and west of the centroid, each spaced one cell's width from the centroid. This set of points is then projected to the new CRS. The new cell size in the x-dimension will be the average of the distance between the east and west points from the centroid, and in the y-dimension the average from the centroid to the north and south points."fallback"(default): This applies theterramethod first, but if that fails, then triestemplate, thencenter. This process can take a long time for large rasters.
- wrap
Logical:
GRasters: When projecting rasters that "wrap around" (i.e., whole-world rasters or rasters that have edges that actually circle around to meet on the globe),wrapshould beTRUEto avoid removing rows and columns from the "edge" of the map. The default isFALSE.GVectors: When projecting vectors that span the international date line at 180E/W,wrapshould beTRUEto avoid an issue where the coordinates are incorrectly mapped to the range -180 to 180.
- verbose
Logical (for projecting
GRasters only): IfTRUE, display progress. Default isFALSE.
Details
When projecting a raster, the "fallback" methods in GRASS tool r.import are actually used, even though the method argument takes the strings specifying non-fallback methods. See the manual page for the r.import GRASS tool.
See also
terra::project(), sf::st_transform(), GRASS manual pages for modules r.proj and v.proj (see grassHelp("r.proj") and grassHelp("v.proj"))
Examples
if (grassStarted()) {
### Setup for all examples
library(sf)
library(terra)
# Climate raster, elevation raster, rivers vector
madElev <- fastData("madElev")
madRivers <- fastData("madRivers")
madChelsa <- fastData("madChelsa")
# Convert objects into fasterRaster formats
chelsa <- fast(madChelsa)
elev <- fast(madElev)
rivers <- fast(madRivers)
### Project raster without resampling
elevWGS84 <- project(elev, crs(chelsa))
elevWGS84
### Project raster and resample to resolution of another raster
elevWGS84Resamp <- project(elev, chelsa)
elevWGS84Resamp
res(elevWGS84)
res(elevWGS84Resamp)
res(chelsa)
### Project vector
riversWGS84 <- project(rivers, chelsa)
riversWGS84
cat(crs(rivers)) # using "cat()" to make it look nice
cat(crs(riversWGS84))
}
