简体   繁体   中英

Calculate number of NA and non-NA values in a raster but with new resolution and extent

I need to calculate the number of NA and non-NA values in a raster with original resolution of 1 x 0.00811 but aggregated to 2 degrees and with a new extent.

The original raster (available here: https://datadryad.org/stash/dataset/doi:10.5061/dryad.052q5 , see output 1) was merged with another dataset (unfortunately, not open-source) to produce a raster in Output 2:

Output 1

class      : RasterLayer 
dimensions : 19142, 35738, 684096796  (nrow, ncol, ncell)
resolution : 0.01, 0.00811  (x, y)
extent     : -178.6931, 178.6869, -65.29534, 89.94628  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : HumanFootprintWGS84.tif 
names      : HumanFootprintWGS84 
values     : 0, 50  (min, max)

Output 2

dimensions : 19142, 35738, 684096796  (nrow, ncol, ncell)
resolution : 0.01, 0.00811  (x, y)
extent     : -178.6931, 178.6869, -65.29534, 89.94628  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : r_tmp_2022-02-10_145403_42352_01781.grd 
names      : layer 
values     : 0.6730382, 1  (min, max)

The raster I used for re-sampling was a dummy and is as follows:

Output 3

class      : RasterLayer 
dimensions : 65, 180, 11700  (nrow, ncol, ncell)
resolution : 2, 2  (x, y)
extent     : -180, 180, -65, 65  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 

Unfortunately, the merged and therefore the re-sampled raster has a lot of NA values on the coastline and near the lakes; and the averaging method in st_warp converts NA values to 0s, which skews the calculation of the mean of some of the cells.

I've decided to adjust the values resulting from averaging by multiplying the averaged values by Number of Non-NA values/(Number of Non-NAs - number of NAs), ie:

average x {Number of non-NA values/(Number of non-NA values/ number of NA values)}

For that I need to know the number of non-NA and NA values is the original merged raster (Output 2) but at 2 degree resolution and the extent in Output 3 (-180, 180, -65, 65).

I'm new to mapping and rasters so apologies if this is a basic question.

I've tried to rasterize with coordinates of original dataset and the dummy grid, but this would not get me the number of NA values, just the number of cells (plus the merged raster is over 5.1 Gb). I've tried to trim NA values (stupid idea), and st_warp but without averaging (uses nearest neighbour anyway).

If anyone has any ideas or have got a more elegant solution, I'd really appreciate it.

Thank you sincerely,

Edit : Through trial and error, I found that it makes a difference for both re-sampling (thank you again, @Robert Hijmans) and st_warp, whether the values are loaded in the memory or not.

Here are example outputs, with and without values loaded:

terra::resample with averaging without data loaded in memory

terra::resample with averaging with data loaded in memory

Example data

library(terra)
#terra 1.5.20
library(geodata)
w <- world(path=".")
# input raster
x <- rast(res=3)
x <- rasterize(w, x, field=1)

# output raster
r <- rast(res=10)

Solution:

rs <- resample(x, r, "sum")

rs
#class       : SpatRaster 
#dimensions  : 18, 36, 1  (nrow, ncol, nlyr)
#resolution  : 10, 10  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : memory 
#name        :     layer 
#min value   : 0.1111111 
#max value   :  11.11111 

plot(rs)

在此处输入图像描述

You need terra 1.5.20 for this. That is currently the development version . The easiest way to install it on Windows or OSX is with install.packages('terra', repos='https://rspatial.r-universe.dev')

Earlier versions of terra ignore the "sum" option.

In response to your comment: I get the same results with sum and average when I use a file as data source:

xx <- writeRaster(x, "test.tif", overwrite=T)
rs <- resample(xx, r, "sum")

We can compare the result with exact extraction with polygons (that also works, but will choke R on big datasets)

p <- as.polygons(r)
e <- extract(x, p, exact=TRUE, fun=sum, na.rm=TRUE)
re <- rast(r)    
re[e[,1]] <- e[,2]

plot(rs, re, xlab="resample", ylab="extract")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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