-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
229 lines (181 loc) · 4.86 KB
/
README.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# sloperouting
<!-- badges: start -->
<!-- badges: end -->
The goal of sloperouting is to provide a place to share code/data for slope sensitive routing.
The starting point is having the latest version of `sfnetworks` installed:
```{r, eval=FALSE}
remotes::install_github("luukvdmeer/sfnetworks")
remotes::install_github("itsleeds/slopes")
remotes::install_github("itsleeds/od")
```
```{r}
library(sfnetworks)
library(tidygraph)
library(dplyr)
library(sf)
```
# Example with package data
Test the package is working:
```{r, eval=TRUE}
net = as_sfnetwork(roxel, directed = FALSE)
class(net)
sf::st_crs(net)
net_proj = sf::st_transform(net, 3035)
p1 = net_proj %>%
activate(nodes) %>%
st_as_sf() %>%
slice(1)
p2 = net_proj %>%
activate(nodes) %>%
st_as_sf() %>%
slice(9)
p3 = sf::st_sfc(
sf::st_geometry(p1)[[1]] + sf::st_point(c(500, 500)),
crs = sf::st_crs(p1)
)
p4 = sf::st_sfc(
sf::st_geometry(p2)[[1]] + sf::st_point(c(-500, -500)),
crs = sf::st_crs(p2)
)
net_proj %>%
activate("edges") %>%
mutate(weight = edge_length()) %>%
convert(to_spatial_shortest_paths, p3, p4) ->
net_sp
par(mar = c(1,1,1,1), bg = NA)
plot(net_proj)
plot(net_sp,
col = "Orange", lwd = 1.5, cex = 1.5,
add = T)
```
# Example with data from Lisbon
```{r}
library(sfnetworks)
library(tidygraph)
library(dplyr)
library(sf)
r = slopes::lisbon_road_segments
sf::st_is_longlat(r)
class(r)
names(r)
plot(r["Avg_Slope"])
net = as_sfnetwork(r)
p1 = net %>%
activate(nodes) %>%
st_as_sf() %>%
slice(1)
p2 = net %>%
activate(nodes) %>%
st_as_sf() %>%
slice(9)
#mapview::mapview(p1) + mapview::mapview(p2)
path1 = net %>%
activate("edges") %>%
mutate(weight = edge_length()) %>%
convert(to_spatial_shortest_paths, p1, p2)
plot(path1)
#mapview::mapview(st_as_sf(path1)) +
# mapview::mapview(p1) + mapview::mapview(p2)
# Change the weight so that it is a product of edge_length and average slope.
# I am not sure how to access the edge columns to see if this worked
path2 = net %>%
activate("edges") %>%
mutate(weight = edge_length() * Avg_Slope) %>%
convert(to_spatial_shortest_paths, p1, p2)
plot(path2)
# plot to see difference in routes
#plot(net, col = "lightgrey") # How do we plot this with the Avg_Slope as a variable
plot(r["Avg_Slope"], reset = F, lwd=5)
plot(path1, add=T, lwd = 3, col = "red")
```
```{r}
plot(r["Avg_Slope"], reset = F, lwd=5)
# less hilly route
plot(path2, add=T, col="green", lwd = 3)
plot(p1, add=T, col="darkred")
plot(p2, add=T, col="darkred")
# lets try with an UNDIRECTED GRAPH (results are different )
net_und = as_sfnetwork(r, directed=F)
path1_und = net_und %>%
activate("edges") %>%
mutate(weight = edge_length()) %>%
convert(to_spatial_shortest_paths, p2, p1)
plot(path1_und)
# Change the weight so that it is a product of edge_length and average slope.
# I am not sure how to access the edge columns to see if this worked
path2_und = net_und %>%
activate("edges") %>%
mutate(weight = edge_length() * Avg_Slope) %>%
convert(to_spatial_shortest_paths, p2, p1)
plot(path2_und)
plot(r["Avg_Slope"], reset = F, lwd=3)
plot(path1_und, add=T, col="red")
plot(path2_und, add=T, col="green")
plot(p1, add=T, col="darkred")
plot(p2, add=T, col="darkred")
```
# Next step
Gradient deterrence function.
## Shortest path in Lisbon with sfnetworks
```{r}
```
```{r, eval=FALSE, echo=FALSE}
v = sf::st_coordinates(r)
nrow(v)
set.seed(5)
p = v[sample(nrow(v), size = 3), ]
head(p)
l = od::points_to_odl(p[, 1:2], crs = st_crs(r), interzone_only = TRUE)
net = as_sfnetwork(r)
net_t = net %>%
activate("edges") %>%
dplyr::mutate(length = sf::st_length(.))
class(net_t)
igraph::shortest_paths(graph = net_t, 1, 200)$vpath
l_start_points = lwgeom::st_startpoint(l)
l_end_points = lwgeom::st_endpoint(l)
```
```{r, echo=FALSE, eval=FALSE}
sln = stplanr::SpatialLinesNetwork(r)
r1 = stplanr::route_local(sln = sln, from = l_start_points[1], to = l_end_points[2])
plot(r1$geometry, col = "red", lwd = 5)
mapview::mapview(r1)
# calculate shortest paths
sp = stplanr::route(
l = l,
route_fun = stplanr::route_local,
sln = sln
)
sln = stplanr::SpatialLinesNetwork(r)
sln = stplanr::sln_clean_graph(sln)
nrow(r)
nrow(sln@sl) # simple graph
v = sf::st_coordinates(sln@sl)
nrow(v)
set.seed(8)
p = v[sample(nrow(v), size = ), ]
p = st_sample(st_convex_hull(st_union(sln@sl)), size = 3)
l = od::points_to_odl(st_coordinates(p), crs = st_crs(r), interzone_only = TRUE)
l$v = 1
l = od::od_oneway(l)
plot(sln@sl$geometry)
plot(p, add = TRUE)
```
```{r, eval=FALSE, echo=FALSE}
net_stplanr = stplanr::SpatialLinesNetwork(roxel)
isS4(net_stplanr)
slotNames(net_stplanr)
net_stplanr@g
net_stplanr@nb[1:2]
net_dodgr
```