将条件命令从栅格计算器转移到R [英] Conditional command from Raster Calculator transfer to R
问题描述
到目前为止,我在ArcGIS的栅格计算器中使用了以下语句:
So far I used the following statement in the Raster Calculator of ArcGIS:
Con(("Land_use.rst" == -20), "Export.rst")
这将计算一个仅包含Land_use等于-20的导出"中的数据的新栅格.那正是我想要的.但是我想在R
中自动执行此操作,因为我必须做很多次.
This calculates a new Raster which only contains the Data from Export where Land_use equals -20. That is exactly what I want. But I want to automatise this in R
, as I have to do it a lot of times.
到目前为止,我得到的是这样的东西:
So far I got something like this:
for (catch_dir in Dir_List) {
r1 <- raster(paste0(catch_dir, '/Export.rst'))
r2 <- raster(paste0(catch_dir,'/LAND_use.rst'))
### statement that output export_streams.rst contains the data of export.rst where LAND_use.rst equals -20.
x <- overlay(r2, r1, fun=function(x,y){ y[x!=-20] <- 0; y})
writeRaster(x, filename = paste0(catch_dir, "/export_streams.rst"), format="IDRISI", NAflag = 0, datatype = "FLT4S",
overwrite=TRUE)
}
该代码执行以下操作:
它产生一个包含r1值的栅格,其中r2 = -20.这样就很好了,但是在栅格的边界上还存在一些残差,它们在r2中不等于-20.但是,两个栅格的范围相同.因此,覆盖可能不适用于我的任务.除了叠加还有其他想法吗?也许有些if(r2 == -20){ }
命令?
That code does the following:
It produces a raster which contains the values of r1 where r2 = -20. So that is good, but there are also leftovers at the border of the raster which do not equal -20 in r2. The extent of the two rasters are the same though. So the overlay probably doesnt work for my task. Any other ideas than overlay? Maybe some if(r2 == -20){ }
command?
推荐答案
我没有您的数据,所以我无法重现您的错误.但是,这就是我要对虚拟数据执行的操作:
I don't habe your data, so I cant reproduce your error. However this is how I would do it with dummy data:
#create dummy matrix
a <- matrix(c(10,10,10,10,10,10,10,10,10),nrow=3)
b <- matrix(c(-20,-20,-20,3,3,3,4,4,4),nrow=3)
#create raster from matrix
r1 <- raster(a)
r2 <- raster(b)
#Create raster with same extent and nrows/ncols as original rasters
x <- raster(r1)
#produce raster x which contains the values of r1 where r2 = -20
x[r2[]==-20] <- r1[r2[]==-20]
这篇关于将条件命令从栅格计算器转移到R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!