简体   繁体   English

raster::stack 在栅格中返回 NA 值,尽管知道值

[英]raster::stack returns NA values in raster despite know values

I am creating ASCII files to include in a MaxEnt model in R .我正在创建 ASCII 文件以包含在 R 的 MaxEnt R中。

When I create the layers and look at the raster values I can see them and they are correct, eg:当我创建图层并查看栅格值时,我可以看到它们并且它们是正确的,例如:

> r_test3
class      : RasterLayer 
dimensions : 4698, 3919, 18411462  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : 159500, 355450, 160454.6, 395354.6  (xmin, xmax, ymin, ymax)
crs        : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
source     : memory
names      : layer 
values     : 1, 43385  (min, max)

then when I stack the three rasters everything seems to be correct:然后当我堆叠三个栅格时,一切似乎都是正确的:

RegionalGridsBest
class      : RasterStack 
dimensions : 4698, 3919, 18411462, 3  (nrow, ncol, ncell, nlayers)
resolution : 50, 50  (x, y)
extent     : 159500, 355450, 160454.6, 395354.6  (xmin, xmax, ymin, ymax)
crs        : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
names      : layer.1, layer.2, layer.3 
min values :       1,       1,       1 
max values :   43385,   92917,   95704 

However when I getValues for the rasters they just show NAs:但是,当我为getValues获取值时,它们只显示 NA:

> getValues(RegionalGridsBest)
            layer.1 layer.2 layer.3
       [1,]      NA      NA      NA
       [2,]      NA      NA      NA
       [3,]      NA      NA      NA
       [4,]      NA      NA      NA
       [5,]      NA      NA      NA
       [6,]      NA      NA      NA
       [7,]      NA      NA      NA
       [8,]      NA      NA      NA
       [9,]      NA      NA      NA
      [10,]      NA      NA      NA
      [11,]      NA      NA      NA
      [12,]      NA      NA      NA
      [13,]      NA      NA      NA

Then due to this, I get this error when I try and use the vifstep function:然后由于这个原因,当我尝试使用vifstep function 时出现此错误:

vifstep(RegionalGridsBest, th = 3, maxobservations = 10000) 

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

I am not sure how tog et around this or wha tI am doing wrong.我不知道如何解决这个问题或我做错了什么。

EDIT编辑

NA values are in the files despite being set to 0 values when creating the layer: NA 值在文件中,尽管在创建层时设置为 0 值:

> summary(RegionalGridsBest)
Error in apply(object@data@values, 2, quantile, na.rm = TRUE) : 
  no slot of name "data" for this object of class "RasterStack"

> summary(r_test3)
             layer
Min.           1.0
1st Qu.    18407.0
Median     28176.5
3rd Qu.    41293.0
Max.       43385.0
NA's    17850028.0

> summary(r_test2)
           layer
Min.           1
1st Qu.    40505
Median     58967
3rd Qu.    75139
Max.       92917
NA's    17086764

> summary(r_test)
           layer
Min.           1
1st Qu.    25570
Median     51215
3rd Qu.    66486
Max.       95704
NA's    17980349

What you are showing is that the first 13 cells are NA --- that is quite common.您所展示的是前 13 个单元格是 NA --- 这很常见。 I suspect that one of your layers has only NA's.我怀疑你的一层只有 NA。 Can you inspect/show你能检查/显示

summary(RegionalGridsBest)

It clearly has to do with NA values.它显然与NA值有关。 From the example从例子

library(usdm)
file <- system.file("external/spain.grd", package="usdm")
r <- stack(file)
v <- vifstep(r, th=10)

If one layer is all NA it fails如果一层都是 NA 则失败

r[[1]][] <- NA
v <- vifstep(r, th=10)
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#  0 (non-NA) cases

But also where there is no cell that without NA's但也有没有 NA 的单元格

r <- stack(file)
r[[1]][1:500] <- NA
r[[2]][450:870] <- NA
v <- vifstep(r, th=10)
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#  0 (non-NA) cases

You can check for that with你可以检查一下

 tst <- any(is.na(RegionalGridsBest))

The cells should not all be 1 (TRUE), there should be some cells with 0 (FALSE) for vifstep to work.单元格不应全部为 1 (TRUE),应该有一些单元格为 0 (FALSE) 才能使 vifstep 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM