通过填充 NA - 在 R 中将栅格带到相同的范围 [英] Bring rasters to same extent by filling with NA - in R

查看:72
本文介绍了通过填充 NA - 在 R 中将栅格带到相同的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个具有不同几何形状/轮廓的裁剪栅格.特别是来自同一领域几年的空间产量图,但范围各不相同 - 测量并不总是整个领域,但在某些年份只是其中的一部分.我想计算这些地图的平均值并将它们组合成一个平均值栅格.然而,这确实意味着,并不是说 5 层/栅格中的每个像素都有一个值.我可以接受这些缺失值是 NA,所以最终的平均值只能通过假设字段部分的 3 个栅格来计算,其中地图不重叠.

I have several cropped rasters with different geometry/outlines. Specifically spatial yield maps from several years of the same field, but the extent varies - the measurements were not always overall the whole field, but in some years only part of it. I want to calculate a mean value of those maps and combine them into one mean-value-raster. That does mean however, that not for every pixel in let's say 5 layers/rasters there is a value. I could accept these missing values to be NA, so the final mean value would only be calculated by let's say 3 rasters for parts of the field, where the maps are not overlapping.

我想用'extend{raster}'扩展光栅,用NA值填充不重叠的部分:

I thought of extending the raster with 'extend{raster}', filling the non-overlapping parts with NA values:

y <-extend(y, shape, value=NA) #Shape 是一个矩形形状,它包含所有产量地图栅格

这适用于所有栅格.但它们仍然没有相同的程度.即使我通过 setExtent()extent() <- extent() 将范围调整到矩形 shapefile 的范围,甚至调整到其他扩展栅格之一,我仍然得到:

That works fine, for all rasters. But they still don't have the same extent. Even if I adjust the extent by setExtent() or extent() <- extent() to the extent of the rectangular shapefile or even to one of the other extended rasters, I still get:

compareRaster(x) 中的错误:不同的数字或列

...当我想堆叠它们并使用 calc(y, fun=mean,...) 时.原始栅格范围差异太大而无法重新采样.但它们确实具有相同的分辨率和 CRS.

..when I want to stack them and use calc(y, fun=mean,...). The original raster extents are too different to resample. But they do have the same resolution and CRS.

有没有人知道如何解决这个问题?

Has anyone an idea how to solve this?

推荐答案

如果你想实现诸如 calc(y, fun=mean,...) 的操作,你可以得到栅格的最小公共范围,并在将它们堆叠在一起并应用操作之前将它们全部裁剪到该范围.

If you want to achieve an operation such as calc(y, fun=mean,...), you could get the minimal common extent of your rasters, and crop them all to that extent before stacking them together and applying the operation.

假设您有三个栅格:

# Generate 3 dummy rasters with different extents
r1 <- raster( crs="+proj=utm +zone=31")
extent(r1) <- extent(0, 100, 0, 500)
res(r1) <- c(5, 5)
values(r1) <- sample(10, ncell(r1), replace=TRUE)

r2 <- raster( crs="+proj=utm +zone=31")
extent(r2) <- extent(10, 120, -10, 400) 
res(r2) <- c(5, 5)
values(r2) <- runif(ncell(r2), 1, 10)

r3 <- raster( crs="+proj=utm +zone=31")
extent(r3) <- extent(50, 150, 30, 200) 
res(r3) <- c(5, 5)
values(r3) <- runif(ncell(r3), 1, 10)

第一种方法:

# Summing your rasters will only work where they are not NA
r123 = r1+r2+r3 # r123 has the minimal common extent
r1 = crop(r1, r123) # crop to that minimal extent
r2 = crop(r2, r123)
r3 = crop(r3, r123)

s123 = stack(r1, r2, r3)
s123.mean = calc(s123, fun=mean)

另一个:

# Manually compute the minimal extent
xmin <- max(bbox(r1)[1,1], bbox(r2)[1,1], bbox(r3)[1,1])
xmax <- min(bbox(r1)[1,2], bbox(r2)[1,2], bbox(r3)[1,2])  
ymin <- max(bbox(r1)[2,1], bbox(r2)[2,1], bbox(r3)[2,1])  
ymax <- min(bbox(r1)[2,2], bbox(r2)[2,2], bbox(r3)[2,2])  
newextent=c(xmin, xmax, ymin, ymax)
r1 = crop(r1, newextent)
r2 = crop(r2, newextent)
r3 = crop(r3, newextent)

s123 = stack(r1, r2, r3)
s123.mean = calc(s123, fun=mean)

我不太确定你到底想要什么,但如果你真的想保持你所有的栅格完整(没有 crop 操作),你也可以计算最大范围(与第二个相同上面的方法,只需反转 minmax) 和 extend 在堆叠它们之前所有光栅到那个光栅(同样的事情,你最终得到更大的栅格,充满了 NA ......不确定这是否真的需要):

I am not too sure of what you exactly want, but if you really want to keep all your rasters complete (no crop operation), you could also compute the largest extent (same as the second method above, just invert min and max) and extend all you rasters to that one before stacking them (same thing, you just end up with larger rasters, filled with NA's... not sure this is really desired):

xmin <- min(bbox(r1)[1,1], bbox(r2)[1,1], bbox(r3)[1,1])
xmax <- max(bbox(r1)[1,2], bbox(r2)[1,2], bbox(r3)[1,2])  
ymin <- min(bbox(r1)[2,1], bbox(r2)[2,1], bbox(r3)[2,1])  
ymax <- max(bbox(r1)[2,2], bbox(r2)[2,2], bbox(r3)[2,2])  
newextent=c(xmin, xmax, ymin, ymax)
r1 = extend(r1, newextent)
r2 = extend(r2, newextent)
r3 = extend(r3, newextent)
s123 = stack(r1, r2, r3)
s123.mean = calc(s123, fun=mean)

这有帮助吗?

注意

您可能不想使用 setExtent()extent() <- extent(),因为您可能会以错误的栅格地理坐标结束(即,范围将被修改,但内容不会被修改,因此您实际上可能会在不针对此目标的情况下翻译您的栅格,因为它们之间产生的重叠在物理上毫无意义).

You might not want to play with setExtent() or extent() <- extent(), as you could end with wrong geographic coordinates of your rasters (i.e., the extent would be modified, but not the content, so you'd actually translate your rasters likely without aiming at that as the resulting overlap between them would be physically meaningless).

这篇关于通过填充 NA - 在 R 中将栅格带到相同的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆