-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
466 lines (433 loc) · 16.9 KB
/
server.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# constant like vars. and api keys
readRenviron(".Renviron")
apikey <- Sys.getenv("apikey")
cycle_api <- Sys.getenv("cycle_api")
# graph_hopper <- Sys.getenv("graph_hopper") # not in use
openrouteservice::ors_api_key("58d904a497c67e00015b45fc441d07cea9f541b39ef5f7f66ebc4209")
help <- TRUE
# Define server logic
server <- function(input, output, session) {
# reactive values-------------------------------------------------------------
values <- reactiveValues(df = NULL)
tmp_route <- reactiveValues(df = NULL) # route as sf
elevPoints <- reactiveValues(df = NULL)
elevPoints_route <- reactiveValues(df = NULL)
pKm <- reactiveValues(df = NULL)
weatherdata <- reactiveValues(df = NULL)
height <- reactiveValues(df = NULL)
routed <- reactiveValues(df = NULL) # bool. - is there a routed track y/n
search <- reactiveValues(df = NULL)
search2 <- reactiveValues(df = NULL)
goweather <- reactiveValues(df = NULL) # update weather panels if trip is routed
goUpdate <- reactiveValues(df = NULL)
reach <- reactiveValues(df = NULL)
reach_tf <- reactiveValues(df = NULL)
y_sync <- reactiveValues(df = NULL)
x_sync <- reactiveValues(df = NULL)
zoom_sync <- reactiveValues(df = NULL)
route_length <- reactiveValues(df = NULL)
latest_route <- reactiveValues(df = NULL)
last_list <- reactiveValues(df = NULL)
pKm <- reactiveValues(df = NULL)
# intial values
route_counter = reactiveValues(df = NULL)
last_list$df = list()
route_counter$df = 1
route_list = list()
goweather$df <- FALSE
routed$df <- FALSE
goUpdate$df <- FALSE
reach_tf$df <- FALSE
#dynamic panels---------------------------------------------------------------
observe({
if(!is.null(input$lat)){
print(input$geolocation)
print(input$lati)
print(input$long)}
})
source("inst/modules/dynamic_ui.R")
#waypoints in adress route
observeEvent(input$waypoints,{
output$waypoints_panel <- waypoints(input$waypoints)})
#downloads
observeEvent(tmp_route$df,{
output$download <- download(tmp_route$df)})
observeEvent(input$leafmap_draw_new_feature,{
output$download <- download(tmp_route$df)})
#impression box with pictures
observeEvent(search$df,{
output$pic_box <- pic_box(hikeR::hike_scrape_pic(input$search,4), input$search)})
#-----------------------------------------------------------------------------
#help page
observeEvent(input$help, {
sendSweetAlert(
session = session,
title = "Information",
text = includeText("./test.txt"),
type = "info"
)
})
#search bar ------------------------------------------------------------------
# search observer
observeEvent(input$search,{
print(input$search)
if(nchar(input$search)==0){
search$df <- search2$df # stops app from crashing when clearing prev. search
} else {
search$df <- hikeR::hike_search_plc(input$search)
print(search$df)
if(search$df == TRUE){
search$df <- search2$df
sendSweetAlert(
session = session,
title = "Error...",
text = paste0(input$search, " is not a Valid adress"),
type = "error"
)
}
search2$df <- search$df
weatherdata$df <- hikeR::hike_weather(search$df)
}
})
# wetterwarnungen ------------------------------------------------------------
#source("inst/modules/warnungen.R")
observeEvent(input$warnungen, {
useSweetAlert()
withProgress(message = "collecting warnings", value = 0.2,{
if(input$warnungen == TRUE){
incProgress(amount = 0.1)
w <- hikeR::hike_warnungen()
incProgress(amount = 0.4)
if(class(w)[1] != "sf"){
incProgress(amount = 0.2)
sendSweetAlert(
session = session,
title = "Information",
text = "No weather warnings for Germany today",
type = "success"
)
} else {
incProgress(amount = 0.4)
pal <- colorFactor(palette = viridisLite::viridis(n = length(levels(w$EVENT))),
domain = w$EVENT,alpha = T)
content <- paste(
"<b>","Art: ","</b>", w$EVENT,"<br>",
"<b>","Desc.: ","</b>", w$DESCRIPTION,"<br>",
"<b>","Detail: ","</b>", w$PARAMATERVALUE,"<br>",
"<b>","Sent: ","</b>" ,w$SENT,"<br>",
"<b>","Source: ","</b>", w$WEB)
leafletProxy("leafmap", data = w) %>%
addPolygons(fillColor = ~pal(EVENT),fillOpacity = 0.6,stroke = F,
popup = content) %>%
setView(lng = 11 ,lat = 50 ,zoom = 6)
}
}
else{
incProgress(amount = 0.4)
leafletProxy("leafmap") %>%
clearShapes()
}
})
})
# mapping --------------------------------------------------------------------
# base leaflet with search function
source("inst/modules/base_map.R")
output$leafmap <- renderLeaflet(base_map(search$df, input$detail))
# sync both maps
observeEvent(input$leafmap_bounds,{
x_sync$df = (input$leafmap_bounds$east + input$leafmap_bounds$west)/2
y_sync$df = (input$leafmap_bounds$north + input$leafmap_bounds$south)/2
zoom_sync$df = input$leafmap_zoom
})
observeEvent(input$search,{
x_sync$df = search$df[1]
y_sync$df = search$df[2]
zoom_sync$df = input$leafmap_zoom
})
# base map for in reach
output$leafmap_reach <- renderLeaflet({reach_base_map(search$df,
input$detail,x_sync$df,y_sync$df,
zoom_sync$df)
})
# map the routed trip --> map tmp_route
observeEvent(tmp_route$df,{
if(!is.null(tmp_route$df)){
if(round(route_length$df) < 15){
zoom = 13
}
else if(round(route_length$df) > 15 & round(route_length$df) < 40){
zoom = 11
}
else if(round(route_length$df) > 40 & round(route_length$df) < 150){
zoom = 9
}
else {
zoom = 7
}
view <- st_as_sf(tmp_route$df) %>%
st_centroid() %>%
st_coordinates()
leafletProxy("leafmap", data = tmp_route$df) %>%
clearShapes() %>% addPolylines(color = "Black",
highlightOptions = highlightOptions(bringToFront = T,
stroke =T,
weight = 5,
color = "red")) %>%
setView(lng = view[,1],lat = view[,2] , zoom = zoom)
} else {
leafletProxy("leafmap", data = c()) %>%
clearShapes()
}
})
# map the polys from in reach-------------------------
observe({
if(!is.null(reach$df)){
in_reach <- TRUE
leafletProxy("leafmap_reach", data = reach$df) %>%
clearShapes() %>% addPolygons( highlightOptions = highlightOptions(bringToFront = T,
stroke =T,
weight = 0,
color = "red"),
color = "white" , weight = 0,
fillColor = viridisLite::plasma(4,begin = 0.4,end = 1),
group = "Dark") %>%
addPolygons( highlightOptions = highlightOptions(bringToFront = T,
stroke =T,
weight = 0,
color = "red"),
color = "white" , weight = 0,
fillColor = viridisLite::viridis(4,begin = 0.4,end = 1),
group = "Light")
} else {
in_reach <- FALSE
leafletProxy("leafmap_reach", data = c()) %>%
clearShapes()
}
})
## hover over height diagram --> map output
observe({
eventdata <- event_data("plotly_hover", source = "routed")
if(!is.null(eventdata)){
leafletProxy("leafmap", data = eventdata) %>%
removeShape("p") %>%
addCircles(lng = eventdata[,3],lat = eventdata[,4] ,
color = "red",radius = 80, fill = "red",layerId = "p", weight = 1) %>%
setView(lng = eventdata[,3],lat = eventdata[,4] ,zoom = 14)
}
})
# map events end here --------------------------------------------------------
# input data starts here -----------------------------------------------------
#*****************************************************************************
# New Feature-----------------------------------------------------------------
#*****************************************************************************
#new feature in leafmap ------------------------------------------------------
source("inst/modules/update.R")
source("inst/modules/leafmap_new_feature.R")
observeEvent(input$leafmap_draw_new_feature,{
# pre module execution
route_length$df <- NULL
tmp_route$df <- NULL
#variables for new feature
new_draw = input$leafmap_draw_new_feature
draw_all = input$leafmap_draw_all_features
### new feature - module excution ############
values$df = leafmap_new_feature(new_draw = new_draw, draw_all = draw_all)
# update only if routed is not switched on - post procsessing
is_routed = input$routing
if(is_routed == F){ # dont double update if airline is going to be routed
update_list <- update_all(is_routed,values = values$df ,
session = session)
weatherdata$df <- update_list[[1]]
elevPoints$df <- update_list[[2]]
pKm$df <- update_list[[3]]
route_length$df <- hike_distance(values$df[1,],values$df[2,], unit = "m")
print(hike_distance(values$df[1,],values$df[2,], unit = "m"))
}
})
#new feature in leafmap_reach-------------------------------------------------
#observer leafmap 2
# pre module execution
observeEvent(input$leafmap_reach_draw_new_feature,{
reach_tf$df <- TRUE
})
observe(if(reach_tf$df){
#variables for new feature
range <- as.numeric(input$reach_time)
profile <- input$reach_plan
x <- input$leafmap_reach_draw_all_features$features[[1]]$geometry$coordinates[[2]]
y <- input$leafmap_reach_draw_all_features$features[[1]]$geometry$coordinates[[1]]
### new feature in reach - module excution ############
reach$df <- hikeR::hike_iso_create(y,x,range,profile, key = Sys.getenv("ors"))
})
# load old route ---------------------------------------------------------
# desc.
# loads previous routes, planned in this session, these are removed if the session is killed
# pre execution (i.e. null obj...)
# observeEvent(tmp_route$df,{
# print("latest_route")
# last_list$df[[route_counter$df]] = tmp_route$df
# route_counter$df = route_counter$df + 1
# if(route_counter$df == 4){ route_counter$df = 1}
#
# output$old_routes = DT::renderDataTable(data.frame(latest = paste0("last trip", 1:3)
# #length = as.numeric(route_length$df)
# ),
# server = FALSE)
#
# observeEvent(input$old_routes_rows_selected,{
# print(input$old_routes_rows_selected) # we need this if we want to save more than one route
# latest_route$df = last_list$df[[1]]
# print(tmp_route$df)
# is_routed = input$routing
# update_list <- update_all(is_routed,tmp_route = latest_route$df,session = session)
# weatherdata$df <- update_list[[1]]
# elevPoints$df <- update_list[[2]]
# # display it
# view <- st_as_sf(latest_route$df) %>%
# st_centroid() %>%
# st_coordinates()
# leafletProxy("leafmap", data = latest_route$df) %>%
# clearShapes() %>% addPolylines(color = "Black",
# highlightOptions = highlightOptions(bringToFront = T,
# stroke =T,
# weight = 5,
# color = "red")) %>%
# setView(lng = view[,1],lat = view[,2] , zoom = 12)
# })
# })
#routing module---------------------------------------------------------------
#includes update of stats and weather
#input routing
source("inst/modules/dorouting.R")
# routing interface
doRoute = function(i_routing){
observe(if(input$routing){
# routing variables
# variables preperation
# reset route button
if(input$string_route){
shinyWidgets::updateMaterialSwitch(session = session,inputId = "routing",
value = FALSE)}
# plan by provider
if(input$route_opt == "cycle"){
plan = input$plan
}
else if(input$route_opt == "ORS"){
plan = input$ors_plan
}
# waypoint list for multiple string inputs
wayp_list <- data.frame(name = paste0("to",1:input$waypoints))
j = 1
wayp_tmp <- list()
for(i in wayp_list$name){
print(i)
wayp_tmp[[j]] <- geo_code(input[[i]])
j <- j + 1 }
# all variables for routing
waypoint_list = data.frame(do.call(rbind,wayp_tmp))
is_routed = input$routing
provider = input$route_opt
string_route = input$string_route
more = input$more
from_i = input$from
to_i = input$to
values = values$df
session = session
# routing function as module - execution
tmp_route$df <- doRouting(provider = provider,plan = plan, string_route = string_route,
waypoint_list = waypoint_list, more = more,from_i = from_i,
to_i = to_i, values = values,session = session)
# update for routed track - postprocession
if(!is.null(tmp_route$df)){
route_length$df <- as.numeric(sf::st_length(tmp_route$df)/1000)
update_list <- update_all(is_routed,tmp_route = tmp_route$df,session = session)
weatherdata$df <- update_list[[1]]
elevPoints$df <- update_list[[2]]
pKm$df <- update_list[[3]]
output$traveltime <- renderTable({
if(length(route_length$df != 0)){ traveltime
} else {
NULL
}
})
}
})
}
doRoute(input$routing)
# new module example ---------------------------------------------------------
# desc.
# pre execution (i.e. null obj...)
# ...
# variables/variable preperation
# ...
# module execution
# ...
# module postprocessing
# ...
# outputs --------------------------------------------------------------------
# weather---------------------------------------------------------------------
source("inst//modules/weather_module.R")
observeEvent(weatherdata$df,{
output$weather <-weather_mod(weatherdata$df)
output$percip <- percip(weatherdata$df)
output$temp <- temp(weatherdata$df)
output$wind <- windspd(weatherdata$df)
output$hum <- humiditiy(weatherdata$df)
})
# elevation-------------------------------------------------------------------
# height info box (black)
source("inst/modules/elev_box.R")
observeEvent(elevPoints$df,{
elevP = elevPoints$df
h <- hikeR::hike_height_diff(elevP,col = "elev")
output$heightbox <- heigth_mod(h)
output$max <- max_box(elevP)
output$min <- min_box(elevP)
})
observeEvent(elevPoints_route$df,{
elevP = elevPoints_route$df
h <- hikeR::hike_height_diff(elevP,col = "elev")
output$heightbox <- heigth_mod(h)
output$max <- max_box(elevP)
output$min <- min_box(elevP)
})
#travel time here ------------------------------------------------------------
observeEvent({input$pace
pKm$df}, {
print(pKm$df)
output$traveltime <- renderTable({
if(!is.null(pKm$df)){
hikeR::hike_traveltime(pkm = pKm$df, speed = input$pace)
} else { NULL }
})
})
#plot outputs here -----------------------------------------------------------
source("inst/modules/elev_plot.R")
observeEvent({elevPoints$df
input$twoD},{
output$plot <- plot_air(elevPoints$df, values$df, input$twoD)})
#import and export------------------------------------------------------------
##download
# download not working 30/06/18
output$downloadData <- downloadHandler(
filename = function() {
if(input$gpx){
paste("trip", ".gpx", sep = "")
} else {
paste("trip", ".kml", sep = "")
}
},
content = function(filename) {
tmp_route$df[2] = "a"
print(class(tmp_route$df))
print(tmp_route$df)
obj = as(tmp_route$df, "Spatial")
if(input$gpx){
writeOGR(obj = obj ,dsn= filename, layer="trip",
dataset_options="GPX_USE_EXTENSIONS=yes",driver="GPX")
} else {
writeOGR(obj = obj ,dsn= filename, layer="trip",driver="KML")
}
}
)
}