This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
forked from MarieAugerMethe/GISwithRDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevation demo. R
53 lines (42 loc) · 1.77 KB
/
elevation demo. R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library(rgeos)
library(rgdal)
library(raster)
library(maptools)
library(sp)
## make directory
dir.create("C:/GIS")
setwd("C:/GIS")
## get elevation data
url <- "http://www.viewfinderpanoramas.org/dem1/N46E011.zip"
download.file(url, "N46E011.zip")
unzip("N46E011.zip")
unlink("N46E011.zip")
## read elevation data
x <- readGDAL("C:/GIS/N46E011.hgt")
## coerce into RasterLayer object as desired by terrain function
y <- raster(x)
## calculate terrain
slo <- terrain(y, opt = "slope", unit = "degrees", df = F)
## set values below 25 to NA, these will be transparent
## classes -> 1 2 3 4 5 6
slo_final <- reclassify(slo, c(-Inf, 25, NA, 25, 30, 25, 30, 35, 30, 35, 40, 35, 40, 45, 40, 45, 50, 45, 50, 90, 50))
hist(slo_final, breaks = 6)
table(values(slo_final))
colv <- rev(heat.colors(6))
KML(slo_final, file="SLOPE_N46_E11.kml", maxpixel = ncell(slo_final),
overwrite = T, blur = 2, col = colv)
shell.exec("SLOPE_N46_E11.kml")
## i uploaded the below legend to imgur for latter use in
## google earth
png(file = "Legend.png", bg = "white")
plot.new()
legend("center", c("25-30", "30-35", "35-40", "40-45", "45-50", "50+"),
pch = 15, cex = 3, col = colv, bty = "n")
dev.off()
cord <- t(matrix(bbox(slo_final)[,1]))
placement_legend <- SpatialPointsDataFrame(cord, data.frame(NA))
icon <- NULL
description <- "<a href='http://i.imgur.com/ROcnDNZ.png' <img src='http://i.imgur.com/ROcnDNZ.png'</img></a>"
kmlPoints(placement_legend, kmlfile="Legend.kml", kmlname="Slope-Legend", name="Click 'Slope-Legend' link for legend..",
description="", icon=icon, kmldescription=description)
shell.exec("Legend.kml")