映射区域的斜率并在R中返回阈值之上和之下的百分比

时间:2017-12-06 13:46:27

标签: r raster terrain r-raster gis

我试图计算一个斜率为0,+ / - 5度的区域的比例。另一种说法是5度以上,5度以下都不好。我想找到实际的数字和图形。

为实现这一目标,我转向R并使用Raster包。 让我们使用一个通用国家,在这种情况下,菲律宾

{list.of.packages <- c("sp","raster","rasterVis","maptools","rgeos")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)}

library(sp)  # classes for spatial data
library(raster)  # grids, rasters
library(rasterVis)  # raster visualisation
library(maptools)
library(rgeos)

现在让我们获取海拔高度信息并绘制斜坡。

elevation <- getData("alt", country = "PHL")
x <- terrain(elevation, opt = c("slope", "aspect"), unit = "degrees")
plot(x$slope)

enter image description here 由于规模不大,所以我们只需看看巴拉望岛

e <- drawExtent(show=TRUE) #to crop out Palawan (it's the long skinny island that is roughly midway on the left and is oriented between 2 and 8 O'clock)
gewataSub <- crop(x,e)
plot(gewataSub, 1)## Now visualize the new cropped object

The Island of Palawan

可视化更好一些。我了解了斜坡的大小,并且在5度的限制下,我大部分都被限制在海岸边。但我需要更多的分析。

我希望结果分为两部分: 1.“所选区域的35%(组成)具有超过+/- 5度的斜率”或“所选区域的65%在+/- 5度内”。 (用代码来获取) 2.一张照片,其中+/- 5度范围内的所有东西都是一种颜色,称之为好或绿色,其他一切都是另一种颜色,称之为坏或红色。

由于

2 个答案:

答案 0 :(得分:1)

没有负斜率,所以我假设你想要那些小于5度

的斜率
library(raster)
elevation <- getData('alt', country='CHE')
x <- terrain(elevation, opt='slope', unit='degrees')

z <- x <= 5

现在您可以使用freq

计算单元格
f <- freq(z)

如果您有平面坐标参照系(即以米为单位或类似的单位),您可以

f <- cbind(f, area=f[,2] * prod(res(z)))

获得区域。但对于lon / lat数据,您需要校正不同大小的单元格并执行

a <- area(z)
zonal(a, z, fun=sum)

有不同的绘图方式,但最基本的方法

plot(z)

答案 1 :(得分:0)

您可以使用reclassify包中的raster来实现这一目标。该函数将位于定义的时间间隔内的每个单元格值分配给某个值。例如,您可以将时间间隔(0,5]中的单元格值分配给值0,将时间范围(5, maxSlope]中的单元格值分配给值1

library(raster)  
library(rasterVis)  

elevation <- getData("alt", country = "PHL")
x <- terrain(elevation, opt = c("slope", "aspect"), unit = "degrees")
plot(x$slope)

e <- drawExtent(show = TRUE)
gewataSub <- crop(x, e)
plot(gewataSub$slope, 1)

m <- c(0, 5, 0,  5, maxValue(gewataSub$slope), 1)
rclmat <- matrix(m, ncol = 3, byrow = TRUE)
rc <- reclassify(gewataSub$slope, rclmat)

levelplot(
  rc,
  margin = F,
  col.regions = c("wheat", "gray"),
  colorkey = list(at = c(0, 1, 2), labels = list(at = c(0.5, 1.5), labels = c("<= 5", "> 5")))
)

enter image description here

重新分类后,您可以计算百分比:

length(rc[rc == 0]) / (length(rc[rc == 0]) + length(rc[rc == 1])) # <= 5 degrees
[1] 0.6628788
length(rc[rc == 1]) / (length(rc[rc == 0]) + length(rc[rc == 1])) # > 5 degrees
[1] 0.3371212