Change the coordinate reference system ("project") of a SpatVector, SpatRaster or a matrix with coordinates.
# S4 method for SpatRaster
project(x, y, method, mask=FALSE, align_only=FALSE, res=NULL,
origin=NULL, threads=FALSE, use_gdal=TRUE, by_util=FALSE, pipeline="",
AOI=NULL, desired_accuracy=-1.0, allow_approx=TRUE, filename="", ...)# S4 method for SpatVector
project(x, y, partial=FALSE, pipeline="", AOI=NULL,
desired_accuracy=-1.0, allow_approx=TRUE)
# S4 method for SpatExtent
project(x, from, to)
# S4 method for matrix
project(x, from, to)
SpatVector or SpatRaster
SpatRaster, SpatVector, SpatExtent or matrix (with x and y columns) whose coordinates to project
if x is a SpatRaster, the preferred approach is for y to be a SpatRaster as well, serving as a template for the geometry (extent and resolution) of the output SpatRaster. Alternatively, you can provide a coordinate reference system (CRS) description.
You can use the following formats to define coordinate reference systems: WKT, PROJ.4 (e.g., +proj=longlat +datum=WGS84), or an EPSG code (e.g., "epsg:4326"). But note that the PROJ.4 notation has been deprecated, and you can only use it with the WGS84/NAD83 and NAD27 datums. Other datums are silently ignored.
If x is a SpatVector, you can provide a crs definition as discussed above, or any other object from which such a crs can be extracted with crs
logical. If TRUE, geometries that can only partially be represented in the output crs are included in the output
character. Method used for estimating the new cell values of a SpatRaster. One of:
bilinear: bilinear interpolation (3x3 cell window). This is used by default if the first layer of x is not categorical
mean: This can be a good choice with continuous variables if the output cells overlap with multiple input cells.
near: nearest neighbor. This is used by default if the first layer of x is categorical. This method is not a good choice for continuous values.
mode: The modal value. This can be a good choice for categorical rasters, if the output cells overlap with multiple input cells.
cubic: cubic interpolation (5x5 cell window).
cubicspline: cubic B-spline interpolation. (5x5 cell window).
lanczos: Lanczos windowed sinc resampling. (7x7 cell window).
sum: the weighted sum of all non-NA contributing grid cells.
min, q1, median, q3, max: the minimum, first quartile, median, third quartile, or maximum value.
rms: the root-mean-square value of all non-NA contributing grid cells.
logical. If TRUE, mask out areas outside the input extent. For example, to avoid data wrapping around the date-line (see example with Robinson projection). To remove cells that are NA in y (if y is a SpatRaster) you can use the mask method after calling project (this function)
logical. If TRUE, and y is a SpatRaster, the template is used for the spatial resolution and origin, but the extent is set such that all of the extent of x is included
numeric. Can be used to set the resolution of the output raster if y is a CRS
numeric. Can be used to set the origin of the output raster if y is a CRS
logical. If TRUE multiple threads are used (faster for large files)
logical. If TRUE the GDAL-warp algorithm is used. Otherwise, a slower internal algorithm is used that may be more accurate if there is much variation in the cell sizes of the output raster. Only the near and bilinear algorithms are available for the internal algorithm
logical. If TRUE and gdal=TRUE, the GDAL warp utility is used
character. A PROJ pipeline string to use for the coordinate transformation instead of letting GDAL select one automatically. You can use proj_pipelines to find available pipelines. When a pipeline is set, y is still used to set the output CRS
SpatExtent or object that has a SpatExtent to set the area of interest for the transformation. This is used to select the most approprite transformation pipeline
numeric. Only use transformations with at least this accuracy (in metres). Use -1 (the default) for no constraint. Requires GDAL >= 3.3
logical. If FALSE, only use transformations with known accuracy (no "ballpark" transformations). Requires GDAL >= 3.3
character. Output filename
additional arguments for writing files as in writeRaster
character. Coordinate reference system of x
character. Output coordinate reference system
crs, resample, warp_scale, proj_pipelines and projNetwork to enable or disable PROJ network access for datum grid downloads.
## SpatRaster
if (FALSE) {
a <- rast(ncols=40, nrows=40, xmin=-110, xmax=-90, ymin=40, ymax=60,
crs="+proj=longlat +datum=WGS84")
values(a) <- 1:ncell(a)
newcrs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
b <- rast(ncols=94, nrows=124, xmin=-944881, xmax=935118, ymin=4664377, ymax=7144377, crs=newcrs)
w <- project(a, b)
## SpatVector
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
crs(v, proj=TRUE)
cat(crs(v), "\n")
project(v, "+proj=moll")
project(v, "EPSG:2169")
}
Run the code above in your browser using DataLab