forked from sepal-contrib/bfastspatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
675 lines (531 loc) · 25.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
####################################################################################
####### BFAST
####### SEPAL shiny application
####### FAO Open Foris SEPAL project
####### remi.dannunzio@fao.org - yelena.finegold@fao.org
####################################################################################
####################################################################################
# FAO declines all responsibility for errors or deficiencies in the database or
# software or in the documentation accompanying it, for program maintenance and
# upgrading as well as for any # damage that may arise from them. FAO also declines
# any responsibility for updating the data and assumes no responsibility for errors
# and omissions in the data provided. Users are, however, kindly asked to report any
# errors or deficiencies in this product to FAO.
####################################################################################
####################################################################################
## Last update: 2019/03/21
## bfast / server # branch for tiling internal
####################################################################################
####################################################################################
####### Start Server
shinyServer(function(input, output, session) {
####################################################################################
##################### Choose language option ###########################
####################################################################################
output$chosen_language <- renderPrint({
if (input$language == "English") {
source("www/scripts/text_english.R",
local = TRUE,
encoding = "UTF-8")
#print("en")
}
if (input$language == "Français") {
source("www/scripts/text_french.R",
local = TRUE,
encoding = "UTF-8")
#print("fr")
}
if (input$language == "Español") {
source("www/scripts/text_spanish.R",
local = TRUE,
encoding = "UTF-8")
#print("sp")
}
})
##################################################################################################################################
############### Stop session when browser is exited
session$onSessionEnded(stopApp)
##################################################################################################################################
############### Show progress bar while loading everything
options(echo=TRUE)
progress <- shiny::Progress$new()
progress$set(message = "Loading data", value = 0)
####################################################################################
####### Step 0 : read the map file and store filepath ###########################
####################################################################################
##################################################################################################################################
############### Find volumes
osSystem <- Sys.info()["sysname"]
volumes <- list()
media <- list.files("/media", full.names = T)
names(media) = basename(media)
volumes <- c(media)
volumes <- c('Home' = Sys.getenv("HOME"),
volumes)
my_zip_tools <- Sys.getenv("R_ZIPCMD", "zip")
##################################################################################################################################
## Allow to download test data
output$dynUI_download_test <- renderPrint({
req(input$download_test_button)
dir.create(file.path("~", "bfast_data_test"),showWarnings = F)
withProgress(message = paste0('Downloading data in ', dirname("~/bfast_data_test/")),
value = 0,
{
if(input$example_dataset == "Liberia example"){
system("wget -O ~/bfast_data_test/bfast_data_test.zip https://www.dropbox.com/s/1f8s15rg6er2y2g/liberia_ts_example.zip")
system("unzip -o ~/bfast_data_test/bfast_data_test.zip -d ~/bfast_data_test/")
system("rm ~/bfast_data_test/bfast_data_test.zip")
}
if(input$example_dataset == "Kenya example 2"){
system("wget -O ~/bfast_data_test/kn_ts_smallarea2_ndvi_2013_2019.zip https://www.dropbox.com/s/r9stcaaf59i4nz9/kn_ts_smallarea2_ndvi_2013_2019.zip")
system("unzip -o ~/bfast_data_test/kn_ts_smallarea2_ndvi_2013_2019.zip -d ~/bfast_data_test/ ")
system("rm ~/bfast_data_test/kn_ts_smallarea2_ndvi_2013_2019.zip")
}
if(input$example_dataset == "Kenya example 1"){
system("wget -O ~/bfast_data_test/bfast_data_kenya1_test.zip https://www.dropbox.com/s/jsxwp8rp3tlcyc2/TS_kenya_L8_2013_2018.zip")
system("unzip -o ~/bfast_data_test/bfast_data_kenya1_test.zip -d ~/bfast_data_test/ ")
system("rm ~/bfast_data_test/bfast_data_kenya1_test.zip")
}
})
list.files("~/bfast_data_test/")
})
##################################################################################################################################
############### Select input file (raster OR vector)
shinyDirChoose(
input,
'time_series_dir',
roots = volumes,
session = session,
restrictions = system.file(package = 'base')
)
##################################################################################################################################
############### Select Forest-Non Forest mask
shinyFileChoose(
input,
'mask_file',
filetype = "tif",
roots = volumes,
session = session,
restrictions = system.file(package = 'base')
)
##################################################################################################################################
############### CREATE A BUTTON THAT SHOWS ONLY IF OPTION SELECTED
output$ui_button_mask <- renderUI({
req(input$time_series_dir)
req(input$option_useMask == "FNF Mask")
shinyFilesButton(id = 'mask_file',
label = "Mask file",
title = "Browse",
multiple=F)
})
################################# Data file path
data_dir <- reactive({
validate(need(input$time_series_dir, "Missing input: Please select time series folder"))
req(input$time_series_dir)
df <- parseDirPath(volumes, input$time_series_dir)
})
################################# Display tiles inside the DATA_DIR
output$outdirpath = renderPrint({
req(input$time_series_dir)
paste0("File: ",data_dir()," Tile: ",basename(list.dirs(data_dir(),recursive = F)))
})
################################# Output directory path
mask_file_path <- reactive({
req(input$mask_file)
df <- parseFilePaths(volumes, input$mask_file)
file_path <- as.character(df[, "datapath"])
})
################################# Print selected mask file
output$print_mask_file <- renderPrint({
req(input$mask_file)
req(input$option_useMask == "FNF Mask")
df <- parseFilePaths(volumes, input$mask_file)
as.character(df[, "datapath"])
})
################################# Setup from the archives the Date Range
list_year <- reactive({
req(data_dir())
data_dir <- data_dir()
first <- list.dirs(data_dir,recursive = F)[1]
dates <- unlist(read.csv(paste0(first,'/','dates.csv'),header = FALSE))
list_year <- str_split_fixed(unlist(dates),"-",3)[,1]
# list <- list.files(data_dir,pattern = glob2rx("*_stack*.tif"),recursive = T)
# unlist(lapply(list,function(x){unlist(strsplit(x,split = "_"))[length(unlist(strsplit(x,split = "_")))-1]}))
})
################################# Take the minimum as beginning Date
beg_year <- reactive({
req(list_year())
validate(need(list_year(), "Missing time series data, make sure the data has been downloaded properly"))
min(list_year())
})
################################# Take the maximum as ending Date
end_year <- reactive({
req(list_year())
max(list_year())
})
################################# Take the average date for the beginning and end of historical period
output$ui_option_h_beg <- renderUI({
req(input$time_series_dir)
sliderInput(inputId = 'option_h_beg',
label = textOutput("text_option_h_date_break"),
min = as.numeric(beg_year()),
max = as.numeric(end_year()),
value = as.numeric(beg_year()),
sep = ""
)
})
################################# Take the average date for the beginning and end of monitoring period
output$ui_option_m_beg <- renderUI({
validate(need(input$time_series_dir, "Missing input: Please select time series folder"))
req(input$option_h_beg)
req(input$time_series_dir)
sliderInput(inputId = 'option_m_beg',
label = textOutput("text_option_m_date_break"),
min = as.numeric(input$option_h_beg),
max = as.numeric(end_year()),
value = c((as.numeric(input$option_h_beg) + as.numeric(end_year()))/2,as.numeric(end_year())),
sep = ""
)
})
################################# OPTION ORDER
output$ui_option_order <- renderUI({
req(input$time_series_dir)
selectInput(inputId = 'option_order',
label = "Order parameter",
choices = 1:5,
selected = 1
)
})
################################# OPTION HISTORY
output$ui_option_history <- renderUI({
req(input$time_series_dir)
selectInput(inputId = 'option_history',
label = "History parameter",
choices = c("ROC", "BP", "all",as.numeric(beg_year())),
selected = "ROC"
)
})
################################# OPTION TYPE
output$ui_option_type <- renderUI({
req(input$time_series_dir)
selectInput(inputId = 'option_type',
label = "Type parameter",
choices = c("OLS-CUSUM", "OLS-MOSUM", "RE", "ME","fluctuation"),
selected = "OLS-CUSUM"
)
})
################################# OPTION FORMULA
output$ui_option_formula <- renderUI({
req(input$time_series_dir)
selectInput(inputId = 'option_formula',
label = "Elements of the formula",
choices = c("harmon","trend"),
multiple = TRUE,
selected = "harmon"
)
})
################################# OPTION LAYERS
output$ui_option_returnLayers <- renderUI({
req(input$time_series_dir)
selectInput(inputId = 'option_returnLayers',
label = "Raster band outputs",
choices = c("breakpoint", "magnitude", "error", "history", "r.squared", "adj.r.squared", "coefficients"),
multiple = TRUE,
selected = c("breakpoint", "magnitude", "error")
)
})
################################# OPTION MODE
output$ui_option_sequential <- renderUI({
req(input$time_series_dir)
selectInput(inputId = "option_sequential",
label = "Computation mode",
choices = c("Overall","Sequential"),
selected= "Overall"
)
})
################################# OPTION MASK
output$ui_option_useMask <- renderUI({
req(input$time_series_dir)
# req(input$mask_file)
selectInput(inputId = "option_useMask",
label = "Use a Forest/Non-Forest mask? (Optional)",
choices = c("No Mask","FNF Mask"),#,"Sequential"),
selected= "No Mask"
)
})
################################# OPTION TILES
output$ui_tiles <- renderUI({
req(input$time_series_dir)
selectInput(inputId = "option_tiles",
label = "Which folders do you want to process?",
choices = basename(list.dirs(data_dir(),recursive = F)),
selected= basename(list.dirs(data_dir(),recursive = F)),
multiple = TRUE
)
})
################################# OPTION CHUNK SIZE
output$ui_option_chunk <- renderUI({
req(input$time_series_dir)
selectInput(inputId = "option_chunk",
label = "Processing chunk size",
choices = c(128,256,512,1024),#,"Sequential"),
selected= 512
)
})
##################################################################################################################################
############### Parameters title as a reactive
parameters <- reactive({
req(input$time_series_dir)
req(input$bfastStartButton)
data_dir <- paste0(data_dir(),"/")
historical_year_beg <- as.numeric(input$option_h_beg)
monitoring_year_beg <- as.numeric(input$option_m_beg)[1]
monitoring_year_end <- as.numeric(input$option_m_beg)[2]
order <- as.numeric(input$option_order)
history <- as.character(input$option_history)
mode <- as.character(input$option_sequential)
type <- as.character(input$option_type)
mask <- as.character(input$option_useMask)
formula_elements <- unlist(input$option_formula)
returnLayers <- c(as.character(input$option_returnLayers))
#chunk_size <- as.numeric(input$option_chunk)
type_num <- c("OC","OM","R","M","f")[which(c("OLS-CUSUM", "OLS-MOSUM", "RE", "ME","fluctuation")==type)]
mask_opt <- c("","_msk")[which(c("No Mask","FNF Mask")==mask)]
formula <- paste0("response ~ ",paste(formula_elements,sep = " " ,collapse = "+"))
if(input$option_useMask == "FNF Mask"){
mask_file_path <- mask_file_path()
}else{ mask_file_path <- ""}
title <- paste0("O_",order,
"_H_",paste0(history,collapse = "-"),
"_T_",type_num,
"_F_",paste0(substr(formula_elements,1,1),collapse= ""),
mask_opt,'_',
mode,'_',
#chunk_size,'_',
historical_year_beg,'_',monitoring_year_beg,'_',monitoring_year_end)
tiles <- input$option_tiles
rootdir <- paste0(path.expand("~"),"/")
username <- unlist(strsplit(rootdir,"/"))[3]
res_dir <- paste0(rootdir,'bfast_results/',basename(data_dir),"_",username,"_PARAM_",title,"/")
dir.create(res_dir,recursive = T,showWarnings = F)
save(rootdir,data_dir,res_dir,historical_year_beg,monitoring_year_end,monitoring_year_beg,order,history,mode,
#chunk_size,
type,formula_elements,type_num,formula,title,tiles,mask,mask_opt,mask_file_path,returnLayers,
file = paste0(res_dir,"my_work_space.RData"))
title
})
##################################################################################################################################
############### Result storage directory name as a reactive
res_dir <- reactive({
req(data_dir())
req(parameters())
rootdir <- paste0(path.expand("~"),"/")
username <- unlist(strsplit(rootdir,"/"))[3]
data_dir <- paste0(data_dir(),"/")
title <- parameters()
res_dir <- paste0(rootdir,'bfast_results/',basename(data_dir),"_",username,"_PARAM_",title,"/")
})
##################################################################################################################################
############### Insert the start button
output$StartButton <- renderUI({
req(input$time_series_dir)
validate(need(input$option_tiles, "Missing input: Please select at least one folder to process"))
validate(need(input$option_formula, "Missing input: Please select at least one element in the formula"))
validate(need(input$option_returnLayers, "Missing input: Please select at least one layer to export"))
actionButton('bfastStartButton', textOutput('start_button'))
})
##################################################################################################################################
############### Insert the display button only if in OVERALL mode
output$DisplayButtonCurrent <- renderUI({
req(input$time_series_dir)
req(input$bfastStartButton)
req(mode() == "Overall")
validate(need(input$option_tiles, "Missing input: Please select at least one folder to process"))
validate(need(input$option_formula, "Missing input: Please select at least one element in the formula"))
validate(need(input$option_returnLayers, "Missing input: Please select at least one layer to export"))
actionButton('bfastDisplayButton_c', textOutput('display_button_c'))
})
##################################################################################################################################
############### Insert the display button
output$DisplayButtonAvailable <- renderUI({
actionButton('bfastDisplayButton_a', textOutput('display_button_a'))
})
##################################################################################################################################
############### Insert the refresh button
output$RefreshButton <- renderUI({
actionButton('refresh_a', textOutput('text_refresh_a'))
})
##################################################################################################################################
############### Set the progress file as a reactive of the parameters
progress_file <- reactive({
req(input$time_series_dir)
req(parameters())
paste0(res_dir(),"processing_",parameters(),".txt")
})
##################################################################################################################################
############### SET A REACTIVE VARIABLE TO MONITOR STATUS
dis_result <- reactiveValues()
mode <- reactive({
req(input$time_series_dir)
as.character(input$option_sequential)
})
##################################################################################################################################
############### Run BFAST
bfast_res <- eventReactive(input$bfastStartButton,
{
req(input$time_series_dir)
req(input$bfastStartButton)
data_dir <- paste0(data_dir(),"/")
res_dir <- paste0(res_dir(),"/")
progress_file <- progress_file()
parameters <- parameters()
system(paste0('echo "Preparing data..." > ', progress_file))
system(paste0("nohup Rscript www/scripts/bfast_run_nochunk.R ",data_dir,' ',progress_file,' ',res_dir,' & '))
#system(paste0("nohup Rscript www/scripts/bfast_run_chunks_warp.R ",data_dir,' ',progress_file,' ',res_dir,' & '))
#system(paste0("nohup Rscript www/scripts/bfast_run_chunks_translate.R ",data_dir,' & '))
print("done")
})
#############################################################
# Progress monitor function
output$print_PROGRESS = renderText({
invalidateLater(1000)
req(bfast_res())
progress_file <- progress_file()
if(file.exists(progress_file)){
NLI <- as.integer(system2("wc", args = c("-l", progress_file," | awk '{print $1}'"), stdout = TRUE))
NLI <- NLI + 1
invalidateLater(1000)
paste(readLines(progress_file, n = NLI, warn = FALSE), collapse = "\n")
}
else{
invalidateLater(2001)
}
})
#############################################################
## list of available results to display
available_results <- eventReactive(input$refresh_a,{
list.files(path= paste0(paste0(path.expand("~"),"/"),'bfast_results/'), pattern = "_threshold.vrt$", recursive = TRUE)
})
################################# LIST OF AVAILABLE RESULTS
output$list_thres <- renderUI({
fullpaths <- available_results()
names(fullpaths) <- basename(available_results())
selectInput(inputId = "results_thres",
label = "List of processed folders",
choices = fullpaths
)
})
################################# CHECK AVAILABLE RESULTS
observeEvent(input$bfastDisplayButton_a, {
dis_result$a <- paste0(paste0(path.expand("~"),"/"),'bfast_results/',input$results_thres)
})
################################# DISPLAY THIS SESSION RESULTS BUTTON
observeEvent(input$bfastDisplayButton_c, {
req(bfast_res())
res_dir <- res_dir()
load(paste0(res_dir(),"/my_work_space.RData"))
if(mode == "Overall"){ dis_result$c <- paste0(res_dir,"results_",title,"_threshold.tif")}
if(mode == "Sequential"){ dis_result$c <- NULL}
})
############### Display the CURRENT results as map
output$display_res <- renderLeaflet({
#req(input$time_series_dir)
req(bfast_res())
print('Check: Display the map')
if (is.null(dis_result$c)) return()
print(dis_result$c)
############### READ THE RESULT AS RASTER-FACTOR
rf <- as.factor(raster(dis_result$c))
print(rf)
print(levels(rf))
lab <- factor(c("Nodata","No change",
"Small negative","Medium negative","Large negative","Very large negative",
"Small positive","Medium positive","Large positive","Very large positive"))
colorspal <- factor(c("#000000","#fdfdfd",
"#f8ffa3","#fdc980","#e31a1c","#a51013",
"#c3e586","#96d165","#58b353","#1a9641"))
pal <- colorNumeric(c("#000000","#fdfdfd",
"#f8ffa3","#fdc980","#e31a1c","#a51013",
"#c3e586","#96d165","#58b353","#1a9641"),
c(0,values(rf)),
na.color = "transparent")
m <- leaflet() %>% addTiles() %>%
addProviderTiles('Esri.WorldImagery') %>%
addProviderTiles("CartoDB.PositronOnlyLabels")%>%
addRasterImage(rf, colors = pal, opacity = 0.8, group='Results') %>%
## LEGEND is not working -- to be fixed
# addLegend(colors=colorspal,
# values = values(rf),
# title = "BFAST results"
# ,labels= lab) %>%
addLayersControl(
overlayGroups = c("Results"),
options = layersControlOptions(collapsed = FALSE)
)
})
############### Display the AVAILABLE results as map
output$display_available_res <- renderLeaflet({
req(input$bfastDisplayButton_a)
print('Check: Display the map')
if (is.null(dis_result$a)) return()
print(dis_result$a)
############### READ THE RESULT AS RASTER-FACTOR
rf <- as.factor(raster(dis_result$a))
print(rf)
print(levels(rf))
lab <- factor(c("Nodata","No change",
"Small negative","Medium negative","Large negative","Very large negative",
"Small positive","Medium positive","Large positive","Very large positive"))
colorspal <- factor(c("#000000","#fdfdfd",
"#f8ffa3","#fdc980","#e31a1c","#a51013",
"#c3e586","#96d165","#58b353","#1a9641"))
pal <- colorNumeric(c("#000000","#fdfdfd",
"#f8ffa3","#fdc980","#e31a1c","#a51013",
"#c3e586","#96d165","#58b353","#1a9641"),
c(0,values(rf)),
na.color = "transparent")
m <- leaflet() %>% addTiles() %>%
addProviderTiles('Esri.WorldImagery') %>%
addProviderTiles("CartoDB.PositronOnlyLabels")%>%
addRasterImage(rf, colors = pal, opacity = 0.8, group='Results') %>%
addLegend(colors=colorspal,
values = values(rf),
title = "BFAST results"
,labels= lab) %>%
addLayersControl(
overlayGroups = c("Results"),
options = layersControlOptions(collapsed = FALSE)
)
})
##################################################################################################################################
############### Display parameters
output$parameterSummary <- renderText({
req(input$time_series_dir)
print(paste0("Parameters are : ",parameters()))
})
##################################################################################################################################
############### Display parameters
# You can access the value of the widget with input$num, e.g.
output$num_class <- renderPrint({ input$num_class })
##################################################################################################################################
############### Processing time as reactive
process_time <- reactive({
req(bfast_res())
#
# log_filename <- list.files(data_dir(),pattern="log",recursive = T)[1]
# print(paste0(data_dir(),"/",log_filename))
# readLines(paste0(data_dir(),"/",log_filename))
})
##################################################################################################################################
############### Display time
output$message <- renderText({
#req(bfast_res())
# print("processing time")
# process_time()
})
##################################################################################################################################
############### Turn off progress bar
progress$close()
################## Stop the shiny server
####################################################################################
})