-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distance_analyses.Rmd
executable file
·191 lines (149 loc) · 5.78 KB
/
Distance_analyses.Rmd
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
title: Distance analyses
author:
- ESR 512 - GIS in Geostatistics
- Postgraduate Institute of Science
- University of Peradeniya
- Thusitha Wagalawatta & Daniel Knitter
date: 10/2015
email: [email protected]
bibliography: Course_Statistics_SriLanka.bib
csl: harvard1.csl
output:
html_document:
toc: false
theme: united
pandoc_args: [
"+RTS", "-K64m",
"-RTS"
]
pdf_document:
toc: false
toc_depth: 2
number_sections: true
pandoc_args: [
"+RTS", "-K64m",
"-RTS"
]
highlight: pygments
---
```{r setup, eval=TRUE, echo=FALSE}
setwd("/media/daniel/homebay/Teachings/WS_2015-2016/Statistics_SriLanka/")
```
# What is the shortest path from Colombo to Kandy? #
```{r, eval=TRUE, echo=TRUE}
library(rgdal)
cities <- readOGR(dsn = "./data/DSL250-Shp", layer = "Cities")
str(cities@data)
## as polygons
colombo <- cities[cities@data$NAME=="COLOMBO",]
kandy <- cities[cities@data$NAME=="KANDY",]
## as points
library(rgeos)
p.colombo <- gCentroid(cities[cities@data$NAME=="COLOMBO",])
p.kandy <- gCentroid(cities[cities@data$NAME=="KANDY",])
par(mfrow = c(1,2))
plot(colombo, main = "Colombo")
points(p.colombo)
plot(kandy, main = "Kandy")
points(p.kandy)
## fancy
## install.packages("plotGoogleMaps")
## library(plotGoogleMaps)
## plotGoogleMaps(p.kandy)
```
## Euclidean Distance ##
The Euclidean distance between points i and j is the length of the line segment connecting them.
$$
\begin{aligned}
d(i,j) = \sqrt{(x_i-x_j)^2 + (y_i-y_j)^2}
\end{aligned}
$$
```{r, eval=TRUE, echo=TRUE}
## Euclidean Distance - manual
sqrt((p.kandy@coords[1]-p.colombo@coords[1])^2+(p.kandy@coords[2]-p.colombo@coords[2])^2)
## Euclidean Distance - with own function
e.d <- function(x, y) {
sqrt(sum((x - y)^2))
}
e.d(p.kandy@coords,p.colombo@coords)
## using package
library(fields)
rdist(p.kandy@coords,p.colombo@coords)
library(rgeos)
gDistance(p.kandy,p.colombo)
```
## Least Cost Distance ##
```{r, eval=TRUE, echo=TRUE}
## load srtm data
library(raster)
srtm <- raster(x = "./results/srtm.tif")
### LEAST COST PATH ###
#######################
nz <- 8 # neigbourhood number: 4,8,16
ras <- focal(srtm, w=matrix(1/9,nrow=3,ncol=3), NAonly=TRUE) # define the moving window and store it in the raster object; it is not allowed to chose the moving window too large because than a "jump" over high cost/ low conductivity cells would be possible
plot(ras, col = gray.colors(25, start = 0.97, end = 0.4))
# cost functions
# Tobler1993 velocity
tobler1993a <- function(s){6 * exp(-3.5 * abs(s + 0.05))} # km/h
tobler1993b <- function(s){0.36 * exp(-3.5 * abs(s + 0.05))} # m/min
## ## plot cost function
plot(tobler1993a
,xlim = c(-1,1)
,main = "Slope-dependent cost function (Tobler)"
,xaxt="n",yaxt="n"
,xlab = "",ylab = "")
mtext(side = 1, text = "slope", line = 2)
mtext(side = 2, text = "speed (km/h)", line = 2)
axis(2, mgp=c(3, .5, 0))
axis(1, mgp=c(3, .5, 0))
abline(v = 0, lty = 2)
library(gdistance) # https://cran.r-project.org/web/packages/gdistance/index.html
# auxilliary function
hdiff <- function(x){(x[2]-x[1])} # calculates the difference of a vector
# transitional object
hd <- transition(ras,hdiff,nz,symm=FALSE) # calculate the slope of the input data; store the conductivity for the movement to all other cells from any specfic cell
slope <- geoCorrection(hd,scl=FALSE) # geocorrection of the object because the diagonal distance is larger than a vertical distance [in the pixel case]
adj <- adjacent(x=ras, cells=1:ncell(ras), direction=nz) # adjacency list - from X to Y
cost <- slope
cost[adj] <- tobler1993a(slope[adj]) # and here the conductivity function comes into account
conduct <- geoCorrection(cost, scl=FALSE) # transform cost to conductivity; conductivity=cost/dist; time=1/conductivity; we need the geocorection twice because
p.colombo <- spTransform(p.colombo, CRSobj = srtm@crs)
p.kandy <- spTransform(p.kandy, CRSobj = srtm@crs)
co.to.ka <- shortestPath(conduct, origin = p.colombo@coords, goal = p.kandy@coords, output="SpatialLines")
ka.to.co <- shortestPath(conduct, origin = p.kandy@coords, goal = p.colombo@coords, output="SpatialLines")
plot(srtm,
xlim = c(300000,500000), ylim = c(730000,830000),
main = "Least-cost path\n based on Tobler's function for walking speed")
lines(co.to.ka, col = "red")
lines(ka.to.co, col = "blue")
points(p.colombo, pch = 19, cex = .5)
points(p.kandy, pch = 19, cex = .5)
text(p.colombo@coords[1], p.colombo@coords[2] - 2500, "Colombo")
text(p.kandy@coords[1], p.kandy@coords[2] + 2500, "Kandy")
## insert the modern highway from Colombo to Kandy as comparison
m.r <- readOGR("./data/DSL250-Shp", "Roads")
m.r <- spTransform(m.r, srtm@crs)
lines(m.r[m.r@data$CLASS=="A1",], col="black")
## and at the the a nice legend
legend("bottom", c("Colombo - Kandy", "Kandy - Colombo", "A1"),
lty = c(1,1,1), col = c("red", "blue", "black"), bty = "n")
e.co.ka <- SpatialLines(list(Lines(Line(coords = (rbind(p.colombo@coords,p.kandy@coords))), ID="1")),proj4string = srtm@crs)
p.co.to.ka <- extract(x=srtm, y=co.to.ka, along=TRUE)
p.ka.to.co <- extract(x=srtm, y=ka.to.co, along=TRUE)
p.e.co.ka <- extract(x=srtm, y=e.co.ka, along=TRUE)
library(rgeos)
par(mfrow = c(3,1))
plot(p.e.co.ka[[1]], type="l",
main = "Elevation profile \nfrom Colombo to Kandy (euclidean)",
sub = paste("Distance ",round(gLength(e.co.ka)/1000,2)," km",sep=""))
plot(p.co.to.ka[[1]], type="l",
main = "Elevation profile \nfrom Colombo to Kandy (least cost)",
sub = paste("Distance ",round(gLength(co.to.ka)/1000,2)," km",sep=""))
plot(p.ka.to.co[[1]], type="l",
main = "Elevation profile \nfrom Kandy to Colombo (least cost)",
sub = paste("Distance ",round(gLength(ka.to.co)/1000,2)," km",sep=""))
```
> **Exercise**
>
> Create some other shortest paths. For example for your home to the university?!