Learn R Programming

terra (version 1.5-34)

project: Change the coordinate reference system

Description

Change the coordinate reference system ("project") of a SpatVector, SpatRaster or a matrix with coordinates.

Usage

# S4 method for SpatVector
project(x, y)

# S4 method for SpatRaster project(x, y, method, mask=FALSE, align=FALSE, gdal=TRUE, res=NULL, origin=NULL, filename="", ...)

# S4 method for matrix project(x, from, to)

Arguments

x

SpatRaster or SpatVector

y

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 if 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

method

character. Method used for estimating the new cell values of a SpatRaster. One of:

near: nearest neighbor. This method is fast, and it can be the preferred method if the cell values represent classes. It is not a good choice for continuous values. This is used by default if the first layer of x is categorical.

bilinear: bilinear interpolation. This is the default if the first layer of x is numeric (not categorical).

cubic: cubic interpolation.

cubicspline: cubic spline interpolation.

mask

logical. If TRUE, mask out areas outside the input extent (see example with Robinson projection)

align

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

gdal

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

res

numeric. Can be used to set the resolution of the output raster if y is a CRS

origin

numeric. Can be used to set the origon of the output raster if y is a CRS

filename

character. Output filename

...

additional arguments for writing files as in writeRaster

from

character. Coordinate reference system for x

to

character. Output coordinate reference system

Value

SpatVector or SpatRaster

See Also

crs, resample

Examples

Run this code
# NOT RUN {
## SpatRaster
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")

crs <- "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84"
p <- project(v, crs)
p

project(v, "EPSG:2169")
# }

Run the code above in your browser using DataLab