Description Usage Arguments Details Value Note Note Author(s) See Also Examples
View source: R/projectRaster.R
Project the values of a Raster* object to a new Raster* object with another projection (coordinate reference system, (CRS)). You can do this by providing the new projection as a single argument in which case the function sets the extent and resolution of the new object. To have more control over the transformation, and, for example, to assure that the new object lines up with other datasets, you can provide a Raster* object with the properties that the input data should be projected to.
projectExtent
returns a RasterLayer with a projected extent, but without any values. This RasterLayer can then
be adjusted (e.g. by setting its resolution) and used as a template 'to'
in projectRaster
.
1 2 3 4 | projectRaster(from, to, res, crs, method="bilinear",
alignOnly=FALSE, over=FALSE, filename="", ...)
projectExtent(object, crs)
|
from |
Raster* object |
to |
Raster* object with the parameters to which 'from' should be projected |
res |
single or (vector of) two numerics. To, optionally, set the output resolution if 'to' is missing |
crs |
character or object of class 'CRS'. PROJ.4 description of the coordinate reference system. In projectRaster this is used to set the output CRS if 'to' is missing, or if 'to' has no valid CRS |
method |
method used to compute values for the new RasterLayer. Either 'ngb' (nearest neighbor), which is useful for categorical variables, or 'bilinear' (bilinear interpolation; the default value), which is appropriate for continuous variables. |
alignOnly |
logical. Use |
over |
logical. If |
filename |
character. Output filename |
... |
additional arguments as for |
object |
Raster* object |
There are two approaches you can follow to project the values of a Raster object.
1) Provide a crs
argument, and, optionally, a res
argument, but do not provide a to
argument.
2) Create a template Raster with the CRS you want to project to. You can use an existing object, or use projectExtent
for this
or an existing Raster* object. Also set the number of rows and columns (or the resolution), and perhaps adjust the extent. The resolution of the output raster should normally be similar to that of the input raster. Then use that object as from
argument to project the input Raster to.
This is the preferred method because you have most control. For example you can assure that the resulting Raster object lines up with other Raster objects.
Projection is performed using the PROJ.4 library accessed through the rgdal package.
One of the best places to find PROJ.4 coordinate reference system descriptions is https://www.spatialreference.org.
You can also consult this page: http://geotiff.maptools.org/proj_list/ to find the parameter options and names for projections.
Also see projInfo('proj')
, projInfo('ellps')
, and projInfo('datum')
for valid PROJ.4 values.
RasterLayer or RasterBrick object.
If the resolution of the output is much larger than that of the input, you should first aggregate the input such that the resolution of the input becomes more similar (perhaps a little smaller) to the output.
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 it, 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
).
Vector (points, lines, polygons) can be transformed with spTransform
.
projectExtent
does not work very well when transforming projected circumpolar data to (e.g.) longitude/latitude.
With such data you may need to adjust the returned object. E.g. do ymax(object) <- 90
Robert J. Hijmans and Joe Cheng
resample
, CRS-class
, projInfo
, spTransform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # create a new (not projected) RasterLayer with cellnumbers as values
r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40)
r <- setValues(r, 1:ncell(r))
crs(r)
# wkt(r)
# proj.4 projection description
newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"
# we need the rgdal package for this
if (require(rgdal)) {
#simplest approach
pr1 <- projectRaster(r, crs=newproj)
# alternatively also set the resolution
pr2 <- projectRaster(r, crs=newproj, res=20000)
# inverse projection, back to the properties of 'r'
inv <- projectRaster(pr2, r)
# to have more control, provide an existing Raster object, here we create one
# using projectExtent (no values are transferred)
pr3 <- projectExtent(r, newproj)
# Adjust the cell size
res(pr3) <- 200000
# now project
pr3 <- projectRaster(r, pr3)
## Not run:
# using a higher resolution
res(pr1) <- 10000
pr <- projectRaster(r, pr1, method='bilinear')
inv <- projectRaster(pr, r, method='bilinear')
dif <- r - inv
# small difference
plot(dif)
## End(Not run)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.