从两个重叠的栅格、焦点矩阵和条件 (ifelse) 语句中检测上边缘

问题描述

我是 R 作为 GIS 的初学者。我的主要目标是通过高程梯度仅检测两个类(森林、非森林)之间的上(最高)边缘。

为了进行边缘检测,我使用了两个栅格层,数据:

Veg_DEM_Test

Veg_Class_Test

1.-Veg_DEM_Test 1km 分辨率 (r1)。

2.-Veg_Class_Test 1km 分辨率 (r2)。带有类的栅格图层,其中类 1 是非森林,类 2 是森林。

然后,我按照以下步骤操作:

首先:我创建了一个焦点矩阵来遍历所有叠加的栅格。 第二:我做了一个带有条件语句的函数,用于检测森林非森林之间的上边缘。

地点:

x=Veg_DEM_Test 和 y=Veg_Class_Test。

检测上限的条件语句是: 当矩阵的焦点单元格的植被类别为 Forest==2 时,ej.(y[5]==2) 并且同时该中心单元格低于(高程)来自 4 个单元格邻居的至少一个.这个上邻单元是类植被非森林==1 ej。 (x[5]

预期结果https://www.dropbox.com/s/sg7t3spm0f6vjy1/Veg_upper_edge.jpg?dl=0

但是我被困在这里我有一个问题要做:编写的代码看起来不错,显然运行良好,但没有产生任何光栅结果。

拜托了,希望有人能指出我正确的方向。

Pd:我对叠加的栅格进行了矢量化处理,因为在第一次尝试时,R 向我显示了一条错误消息: (函数 (x,fun,文件名 = "",recycle = TRUE,forcefun = FALSE,: 不能用这个公式,可能是因为没有向量化

代码下方找到。

R 版本:3.6.3 R Studio 版本:1.3.959 光栅包:3.4-5

library(raster)
#URL data
Veg_DEM_Test <- "https://www.dropBox.com/s/tklr5roxhd6v1n6/Veg_DEM_Test.tif?dl=0"

Veg_Class_Test <- "https://www.dropBox.com/s/t1s18rump8zfreh/Veg_Class_Test.tif?dl=0"

#Download data

download.file(Veg_DEM_Test,destfile=paste0(getwd(),"/","Veg_DEM_Test.tif"),method="auto",mode="wb",timeout="6000")
download.file(Veg_Class_Test,"Veg_Class_Test.tif"),timeout="6000")

    
#Load Rasters 
r1 <- raster("...\\Veg_DEM_Test.tif")
r1 #View the characteristics of the layer
extent(r1) #view their characteristics of the Tile
    
r2 <- raster("...\\Veg_Class_Test.tif")
r2 #View the characteristics of the layer
extent(r2) #view their characteristics of the Tile
    
extent (r1)== extent (r2)
    
#Creating the Window Matrix Move
    
f = matrix(c(0,1,0),nrow=3,byrow = T)
    
#Creating function and upper edge
    
rast_edge_TEST <-focal(na.rm = T,w=f,pad= T,(overlay(r1,r2,fun=Vectorize(function(x,y){ifelse(y[5]==2 & x[5]<x[2] & y[2]==1,-1,ifelse(y[5]==2 & x[5]<x[4] & y[4]==1,ifelse(y[5]==2 & x[5]<x[6] & y[6]==1,ifelse(y[5]==2 & x[5]<x[8] & y[8]==1,NA))))}))))

rast_edge_TEST
    
#Plotting
    
x11()
    
plot(rast_edge_TEST) 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)