区域的斜率以及R中阈值之上和之下的返回百分比 [英] Mapping slope of an area and returning percent above and below a threshold in R

查看:124
本文介绍了区域的斜率以及R中阈值之上和之下的返回百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算斜率为0,+/-5度的区域所占的比例.换句话说,高于5度和低于5度都是不好的.我正在尝试找到实际的数字和图形.

I am trying to figure our the proportion of an area that has a slope of 0, +/- 5 degrees. Another way of saying it is anything above 5 degrees and below 5 degrees are bad. I am trying to find the actual number, and a graphic.

要实现此目的,我转向R并使用Raster包. 让我们使用一个通用国家(在这种情况下为菲律宾)

To achieve this I turned to R and using the Raster package. Let's use a generic country, in this case, the Philippines

{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)

现在让我们获取高度信息并绘制坡度.

Now let's get the altitude information and plot the slopes.

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

由于规模不够大,所以让我们简单地看一下巴拉望岛

Not very helpful due to the scale, so let's simply look at the Island of Palawan

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

更好的可视化效果.我对斜坡的大小有所了解,并且在5度的限制下,我主要局限于海岸.但我还需要更多分析信息.

A little bit better to visualize. I get a sense of the magnitude of the slopes and that with a 5 degree restriction, I am mostly confined to the coast. But I need a little bit more for analysis.

我希望结果分为两个部分: 1.选定区域的35%(组成)的斜率超过+/- 5度"或选定区域的65%在+/- 5度以内". (带有获取它的代码) 2. +/- 5度以内的所有事物都是一种颜色的图片,称之为好或绿色,而另一种事物则处于另一种颜色,称为劣质或红色.

I would like Results to be something to be in two parts: 1. " 35 % (made up) of the selected area has a slope exceeding +/- 5 degrees" or " 65 % of the selected area is within +/- 5 degrees". (with the code to get it) 2. A picture where everything within +/- 5 degrees is one color, call it good or green, and everything else is in another color, call it bad or red.

谢谢

推荐答案

您可以使用raster包中的reclassify来实现.该函数为位于定义间隔内的每个单元格值分配一个特定值.例如,您可以将间隔(0,5]内的单元格值分配给值0,并将间隔(5, maxSlope]内的单元格值分配给值1.

You can use reclassify from the raster package to achieve that. The function assigns each cell value that lies within a defined interval a certain value. For example, you can assign cell values within interval (0,5] to value 0 and cell values within the interval (5, maxSlope] to value 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")))
)

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

After the reclassification you can calculate the percentages:

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

这篇关于区域的斜率以及R中阈值之上和之下的返回百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆