How does the function “extract” deal with different projections?

∥☆過路亽.° 提交于 2019-12-06 15:47:03

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!