【问题标题】:Extract ncell from RasterLayer in R从R中的RasterLayer中提取ncell
【发布时间】:2023-03-26 19:55:01
【问题描述】:

假设我有以下名为 newTemplate 的 RasterLayer:

class      : RasterLayer 
dimensions : 139615, 184156, 25710939940  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : 668395.5, 9876195, 2620826, 9601576  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=17 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

由此,我想直接提取与 ncell 对应的值,即不通过 nrow 和 ncol 的乘法。我该怎么做?原因是这种间接方法在 R 中返回以下警告消息:

[1] NA
Warning message:
In newTemplate@ncols * newTemplate@nrows : NAs produced by integer overflow

【问题讨论】:

    标签: r raster


    【解决方案1】:

    您想从RasterLayer 对象中获取单元格的数量?

    有一个函数:ncell

    library(raster)
    r <- raster()
    
    > print(r)
    class      : RasterLayer 
    dimensions : 180, 360, 64800  (nrow, ncol, ncell)
    resolution : 1, 1  (x, y)
    extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    crs        : +proj=longlat +datum=WGS84 +no_defs 
    
    > ncell(r)
    [1] 64800
    

    【讨论】:

    • Wunderbar,你的简单回答让我为自己无法找到这个感到有点尴尬。 +1
    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多