【问题标题】:resample() function in R's "terra" library does not work properly in specific situationR\"terra\" 库中的 resample() 函数在特定情况下无法正常工作
【发布时间】:2023-02-04 03:26:58
【问题描述】:

在下面的代码中,我尝试使用 resample(x, y, method = "sum") 将高分辨率栅格重新采样为低分辨率栅格。但是,重采样栅格在某些边缘显示 NA。

library(terra)
set.seed(42)

low_res <- rast(xmin = -1.05, xmax = 1.05, ymin = -0.05, ymax = 2.05, res = 0.5)

high_res <- rast(xmin = -1, xmax = 1, ymin = 0, ymax = 2, res = 0.01)
high_res[] <- runif(ncell(high_res))
plot(high_res, colNA = "darkblue")

resampled <- resample(high_res, low_res, method = "sum")
plot(resampled, colNA = "darkblue")
plot(as.polygons(low_res), add=TRUE, border='black', lwd=1) 

高分辨率栅格:

重采样栅格(深蓝色像元为 NA):

但是,如果将低分辨率栅格的范围四舍五入(即删除 _.05),一切看起来都不错:

library(terra)
set.seed(42)

##################################
# only changed extent here
low_res <- rast(xmin = -1, xmax = 1, ymin = -0, ymax = 2, res = 0.5) 
##################################

high_res <- rast(xmin = -1, xmax = 1, ymin = 0, ymax = 2, res = 0.01)
high_res[] <- runif(ncell(high_res))
plot(high_res, colNA = "darkblue")

resampled <- resample(high_res, low_res, method = "sum")
plot(resampled, colNA = "darkblue")
plot(as.polygons(low_res), add=TRUE, border='black', lwd=1) 

重采样栅格:

【问题讨论】:

    标签: r raster r-raster terra


    【解决方案1】:

    好的,我认为,如果低分辨率栅格中的单元格不在高分辨率栅格的范围内,它将获得 NA 值(我希望不是这样).因此,作为一种解决方案,我尝试使用 extend() 在 high_res 栅格周围添加像元(我认为,扩展 high_res 的保守大小将是 low_res 栅格的分辨率大小)。下面的代码运行良好:

    library(terra)
    
    set.seed(42)
    low_res <- rast(xmin = -1.05, xmax = 1.05, ymin = -0.05, ymax = 2.05, res = 0.5)
    
    high_res <- rast(xmin = -1, xmax = 1, ymin = 0, ymax = 2, res = 0.01)
    high_res[] <- runif(ncell(high_res))
    plot(high_res, colNA = "darkblue")
    
    #######################################
    # add paddings around high_res raster
    # with at least the resolution size of low_res raster
    add_n_cells <- ceiling(res(low_res) / res(high_res))
    high_res <- extend(high_res, add_n_cells)
    plot(high_res, colNA = "darkblue")
    #######################################
    
    resampled <- resample(high_res, low_res, method = "sum")
    plot(resampled, colNA = "darkblue")
    plot(as.polygons(low_res), add=TRUE, border='black', lwd=1) 
    

    高分辨率栅格扩展:

    最终重采样栅格:

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多