如何在R中使用ifelse(条件语句)覆盖两个栅格? [英] How to overlay two Rasters using ifelse (Conditional Statements) in R?

查看:75
本文介绍了如何在R中使用ifelse(条件语句)覆盖两个栅格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个栅格(图像),并希望使用以下代码对其进行覆盖:

I have two rasters (images), and want to overlay them using this code:

# Getting the images
library(raster)

URL1 <- "https://www.dropbox.com/s/6jjz7ou1skz88wr/raster_1.tif?dl=1"
URL2 <- "https://www.dropbox.com/s/d5xuixohjqfnfze/raster_2.tif?dl=1"

download.file(URL1, destfile=paste0(getwd(),"/", "raster_1.tif"), method="auto", mode="wb", timeout="6000")
download.file(URL2, destfile=paste0(getwd(),"/", "raster_2.tif"), method="auto", mode="wb", timeout="6000")

# Reading the images 
raster_1 <- raster(list.files(pattern="raster_1.tif$"))
raster_2 <- raster(list.files(pattern="raster_2.tif$"))

# Overlaying
myFun <- function(x,y){ifelse(x==0 && y==0, 0, ifelse(x==1 && y==0, 2, ifelse(x==1 && y>0, y)))}

( res <- overlay(stack(raster_1 ,raster_2), fun = Vectorize(myFun) ) )

### R gives this error
Error in .overlayList(x, fun = fun, filename = filename, forcefun = forcefun,  : 
  cannot use this formula, probably because it is not vectorized

如果有人能帮助我,我将不胜感激.

I would be very grateful if anyone could help me.

谢谢.

推荐答案

您需要一个仅使用矢量化运算符的函数.在这种情况下,布尔运算既要成功又要更有效率

You need a function that only uses vectorized operators. This is case where Boolean arithmetic should both succeed and be more efficient

myFun <- function(x,y){ 0*(x==0 && y==0)+ 
                        2*(x==1 && y==0)+
                        y*(x==1 && y>0) }

有些边缘情况没有被掩盖. x 可以不是完全为0或1的值吗? y 可以为负吗?

There are some edge cases that do not appear covered. Can x ever be a value other than exactly 0 or 1? Can y ever be negative?

运行我的版本后,我得到:

After running my version I get:

> ( res <- overlay(stack(raster_1 ,raster_2), fun = Vectorize(myFun) ) )
class       : RasterLayer 
dimensions  : 2958, 1642, 4857036  (nrow, ncol, ncell)
resolution  : 500, 500  (x, y)
extent      : -171063.8, 649936.2, 5317253, 6796253  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=12 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : in memory
names       : layer 
values      : 0, 14751  (min, max)

我认为我不需要在 myFun 周围使用 Vectorize ,在此处输入代码,但结果似乎更有可能是正确的当我将其保留在对 overlay :

I didn't think I would need to use Vectorize around myFun,enter code here but the results seems more likely to be correct when I leave it in the call to overlay:

> Hmisc::describe(values(res))
values(res) 
       n  missing distinct     Info     Mean      Gmd      .05      .10      .25 
 3222508  1634528     1502    0.727     4918     6403        0        0        0 
     .50      .75      .90      .95 
       0    13898    14082    14168 

Value            0   13000   13200   13400   13600   13800   14000   14200   14400
Frequency  2089448      67     578   10515   69031  249817  523241  226628   46191
Proportion   0.648   0.000   0.000   0.003   0.021   0.078   0.162   0.070   0.014

Value        14600   14800
Frequency     6876     116
Proportion   0.002   0.000

当我执行Vectorize步骤时,我没有收到错误,而是得到了全零.

When I took out the Vectorize step I did not get an error but I got all zeros, instead.

这篇关于如何在R中使用ifelse(条件语句)覆盖两个栅格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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