Change the coordinate reference system
Change the coordinate reference system ("project") of a SpatVector or SpatRaster.
## S4 method for signature 'SpatVector' project(x, y) ## S4 method for signature 'SpatRaster' project(x, y, method="bilinear", mask=FALSE, filename="", ...)
x |
SpatVector |
y |
if ( You can use the following formats to provide textual coordinate reference system (crs) definitions: WKT, PROJ.4 (e.g., If |
method |
character. Method used for estimating the new cell values. 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. bilinear: bilinear interpolation. Default. cubic: cubic interpolation. cubicspline: cubic spline interpolation. |
mask |
logical. If |
filename |
character. Output filename |
... |
additional arguments for writing files as in |
SpatVector or SpatRaster
User beware. Sadly, the PROJ.4 notation has been partly deprecated in the GDAL/PROJ library that is used by this function. You can still use this notation, but *only* with the the WGS84 datum. Other datums are silently ignored.
When printing a Spat* object the PROJ.4 notation is shown because it is the most concise and clear format available. However, internally a WKT representation is used (see crs
).
Transforming (projecting) raster data is fundamentally different from transforming vector data. Vector data can be transformed and back-transformed without loss in precision and without changes in the values. This is not the case with raster data. In each transformation the values for the new cells are estiamted in some fashion. Therefore, if you need to match raster and vector data for analysis, you should generally transform the vector data.
When using this method with a SpatRaster
, the preferable approach is to provide a template SpatRaster
as argument y
. The template is then another raster dataset that you want your data to align with. If you do not have a template to begin with, you can do project(x, crs)
and then manipulate the output to get the template you want. For example, where possible use whole numbers for the extent and resolution so that you do not have to worry about small differences in the future. You can use commands like dim(z) = c(180, 360)
or res(z) <- 100000
.
The output resolution should be similar to the input resolution, but there is not "correct" resolution in raster transformation; but it is not obvious what this resolution is if you are using lon/lat data that spans a large North-South extent.
## SpatRaster a <- rast(ncol=40, nrow=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(ncol=94, nrow=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 <- "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84" p <- project(v, crs) p
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.