I need to use the function extract() to do a weighted average extraction from a raster using a grid cell of equal sized squares. My polygon grid is in UTM21n and the raster is in GCS WGS84 datum D. Do I have to reproject the raster before using it into extract()? Or will the function handle it properly?
You can find the source code of function extract
for SpatialPolygons
here. The code starts with the following snippet:
setMethod('extract', signature(x='Raster', y='SpatialPolygons'),
function(x, y, fun=NULL, na.rm=FALSE, weights=FALSE, cellnumbers=FALSE, small=FALSE, df=FALSE, layer, nl, factors=FALSE, sp=FALSE, ...){
px <- projection(x, asText=FALSE)
comp <- .compareCRS(px, projection(y), unknown=TRUE)
if (!comp) {
.requireRgdal()
warning('Transforming SpatialPolygons to the CRS of the Raster')
y <- spTransform(y, px)
}
...
Which suggests that extract
does in fact perform the projection itself (changing the projection of the SpatialPolygon to the projection of the raster), despite the fact that it is not documented in the help page.
The documentation does not mention automatic reprojection. So, I think it is save to assume the function does not do this. Therefore, you need to reproject yourself before calling extract
.
来源:https://stackoverflow.com/questions/19311796/how-does-the-function-extract-deal-with-different-projections