【问题标题】:r raster changing extent removes datar 栅格变化范围删除数据
【发布时间】:2020-04-07 03:43:47
【问题描述】:

我试图弄清楚为什么在更改范围后会收到一些简单的栅格代数的错误消息。为了证明这一点,我想我会在另一个堆栈溢出问题的一些代码之后创建一个玩具示例。

library(raster)    
## Create a matrix with random data
xy <- matrix(rnorm(400),20,20)

# generate two extents to apply
globeExtent <- extent(c(-180, 180, -90, 90))
smallerExtent <- extent(c(-180, 180, -59.5, 83.5))

# Turn the matrix into a raster
rast.smallextent <- raster(xy)
extent(rast.smallextent) <- smallerExtent

rast.globeExtent <- setExtent(rast.smallextent, ext = globeExtent, keepres = TRUE)
mathtest <- rast.globeExtent - rast.smallextent

mathtest 代码行失败,因为 rast.globeExtent 没有值,所以我实际上无法使用它来测试我在其他地方看到的错误。如何在不丢失所有数据的情况下扩展此栅格的范围?

【问题讨论】:

    标签: r r-raster


    【解决方案1】:

    如果我正确解释了这个问题,您需要做的不是更改rast.smallextent 的范围,而是扩展 栅格,使用函数@987654326 @。像这样的:

    library(raster) 
    #> Loading required package: sp
    library(tmap)
    ## Create a matrix with random data
    xy <- matrix(rnorm(400),20,20)
    
    # generate two extents to apply
    globeExtent   <- extent(c(-180, 180, -90, 90))
    smallerExtent <- extent(c(-180, 180, -20, 20))
    
    # Turn the matrix into a raster
    rast.smallextent <- raster(xy)
    extent(rast.smallextent) <- smallerExtent
    tmap::tm_shape(rast.smallextent) + tmap::tm_raster() + tmap::tm_grid()
    

    # extend the raster over a wider area, while keeping the values
    # 
    rast.globeExtent  <- extend(rast.smallextent, globeExtent)
    
    # Now rast.globeExtent is "expanded", but values are still there:
    
    rast.globeExtent
    #> class      : RasterLayer 
    #> dimensions : 90, 20, 1800  (nrow, ncol, ncell)
    #> resolution : 18, 2  (x, y)
    #> extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #> crs        : NA 
    #> source     : memory
    #> names      : layer 
    #> values     : -3.606916, 2.795636  (min, max)
    tmap::tm_shape(rast.globeExtent) + tmap::tm_raster() + tmap::tm_grid()
    

    # Math now works on the intersection, although results are "cropped" on 
    # the intersecting area
    
    rast.globeExtent <- rast.globeExtent + 1 #add 1 to check math is correct
    mathtest <- rast.globeExtent - rast.smallextent
    #> Warning in rast.globeExtent - rast.smallextent: Raster objects have different
    #> extents. Result for their intersection is returned
    mathtest
    #> class      : RasterLayer 
    #> dimensions : 20, 20, 400  (nrow, ncol, ncell)
    #> resolution : 18, 2  (x, y)
    #> extent     : -180, 180, -20, 20  (xmin, xmax, ymin, ymax)
    #> crs        : NA 
    #> source     : memory
    #> names      : layer 
    #> values     : 1, 1  (min, max)
    tmap::tm_shape(mathtest) + tmap::tm_raster() + tmap::tm_grid()
    

    HTH!

    reprex package (v0.3.0) 于 2019 年 12 月 13 日创建

    【讨论】:

    • setExtent 和 extend 的区别从所使用的术语中并不清楚,而且我没想过要搜索 setExtent 之外的内容,但是您的解决方案有效,并且我学习了 raster 包的新部分。在此之前,我找到了另一种解决方案。该范围最初是由于使用多边形文件从覆盖全球的栅格中裁剪出南极洲而引起的。裁剪过程还减少了栅格的范围。我的解决方案是将多边形文件的边界框恢复到整个纬度范围。具体代码为wrld_land@bbox &lt;- bbox(rbind(c(-180, -90), c(180, 90)))
    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2021-01-30
    • 2015-03-22
    相关资源
    最近更新 更多