从栅格计算器转移到 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?
推荐答案
您可以这样做:
m <- subs(r2, cbind(-20, 1))
x <- mask(r1, m)
这篇关于从栅格计算器转移到 R 的条件命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!