-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.R
2134 lines (1823 loc) · 87 KB
/
app.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ============================================================
# Script Author: [Spaska Forteva]
# Created On: 2021-06-10c
# ============================================================
# ============================================================
# Main Shiny App Distribution Digitization
# ============================================================
#
#
# ============================================================
# # Tab 1 Config Dialog for Book Distribution Digitization
# ============================================================
# This configuration dialog is designed to gather essential
# information about a book before the digitization process.
# Users can input details to create a comprehensive summary
# for proper documentation. The dialog includes the following fields:
#
# 1. Title: Enter the title of the book.
# 2. Author: Provide the name of the book's author.
# 3. Publication Year: Specify the year the book was published.
# 4. Data Input Directory: Specify the directory where the raw
# data for the book digitization is stored.
# 5. Data Output Directory: Specify the directory where the
# digitized output for the book will be stored.
# 6. Number of Book Sites per One Print: Define the number of
# book pages to be included in one printed output.
# 7. All Printed Pages: Indicate whether all pages of the book
# will be included in the digitization process.
# 8. Site Number Position: Specify the placement of the site
# number on each printed page (e.g., top-right, bottom-left).
# 9. Image Format of the Scanned Sites: Choose the format
# (e.g., JPEG, PNG) for the digitized pages.
# 10. Page Color: Indicate the color of the book pages
# (e.g., black and white, color).
#
# This dialog aims to streamline the digitization process by
# ensuring that all relevant information is captured accurately.
# Once the user completes the form, the gathered details can
# be used for cataloging and organizing the digital version of
# the book, preserving its content for future reference.
# ============================================================
library(shiny)
library(shinydashboard)
if(!require(magick)){
install.packages("magick", dependencies = T)
library(magick)
}
if(!require(grid)){
install.packages("grid", dependencies = T)
library(grid)
}
if(!require(rdrop2)){
install.packages("rdrop2", dependencies = T)
library(rdrop2)
}
if(!require(shiny)){
install.packages("shiny",dependencies = T)
library(shiny)
}
if(!require(shinyFiles)){
install.packages("shinyFiles",dependencies = T)
library(shinyFiles)
}
if(!require(reticulate)){
install.packages("reticulate",dependencies = T)
library(reticulate)
}
library(shinyalert)
library(reticulate)
library(tesseract)
if(!require(leaflet)){
install.packages("leaflet",dependencies = T)
library(leaflet)
}
if(!require(raster)){
install.packages("raster",dependencies = T)
library(raster)
}
library(sf)
# Global variables
processEventNumber = 0
# Input variables
# host
options(shiny.host = '127.0.0.1')
# port
options(shiny.port = 8888)
# Change the max uploaf size
options(shiny.maxRequestSize=100*1024^2)
tempImage="temp.png"
scale =20
rescale= (100/scale)
workingDir <- getwd()
print("Working directory 1:")
print(workingDir)
outDir <- ""
inputDir = paste0(workingDir,"/data/input/")
#read config fields from config.csv in .../distribution_digitizer/config directory
fileFullPath = (paste0(workingDir,'/config/config.csv'))
if (file.exists(fileFullPath)){
config <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
if (file.exists(config$dataOutputDir)) {
outDir <- config$dataOutputDir
}
fileFullPath = (paste0(workingDir,'/config/configStartDialog.csv'))
if (file.exists(fileFullPath)){
configStartDialog <- read.csv(fileFullPath, header = TRUE, sep = ';')
} else{
stop("The file configStartDialog.csv was not found, please create them and start the app")
}
# read the shiny text fields
#1 shinyfields_create_templates.csv
fileFullPath = (paste0(workingDir,'/config/shinyfields_create_templates.csv'))
if (file.exists(fileFullPath)){
shinyfields1 <- read.csv(fileFullPath, header = TRUE, sep = ';')
} else{
stop("file shinyfields_create_templates.csv not found, please create them and start the app")
}
#2 shinyfields_detect_maps.csv
fileFullPath = (paste0(workingDir,'/config/shinyfields_detect_maps.csv'))
if (file.exists(fileFullPath)){
shinyfields2 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#3 shinyfields_detect_points.csv
fileFullPath = (paste0(workingDir,'/config/shinyfields_detect_points.csv'))
if (file.exists(fileFullPath)){
shinyfields3 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#4 shinyfields_detect_points_using_filtering
fileFullPath = (paste0(workingDir,'/config/shinyfields_detect_points_using_filtering.csv'))
if (file.exists(fileFullPath)){
shinyfields4 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#4.1 shinyfields_detect_points_using_circle_detection
fileFullPath = (paste0(workingDir,'/config/shinyfields_detect_points_using_circle_detection.csv'))
if (file.exists(fileFullPath)){
shinyfields4.1 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#5 shinyfields_masking
fileFullPath = (paste0(workingDir,'/config/shinyfields_masking.csv'))
if (file.exists(fileFullPath)){
shinyfields5 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#5.1 shinyfields_mask_centroids
fileFullPath = (paste0(workingDir,'/config/shinyfields_mask_centroids.csv'))
if (file.exists(fileFullPath)){
shinyfields5.1 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#6 shinyfields_georeferensing
fileFullPath = (paste0(workingDir,'/config/shinyfields_georeferensing.csv'))
if (file.exists(fileFullPath)){
shinyfields6 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
#7 shinyfields_polygonize
fileFullPath = (paste0(workingDir,'/config/shinyfields_polygonize.csv'))
if (file.exists(fileFullPath)){
shinyfields7 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
# 8 shinyfields_georef_coords_from_csv_file
fileFullPath = (paste0(workingDir,'/config/shinyfields_georef_coords_from_csv_file.csv'))
if (file.exists(fileFullPath)){
shinyfields8 <- read.csv(fileFullPath,header = TRUE, sep = ';')
} else{
stop(paste0("file:", fileFullPath, "not found, please create them and start the app"))
}
header <- dashboardHeader(
tags$li(
class = "dropdown",
tags$style(HTML("
.navbar-custom-menu{float:left !important;}
.sidebar-menu{display:flex;align-items:baseline;}
/* layout of the map images */
.shiny-map-image{margin:7px;}
#message {color: red;}
"))
),
tags$li(
class = "dropdown",
sidebarMenu(
id = "tablist",
menuItem("Environment DD", tabName = "tab0"),
menuItem("Create Templates", tabName = "tab1"),
menuItem("Maps Matching", tabName = "tab2"),
menuItem("Points Matching", tabName = "tab3"),
menuItem("Masking", tabName = "tab4" ),
menuItem("Georeferencing", tabName = "tab5" ),
menuItem("Polygonize", tabName = "tab6" ),
menuItem("Spatial View", tabName = "tab7" ),
menuItem("Download", tabName = "tab8" )
)
)
)
body <- dashboardBody(
# Top Information
# Working directory
titlePanel("Distribution Digitizer"),
p(paste0(config$workingDirInformation,": ",workingDir) , style = "color:black"),
tabItems(
# Tab 0 Config Dialog --------------------------------------------------------------------------------------------------------------
tabItem(
tabName = "tab0",
p("In ", strong("Distribution Digitizer"), " 1.0, certain lines are skipped during species name search. Lines containing special characters like",
strong("doublequotes\""), ",",strong(" point ."),", ", strong("colons :"),", or the word", strong(" \"distribution\"")," are excluded.",
br(),
"The tool also searches for species names in the legend map and uses a specified regular expression for matching the year.",
br(),
"If you've added an extra ",strong("additional keyword"),", indicate if it appears before, after, the species name."
),
fluidRow(
# Linke Spalte
column(
width = 6, # Zum Beispiel die Hälfte der Breite des Containers
wellPanel(
h3(strong("General configuration fields", style = "color:black")),
# Title: Provide the name of the book's author.
fluidRow(textInput("title", label = "Please write the title of the book.", value = config$title)),
# Author: Provide the name of the book's author.
fluidRow(textInput("author", label = "Please write the author of the book.", value = config$autor)),
# Publication Year: Specify the year the book was published.
fluidRow(textInput("pYear", label = "Publication Year", value = config$pYear)),
# Data input directory
fluidRow(textInput("dataInputDir", p("Please write the path to the inputs.",br(),
"This input directory should contain two folders:",br(),
"- 'pages', where the scanned images are stored,",br(),
"- 'templates', which includes four additional folders.",br(),br(),
"-- 'maps' ",br(),
"--- 'map_1.tif' use the next Tab: Create Templates to create this",br(),
"-- 'symbols' ",br(),
"--- 'symbol_1.tif' 'symbol_2.tif' use the next Tab: Create Templates to create this",br(),
"-- 'align_ref' ",br(),
"--- 'map_1_align.tif' make one template map properly aligned (Gimp) and save it here.",br(),
"-- 'geopoints'",br(),
"--- 'gcp_point_map1.points' geopoints data for the georeferens. you can user the program QGIS to extract this",br(),br(),
"To ensure proper functionality of the application,",
"please ensure that the provided path adheres to the aforementioned structure."),
value = config$dataInputDir), verbatimTextOutput("message"),),
# Data output directory
fluidRow(textInput("dataOutputDir", label = "Please write the path to the output environment of the distribution digitizer", value = config$dataOutputDir)),
# format
fluidRow(selectInput("pFormat", label = "Image iiFormat of the Scanned Sites", c("tif" = 1, "png" = 2, "jpg" = 3), selected = config$pFormat)),
# Page color
fluidRow(selectInput("pColor", label = "Page Color", c("black white" = 1, "color" = 2), selected = config$pColor)),
# allprintedPages
fluidRow(textInput("allPrintedPages", label = "Please provide the number of scanned images:", value = config$allPrintedPages)),
#fluidRow(selectInput("numberSitesPrint", label = "Number of Book Sites per One Print", c("One site per scan" = 1, "Two sites per scan" = 2), selected = config$numberSitesPrint)),
)
),
# Rechte Spalte
column(
width = 6, # Zum Beispiel die Hälfte der Breite des Containers
wellPanel(
h3(strong(" Additional specific configuration input fields", style = "color:black")),
# mapCaptureType
fluidRow(selectInput("mapCaptureType", label = "Select map capture type: points or contours.", c("points" = 1, "countors" = 2), selected = config$mapCaptureType)),
# site number position
fluidRow(selectInput("sNumberPosition", label = "Indicate whether the page number is positioned at the top or bottom of the page.", c("top" = 1, "botom" = 2), selected = config$sNumberPosition)),
# Is the term "species name" listed on the map (bottom)?
fluidRow(selectInput("speciesOnMap", label = "Is there a species name listed on the map?", c( "No" = 0, "Yes" = 1 ), selected = config$speciesOnMap)),
# Select which is the type of matching: template or contours
fluidRow(column(8, selectInput("matchingType", label = shinyfields2$matchingType, c("Template matching" = 1, "Countor matching" = 2), selected = config$matchingType))),
# Select species is approximately at the same level in the book as the map
fluidRow(column(8, selectInput("approximatelySpecieMap", label = "Could you check if the title/term of the species is approximately at the same level in the book as the map?", c("Yes" = 1, "Now" = 2), selected = config$approximatelySpecieMap))),
# Is the term "species name" on the page inclusive of special patterns such as year, parentheses, or symbols?
fluidRow(selectInput("middle", label = "Is the term-species shifted to the middle, indented?", c( "No" = 0, "Yes" = 1 ), selected = config$middle)),
fluidRow(selectInput("regExYear", label = "Does the term contain a regular expression like a year?", c( "No" = 0, "Yes" = 1), selected = config$regExYear)),# keayword to read species data
fluidRow(textInput("keywordReadSpecies", label = "If there is a keyword related to the term 'species', please write it here. Please note that the word should almost always appear a few lines before, after, or directly on the same line as the species term.", value = config$keywordReadSpecies)),
fluidRow(selectInput("keywordBefore", label = "Is the keyword located a few lines before the species name? If so, how many lines?", c("0" = 0, "1" = 1, "2" = 2, "3" = 3), selected = config$keywordBefore)),
fluidRow(selectInput("keywordThen", label = "Is the keyword located a few lines after the species name? If yes, please specify how many.", c("0" = 0, "1" = 1, "2" = 2, "3" = 3), selected = config$keywordThen)),
#Is the keyword a few lines before the species name? If yes, how many?
#Is the keyword exactly on the line with the species name?
#Is the keyword a few lines after the species name? If yes, please specify how many."
# Save button
fluidRow(actionButton("saveConfig", label = "Save", style = "color:#FFFFFF;background:#999999"))
)
)
)
),
# Tab 1 Create Templates #---------------------------------------------------------------------
tabItem(
tabName = "tab1",
actionButton("listMTemplates", label = "List saved map templates"),
actionButton("listSTemplates", label = "List saved symbol templates"),
fluidRow(
column(4,
wellPanel(
h3(strong(shinyfields1$head, style = "color:black")),
p(shinyfields1$inf4, style = "color:black"),
# Choose the file
fileInput("image", label = h5(shinyfields1$lab1), buttonLabel = "Browse...",
placeholder = "No file selected"),
),
wellPanel(
h4(strong(shinyfields1$save_template, style = "color:black")),
# Add number to the file name of the created template file
fluidRow(column(8, numericInput("imgIndexTemplate", label = h5(shinyfields1$lab2),value = 1),
p(strong(paste0(shinyfields1$inf, workingDir, "/data/templates/maps/"), style = "color:black")),
p(shinyfields1$inf1, style = "color:black"),
# Save the template map image with the given index
downloadButton('saveTemplate', 'Save map template', style="color:#FFFFFF;background:#999999"))),
),
wellPanel(
h4(strong(shinyfields1$save_symbol, style = "color:black")),
# Add number to the file name of the created template file
fluidRow(column(8, numericInput("imgIndexTemplate", label = h5(shinyfields1$lab2),value = 1),
p(strong(paste0(shinyfields1$inf2, workingDir, "/data/templates/symbols"), style = "color:black")),
p(shinyfields1$inf3, style = "color:black"),
# Save the template map image with the given index
downloadButton('saveSymbol', 'Save map symbol/Legende', style="color:#FFFFFF;background:#999999")))
)
), # col 4
column(8,
uiOutput('listMapTemplates', style="width:35%;float:left"),
uiOutput('listSymbolTemplates', style="width:35%;float:left"),
plotOutput("plot", click = "plot_click", # Equiv, to click=clickOpts(id="plot_click")
hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
brush = brushOpts(id = "plot_brush")),
)
) # END fluid Row
), # END tabItem 1
# Tab 2 Maps Matching #----------------------------------------------------------------------
tabItem(
tabName = "tab2",
# which site become overview
fluidRow(column(3,textInput("siteNumberMapsMatching", label=shinyfields6$input, value = ''))),
actionButton("listMapsMatching", label = "List maps"),
actionButton("listAlign", label = "List aligned maps"),
actionButton("listCropped", label = "List cropped maps"),
fluidRow(
column(4,
wellPanel(
# submit action button
h3(strong(shinyfields2$head, style = "color:black")),
p(shinyfields2$inf1, style = "color:black"),
fluidRow(column(8, numericInput("threshold_for_TM", label = shinyfields2$threshold, value = 0.2, min = 0, max = 1, step = 0.05))),
p(shinyfields2$inf2, style = "color:black"),
# Start map matching
fluidRow(column(3,actionButton("templateMatching", label = shinyfields2$start1, style="color:#FFFFFF;background:#999999"))),
),
wellPanel(
# maps align
h3(shinyfields2$head_sub, style = "color:black"),
p(shinyfields2$inf3, style = "color:black"),
fluidRow(column(3, actionButton("alignMaps", label = shinyfields2$start2, style="color:#FFFFFF;background:#999999"))),
),
# speciesOnMap
wellPanel(
# maps species
h3(shinyfields2$head_species, style = "color:black"),
p(shinyfields2$inf4, style = "color:black"),
fluidRow(column(3, actionButton("mapReadRpecies", label = shinyfields2$start3, style="color:#FFFFFF;background:#999999"))),
),
wellPanel(
# page species
h3(shinyfields2$head_page_species, style = "color:black"),
p(shinyfields2$inf5, style = "color:black"),
fluidRow(column(3, actionButton("pageReadRpecies", label = shinyfields2$start4, style="color:#FFFFFF;background:#999999"))),
)
), # col 4
column(8,
uiOutput('listMaps', style="width:30%;float:left"),
uiOutput('listAlign', style="width:30%;float:left"),
uiOutput('listCropped', style="width:30%;float:left")
)
) # END fluid Row
), # END tabItem 2
# Tab 3 Points Matching #----------------------------------------------------------------------
tabItem(
tabName = "tab3",
fluidRow(column(3,textInput("siteNumberPointsMatching", label=shinyfields6$input, value = ''))),
actionButton("listMapsMatching2", label = "List maps"),
actionButton("listPointsM", label = "List points matching"),
actionButton("listPointsF", label = "List points filterng"),
actionButton("listPointsCD", label = "List points circle detection"),
fluidRow(
column(4,
wellPanel(
h3(strong(shinyfields3$head, style = "color:black")),
p(shinyfields3$inf1, style = "color:black"),
p(shinyfields3$inf2, style = "color:black"),
),
wellPanel(
# ----------------------------------------# 3.1 Points detection Using template FILE=shinyfields_detect_points #---------------------------------------------------------------------
h4(shinyfields3$head_sub, style = "color:black"),
p(shinyfields3$inf3, style = "color:black"),
# Threshold for point matching
fluidRow(column(8,numericInput("threshold_for_PM", label = shinyfields3$threshold, value = 0.87, min = 0, max = 1, step = 0.05))),
p(shinyfields3$inf4, style = "color:black"),
fluidRow(column(3, actionButton("pointMatching", label = shinyfields3$lab, style="color:#FFFFFF;background:#999999"))),
),
wellPanel(
# ----------------------------------------# 3.2 Points detection Using filtering FILE=shinyfields_detect_points_using_filtering #---------------------------------------------------
h4(shinyfields4$head, style = "color:black"),
fluidRow(column(8,numericInput("filterK", label = shinyfields4$lab1, value = 5))),#, width = NULL, placeholder = NULL)
p(shinyfields4$inf1, style = "color:black"),
fluidRow(column(8,numericInput("filterG", label = shinyfields4$lab2, value = 9))),#, width = NULL, placeholder = NULL)
p(shinyfields4$inf2, style = "color:black"),
fluidRow(column(3, actionButton("pointFiltering", label = shinyfields4$lab3, style="color:#FFFFFF;background:#999999"))),
),
wellPanel(
# ----------------------------------------# 3.3 Points detection Using circle detection FILE=shinyfields_detect_points_using_circle_detection #------------------------------------
h4(shinyfields4.1$head, style = "color:black"),
fluidRow(column(8,numericInput("Gaussian", label = shinyfields4.1$lab1, value = 5, min = 0))),
p(shinyfields4.1$inf1, style = "color:black"),
fluidRow(column(8,numericInput("minDist", label = shinyfields4.1$lab2, value = 1, min = 0))),
p(shinyfields4.1$inf2, style = "color:black"),
fluidRow(column(8,numericInput("thresholdEdge", label = shinyfields4.1$lab3, value = 100, min = 0))),
p(shinyfields4.1$inf3, style = "color:black"),
fluidRow(column(8,numericInput("thresholdCircles", label = shinyfields4.1$lab4, value = 21, min = 0))),
p(shinyfields4.1$inf4, style = "color:black"),
fluidRow(column(8,numericInput("minRadius", label = shinyfields4.1$lab5, value = 3, min = 0))),
p(shinyfields4.1$inf5, style = "color:black"),
fluidRow(column(8,numericInput("maxRadius", label = shinyfields4.1$lab6, value = 12, min = 0))),
p(shinyfields4.1$inf6, style = "color:black"),
p(shinyfields4.1$inf7, style = "color:black"),
fluidRow(column(3, actionButton("pointCircleDetection", label = shinyfields4.1$lab7, style="color:#FFFFFF;background:#999999"))),
)
), # col 4
column(8,
uiOutput('listMapsMatching2', style="width:25%;float:left"),
uiOutput('listPM', style="width:25%;float:left"),
uiOutput('listPF', style="width:25%;float:left"),
uiOutput('listPCD', style="width:25%;float:left")
)
) # END fluid Row
), # END tabItem 3
# Tab 4 Masking #----------------------------------------------------------------------
tabItem(
tabName = "tab4",
fluidRow(column(3,textInput("siteNumberMasks", label=shinyfields6$input, value = ''))),
actionButton("listMasks", label = "List masks"),
actionButton("listMasksB", label = "List black masks"),
actionButton("listMasksCD", label = "List CD masks"),
# actionButton("listMPointsF", label = "List points filterng"),
fluidRow(
column(4,
wellPanel(
h3(strong(shinyfields5$head, style = "color:black"))
),
wellPanel(
# ----------------------------------------# 4. 1 Masking (white)#----------------------------------------------------------------------
h3(shinyfields5$head_sub, style = "color:black"),
h4("You can extract masks with white background", style = "color:black"),
p(shinyfields5$inf1, style = "color:black"),
# p(shinyfields7$inf2, style = "color:black"),
fluidRow(column(8,numericInput("morph_ellipse", label = shinyfields5$lab1, value = 5))),#, width = NULL, placeholder = NULL)
fluidRow(column(3, actionButton("masking", label = shinyfields5$lab2, style="color:#FFFFFF;background:#999999"))),
),
#wellPanel(
# ----------------------------------------# Masking (black)#----------------------------------------------------------------------
# h4("Or you can extract masks with black background", style = "color:black"),
# p(shinyfields5$inf2, style = "color:black"),
# fluidRow(column(8,numericInput("morph_ellipse", label = shinyfields5$lab1, value = 5))),#, width = NULL, placeholder = NULL)
# fluidRow(column(3, actionButton("maskingBlack", label = shinyfields5$lab2, style="color:#FFFFFF;background:#999999"))),
# ),
# ---------------------------------------- # 4.2 Masking centroids -----------------------------------------------------------------
wellPanel(
h3(shinyfields5.1$head_sub, style = "color:black"),
h4("You can mask the centroids of the points detected by Point Filtering and Circle Detection.", style = "color:black"),
p(shinyfields5.1$inf1, style = "color:black"),
fluidRow(column(3, actionButton("maskingCentroids", label = shinyfields5.1$lab1, style="color:#FFFFFF;background:#999999")))
)
), # col 4
column(8,
uiOutput('listMS', style="float:left"),
uiOutput('listMSB', style="float:left"),
uiOutput('listMCD', style="float:left")
)
) # END fluid Row
), # END tabItem 4
# Tab 5 Georeferencing FILES=shinyfields_georeferensing & shinyfields_georef_coords_from_csv_file.csv #-------------------------------------------------
tabItem(
tabName = "tab5",
wellPanel(
h3(strong(shinyfields6$head, style = "color:black")),
p(shinyfields6$inf1, style = "color:black"),
p(shinyfields6$inf2, style = "color:black"),
# start georeferencing
actionButton("georeferencing", label = shinyfields6$lab1, style="color:#FFFFFF;background:#999999")
),
wellPanel(
# which site become overview
fluidRow(column(3,textInput("siteNumberGeoreferencing", label=shinyfields6$input, value = ''))),
# start overview
actionButton("listGeoreferencing", label = "List georeferenced files"),
),
wellPanel(
uiOutput('leaflet_outputs_GEO')
),
wellPanel(
h4(shinyfields8$head_sub, style = "color:red"),
p(shinyfields8$info1, style = "color:black"),
p(shinyfields8$info2, style = "color:black"),
actionButton("georef_coords_from_csv", label = shinyfields8$lab1, style="color:#FFFFFF;background:#999999")
)
# END fluid Row
),# END tabItem 5
# Tab 6 Polygonize FILE=shinyfields_polygonize #----------------------------------------------------------------------
tabItem(
tabName = "tab6",
wellPanel(
h3(strong(shinyfields7$head, style = "color:black")),
p(shinyfields7$inf1, style = "color:black"),
p(shinyfields7$inf2, style = "color:black"),
actionButton("polygonize", label = shinyfields7$lab1, style="color:#FFFFFF;background:#999999")
),
wellPanel(
# which site become overview
fluidRow(column(3,textInput("siteNumberPolygonize", label=shinyfields6$input, value = ''))),
actionButton("listPolygonize", label = "Listf polygonized files",),
),
wellPanel(
uiOutput("leaflet_outputs_PL")
)
), # END tabItem 6
# 7. Spatial data view #----------------------------------------------------------------------
tabItem(
tabName = "tab7",
# wellPanel(
# h3(strong("Save the outputs in csv file", style = "color:black")),
# p("hier kommt noch mehr Text", style = "color:black"),
# p("hier kommt noch mehr Text", style = "color:black"),
actionButton("startSpatialDataComputing", label ="Spatial Data Computing", style="color:#FFFFFF;background:#999999"),
#),
wellPanel(
# which site become overview
#fluidRow(column(3,textInput("siteNumberSave", label="Test", value = ''))),
actionButton("spatialViewCD", label = "Start View circle detection",),
leafletOutput("mapSpatialViewCD"),
verbatimTextOutput("hoverInfo")
),
wellPanel(
# which site become overview
#fluidRow(column(3,textInput("siteNumberSave", label="Test", value = ''))),
actionButton("spatialViewPF", label = "Start View point detection",),
leafletOutput("mapSpatialViewPF"),
verbatimTextOutput("hoverInfo3")
)
), # END tabItem 6
tabItem(
tabName = "tab8",
actionButton("viewCSV", label ="Overview spatial final data", style="color:#FFFFFF;background:#999999"),
wellPanel(
#downloadButton("downloadCSV", label = "Download CSV", style="color:#FFFFFF;background:#999999"),
downloadButton("download_csv", label = "Download CSV", style="color:#FFFFFF;background:#999999"),
dataTableOutput("view_csv")
),
)
) # END tabItems
) # END BODY
sidebar <-
ui <- dashboardPage(
header = header,
sidebar = dashboardSidebar(disable = TRUE),
body = body,
title = NULL,
skin = "black"
)
################################################################################
# Shiny SERVER CODE
################################################################################
################################################################################
server <- shinyServer(function(input, output, session) {
dataInputDir = ""
# Update the clock every second using a reactiveTimer
current_time <- reactiveTimer(1000)
# SAVE the config
observeEvent(input$saveConfig, {
dataInputDir = input$dataInputDir
tryCatch({
# Check if the directory exists
if (file.exists(dataInputDir) ){#&& file.info(dataInputDir)$isdir) {
output$message <- NULL
# List of subdirectories in the "input" directory
subdirectories <- list.dirs(dataInputDir, full.names = FALSE, recursive = FALSE)
# Required subdirectories
required_subdirectories <- c("pages", "templates")
#required_subdirectories <- c("maps", "symbols", "align", "points")
# Check if all required subdirectories are present
if (all(required_subdirectories %in% subdirectories)) {
print("All required subdirectories of input are present.")
templates <- paste0(dataInputDir,"/templates/")
# List of subdirectories in the "input" directory
subdirectories <- list.dirs(templates, full.names = FALSE, recursive = FALSE)
# Required subdirectories
required_subdirectories <- c("align_ref", "maps","symbols", "geopoints")
#required_subdirectories <- c("maps", "symbols", "align", "points")
if (all(required_subdirectories %in% subdirectories)) {
print("All required subdirectories of templates are present.")
} else {
missing_subdirectories <- required_subdirectories[!required_subdirectories %in% subdirectories]
output$message <- renderPrint(paste("The following subdirectories in templates are missing:", paste(missing_subdirectories, collapse = ", ")))
break
}
} else {
missing_subdirectories <- required_subdirectories[!required_subdirectories %in% subdirectories]
output$message <- renderPrint(paste("The following subdirectories are missing:", paste(missing_subdirectories, collapse = ", ")))
break
}
} else {
#rv$message <- "The 'templates' directory does not exist."
output$message <- renderPrint("The 'templates' directory does not exist.")
break
}
# Generate current date-time string
current_datetime <- format(Sys.time(), "%Y-%m-%d_%H-%M-%S")
# Directory name with current date-time
out_directory_name <- paste("output_", current_datetime, sep = "")
# Concatenate base path with directory name
outDir <- file.path(input$dataOutputDir, out_directory_name)
prepare_base_output(outDir)
webViewMap = paste0(workingDir,"/www/data/")
prepare_www_output(webViewMap)
x <- data.frame(workingDir= workingDir,
workingDirInformation = "Your working directory is the local digitizer repository!",
title = input$title,
autor = input$author,
pYear = input$pYear,
dataInputDir= input$dataInputDir,
dataOutputDir = outDir,
allPrintedPages = input$allPrintedPages,
sNumberPosition = input$sNumberPosition,
# special config data, in relation with the book Moths of Europe
mapCaptureType = input$mapCaptureType,
speciesOnMap = input$speciesOnMap,
matchingType = input$matchingType,
approximatelySpecieMap = input$approximatelySpecieMap,
middle = input$middle,
regExYear = input$regExYear,
keywordReadSpecies = input$keywordReadSpecies,
keywordBefore = input$keywordBefore,
keywordThen = input$keywordThen,
pFormat = input$pFormat,
pColor = input$pColor)
tf <- tempfile(fileext = ".csv")
## To write a CSV file for input to Excel one might use
write.table(x, file = paste0(workingDir,"/config/config.csv"), sep = ";", row.names = FALSE,
quote=FALSE)
shinyalert::shinyalert(title = "Success", text = "Configuration successfully saved!", type = "success")
},
error = function(e) {
errorMessage <- paste("Ein Fehler ist aufgetreten:", e$message)
stackTrace <- paste("Stapelrückverfolgung:", traceback())
shinyalert::shinyalert(title = "Fehler", text = c(errorMessage, stackTrace), type = "error")
})
})
# -----------------------------------------# 1. Step - Create templates #---------------------------------------------------------------------
#Function to show the ccrop process in the app
plot_png <- function(path, plot_brush, index, add=FALSE)
{
require('png')
#fname=paste0(workingDir, "/", tempImage)
fname=tempImage
png = png::readPNG(fname, native=T) # read the file
# this for tests png <- image_read('DD_shiny/0045.png')
# get the resolution, [x, y]
res = dim(png)[2:1]
# initialize an empty plot area if add==FALSE
if (!add)
plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',
xlab='',ylab='',bty='n')
img <- as.raster(readPNG(fname))
# rasterImage(img,1,1,res[1],res[2])
#grid.raster(img[1:600,1:500,]) wichtig img[y1:y2,x2:y2]
x1 = plot_brush$xmin
x2 = plot_brush$xmax
y2 = plot_brush$ymin
y1 = plot_brush$ymax
grid.raster(img[y2:y1,x1:x2,])
}
# Render the image in the plot with given dynamical 10%
output$plot <- renderImage({
#only if input$image is given
req(input$image)
temp <- image_read(input$image$datapath)
file <- image_convert(temp, "png")
temp_scale <- image_scale(file, paste0(scale,"%"))
fname = paste0(workingDir, "/", tempImage)
workingDir = workingDir
image_write(temp_scale, path = fname, format = "png", )
req(file)
list(src = fname, alt="alternative text")
shinyalert::shinyalert(title = "Success", text = "Configuration successfully saved!", type = "success")
}, deleteFile = FALSE)
#plot1
output$plot1 <- renderPlot({
req(input$image)
req(input$plot_brush)
d <- data()
if(!is.null(input$image$datapath) && input$image$datapath!=""){
plot_png(input$image$datapath, input$plot_brush, input$imgIndexTemplate)
}
})
# Function to save the cropped tepmlate map image
output$saveTemplate <- downloadHandler(
filename = function() {
paste(workingDir, '/data/templates/maps/map', '_',input$imgIndexTemplate,'.tif', sep='')
},
content = function(file) {
x1 = input$plot_brush$xmin
x2 = input$plot_brush$xmax
y2 = input$plot_brush$ymin
y1 = input$plot_brush$ymax
tempI <- image_read(input$image$datapath)
widht=(x2*rescale-x1*rescale)
height=(y1*rescale-y2*rescale)
geometrie <- paste0(widht, "x", height, "+",x1*rescale,"+", y2*rescale)
#"100x150+0+0")
tempI <- image_crop(tempI, geometrie)
image_write(tempI, file, format = "tif")
#writePNG(tempImage, target = file)
# unlink(paste0(workingDir,"/", tempImage))
i = input$imgIndexTemplate +1
updateNumericInput(session, "imgIndexTemplate", value = i)
})
observeEvent(input$listMTemplates, {
output$listMapTemplates = renderUI({
# Check if the directory already exists
findTemplateResult = paste0(workingDir, "/data/input/templates/maps/")
convertTifToPngSave(findTemplateResult, paste0(workingDir, "/www/data/map_templates_png/"))
prepareImageView("/map_templates_png/", '.png')
})
})
# Function to save the cropped template symbol image
output$saveSymbol <- downloadHandler(
filename = function() {
paste(workingDir, '/data/templates/maps/symbols', '_',input$imgIndexSymbol,'.tif', sep='')
},
content = function(file) {
x1 = input$plot_brush$xmin
x2 = input$plot_brush$xmax
y2 = input$plot_brush$ymin
y1 = input$plot_brush$ymax
tempI <- image_read(input$image$datapath)
widht=(x2*rescale-x1*rescale)
height=(y1*rescale-y2*rescale)
geometrie <- paste0(widht, "x", height, "+",x1*rescale,"+", y2*rescale)
#"100x150+0+0")
tempI <- image_crop(tempI, geometrie)
image_write(tempI, file, format = "tif")
#writePNG(tempImage, target = file)
# unlink(paste0(workingDir,"/", tempImage))
i = input$imgIndexSymbol +1
updateNumericInput(session, "imgIndexSymbol", value = i)
})
observeEvent(input$listSTemplates, {
output$listSymbolTemplates = renderUI({
findTemplateResult = paste0(workingDir, "/data/input/templates/symbols/")
convertTifToPngSave(findTemplateResult, paste0(workingDir, "/www/data/symbol_templates_png/"))
prepareImageView("/symbol_templates_png/", '.png')
})
})
####################
# 2. Maps matching #----------------------------------------------------------------------#
####################
# START th template matching
observeEvent(input$templateMatching, {
# call the function for map matching
manageProcessFlow("mapMatching", "map matching", "matching")
})
observeEvent(input$listMapsMatching, {
if(input$siteNumberMapsMatching != ''){
#print(input$siteNumberMapsMatching)
output$listMaps = renderUI({
prepareImageView("/matching_png/", input$siteNumberMapsMatching)
})
}
else{
output$listMaps = renderUI({
prepareImageView("/matching_png/", '.png')
})
}
})
####################
# 2.1 Maps align #----------------------------------------------------------------------#
####################
# Start Align maps
observeEvent(input$alignMaps, {
# call the function for align maps
manageProcessFlow("alignMaps", "align maps", "allign")
})
# List align maps
observeEvent(input$listAlign, {
if(input$siteNumberMapsMatching != ''){
#print(input$siteNumberMapsMatching)
output$listAlign = renderUI({
prepareImageView("/align_png/", input$siteNumberMapsMatching)
})
}
else{
output$listAlign = renderUI({
prepareImageView("/align_png/", '.png')
})
}
})
####################
# 2.2 Crop map legend species#----------------------------------------------------------------------#
####################
# Start read legend species
observeEvent(input$mapReadRpecies, {
# call the function for cropping
manageProcessFlow("mapReadRpecies", "cropping map species", "align")
})
# List map legend species
observeEvent(input$listCropped, {
if(input$siteNumberMapsMatching != ''){
#print(input$siteNumberMapsMatching)
output$listCropped = renderUI({
prepareImageView("/cropped_png/", input$siteNumberMapsMatching)
})
}
else{
output$listCropped = renderUI({
prepareImageView("/cropped_png/", '.png')
})
}
})
####################
# 2.3 Crop species name of the page content #----------------------------------------------------------------------#
####################
# Start Crop page species
observeEvent(input$pageReadRpecies, {
# call the function for cropping
manageProcessFlow("pageReadRpecies", "read page species", "output")
})
####################
# 3. Points Matching #----------------------------------------------------------------------#
####################
# Start points detection with matching
observeEvent(input$pointMatching, {
# call the function for cropping
manageProcessFlow("pointMatching", "points matching", "pointMatching")
})
observeEvent(input$listPointsM, {
if(input$siteNumberPointsMatching != ''){
#print(input$siteNumberPointsMatching)
output$listPM = renderUI({
prepareImageView("/pointMatching_png/", input$siteNumberPointsMatching)
})
}
else{
output$listPM = renderUI({
prepareImageView("/pointMatching_png/", '.png')
})
}
})
####################
# 3.1 Points Filtering #----------------------------------------------------------------------#
####################
# Start Process point filtering