project()
changes the coordinate reference system (CRS) of a GRaster
or GVector
. It has three use cases:
x
is aGRaster
andy
is aGRaster
:x
will be projected to the CRS ofy
and resampled to have the same resolution asy
. If argumentalign
isFALSE
, then it will also be cropped to the extent ofy
.x
is aGRaster
andy
is aGVector
or a CRS string (typically in Well-Known Text format):x
will be projected to the CRS specified byy
and resampled but not cropped.x
is aGVector
andy
is 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
GRaster
orGVector
to be projected.- y
A character or
GLocation
object (i.e., typically aGRaster
orGVector
): Used to set the focalGRaster
orGVector
's new CRS (and resolution and possibly extent, forGRaster
s).- align
Logical: If
FALSE
(default), andx
andy
areGRaster
s, then the extent ofx
will be cropped to the extent ofy
. IfTRUE
, no cropping is performed.- method
Character or
NULL
(forGRaster
s only): Method to use to conduct the transformation (rasters only). Partial matching is used.NULL
(default): Automatically choose based on raster properties (near
for categorical data,bilinear
for 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
x
andy
areGRaster
s, andres = "terra"
, then the samemethod
is used to resamplex
to the resolution ofy
before projectingx
.Note #2: Methods that use multiple cells will cause the focal cell to become
NA
if there is at least one cell with anNA
in the cells it draws from. TheseNA
cells can often be filled using thefallback
argument.- fallback
Logical (for projecting
GRaster
s only): IfTRUE
(default), then use "lower" resampling methods to fill inNA
cells when a "higher" resampling method is used. For example, ifmethod = "bicubic"
,NA
cells will be filled in using thebilinear
method, except when that results inNA
s, in which case thenear
method will be used. Fallback causes fewer cells to revert toNA
values, 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
GRaster
s only): Method used to set the resolution of aGRaster
in 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 ify
is aGRaster
. The output will have the same resolution asy
and 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 theterra
method first, but if that fails, then triestemplate
, thencenter
. This process can take a long time for large rasters.
- wrap
Logical:
GRaster
s: 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),wrap
should beTRUE
to avoid removing rows and columns from the "edge" of the map. The default isFALSE
.GVector
s: When projecting vectors that span the international date line at 180E/W,wrap
should beTRUE
to avoid an issue where the coordinates are incorrectly mapped to the range -180 to 180.
- verbose
Logical (for projecting
GRaster
s only): IfTRUE
, display progress. Default isFALSE
.
Details
When projecting a raster, the "fallback" methods in GRASS module 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 module.
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))
}