函数“extract”如何处理不同的投影?

Her*_*rot 2 r extract raster map-projections

我需要使用函数extract() 使用大小相等的正方形网格单元从栅格中进行加权平均提取。我的多边形网格在 UTM21n 中,而栅格在 GCS WGS84 基准面 D 中。在将栅格使用到 extract() 中之前,我是否必须重新投影栅格?或者函数会正确处理它吗?

pla*_*pus 5

你可以找到的功能的源代码extractSpatialPolygons 位置。代码以以下代码段开头:

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)
    }
...
Run Code Online (Sandbox Code Playgroud)

这表明它extract实际上执行了投影本身(将 SpatialPolygon 的投影更改为栅格的投影),尽管它没有在帮助页面中记录。