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

偶尔善良 提交于 2019-12-23 02:30:16

问题


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?


回答1:


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.




回答2:


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

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