我有两个大小相同的栅格,其中包含来自同一位置的数据,但是数据类型不同(一个栅格具有坡度数据,另一个栅格具有纵横数据)。我希望能够一次查看一个方面的坡度数据,因此我试图创建一个设置(也许是if / else语句?),在该设置中我说“如果(视点条件)在一个栅格中得到满足,坡度数据将从另一个栅格中的同一像素中提取。
#I have a slope and an aspect raster that i pulled
library(raster)
library(rgdal)
library(sp)
aspect <- raster("geotiff name here")
slope <- raster("geotiff name here")
#Looking at the north aspect (between 0-22.5 degrees or 337.5-360 degrees)
#First I am setting the pixels in the aspect raster that correspond to north
#equal to 1, and the values that don't = 0
aspect[aspect >= 0 & aspect <= 22.5] <- 1
aspect[aspect >= 337.5 & aspect <= 360] <- 1
aspect[aspect > 22.5 & aspect < 337.5] <- 0
#Here i am saving the indices of the raster that face north to a new one
north <- which(aspect == 1, cells = true)
然后,我只想从坡度栅格的像素中读取数据,该像素已从纵横栅格中分配了TRUE值,但这就是我的绊脚石!我最近才开始使用R,所以可能缺少一种简单的方法来实现此功能,我很想念它,感谢您的帮助。非常感谢你!
答案 0 :(得分:0)
您不需要将1转换为TRUE,因为R会自动执行此操作。尝试以下代码:
#create a data frame
data <- data.frame(aspect=aspect, slope=slope)
#create a 'north' column and populate with 1
data$north <- 1
#those that don't meet the north criteria are converted to 0
data$north[data$aspect > 22.5 & data$aspect < 337.5] <- 0
#report the 'slope' values where north=1
data$slope[data$north == 1]
答案 1 :(得分:0)
总是包含示例数据(请参阅帮助文件,以获取灵感,请点击?raster::terrain
)
library(raster)
x <- getData('alt', country='CHE')
aspect <- terrain(x, 'aspect', unit='degrees')
slope <- terrain(x, 'slope', unit='degrees')
这是重新分类的更好方法:
m <- matrix(c(0,22.5,1,22.5,337.50,0,337.5,360,1), ncol=3, byrow=TRUE)
aspectcls <- reclassify(aspect, m)
获取其中aspectcls = 0的坡度数据
nslope <- mask(slope, aspectcls, maskvalue=0)
获取值
v <- values(nslope)
boxplot(v)
您也可以这样做
crosstab(aspectcls, slope)
我不建议您采用的路径,但是如果您采用了,可以这样做
cells <- Which(aspectcls, cells=T)
vv <- slope[cells]
boxplot(vv)