Skip to content

Commit d9f5ef3

Browse files
committed
commit to download
1 parent dc564aa commit d9f5ef3

4 files changed

Lines changed: 137 additions & 18 deletions

File tree

inst/app/functions_server.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
plotPostProcess <- function(simulations){
3+
4+
r <- currentRasterStack[[1]]
5+
# 2. Extract X and Y coordinates using the cell IDs
6+
coords <- terra::xyFromCell(r, simulations$ignition_cell)
7+
8+
# Bind the coordinates back to the data frame
9+
df_coords <- cbind(simulations, coords)
10+
11+
df_sf <- st_as_sf(df_coords, coords = c("x", "y"), crs = terra::crs(r)) %>%
12+
st_transform(4326) # Leaflet strictly requires EPSG:4326 (Lat/Lon)
13+
bbox <- st_bbox(df_sf)
14+
print(bbox)
15+
16+
# 4. Add to the existing Leaflet map
17+
# Replace "fireMap" with the actual ID of your leafletOutput in the UI
18+
leafletProxy("map") |>
19+
clearGroup("Simulation Ignition Points") |>
20+
addMapPane(name = "ignition_points_pane", zIndex = 650) |>
21+
# clearGroup("Simulation Output") %>% # Optional: remove previous run's markers
22+
addCircleMarkers(group = "Simulation Ignition Points",
23+
data = df_sf,
24+
radius = 12,
25+
options = pathOptions(pane = "ignition_points_pane"),
26+
color = "#e74c3c", # Red for fire
27+
# stroke = FALSE,
28+
fillOpacity = 0.8,
29+
popup = ~paste0(
30+
"<div style='font-family: sans-serif;'>",
31+
"<b>Simulation:</b> ", simulation, "<br>",
32+
"<b>Ignition Cell:</b> ", ignition_cell, "<br>",
33+
"<b>Burnt Cells:</b> ", burnt_cells,
34+
"</div>"
35+
)
36+
) |>
37+
leaflet::fitBounds( lng1 = bbox[["xmin"]], lat1 = bbox[["ymin"]],
38+
lng2 = bbox[["xmax"]], lat2 = bbox[["ymax"]]
39+
)
40+
41+
}
42+
43+
runPostProcess <- function(resDir){
44+
45+
fl <- list.files(resDir, pattern = "simLog.log", recursive = T, full.names = T)
46+
47+
if(length(fl)!=1){
48+
print(fl)
49+
print(resDir)
50+
showNotification("Output simLog.log file not found!", type="warning", duration=19)
51+
return(NA)
52+
}
53+
simout <- parse_fire_log(readLines(fl ) )
54+
plotPostProcess(simout)
55+
}
56+
57+
58+
59+
60+
checkIgnitionFile <- function(ignfile){
61+
ip <- read.csv(ignfile)
62+
63+
if(nrow(ip)!=0){
64+
ncell <- grep("ncell", names(ip), ignore.case = T )
65+
if(length(ncell)==0){
66+
showNotification(
67+
paste0("In file ", basename(ignfile) , " invalid ignition points found! please check file format and remove"),
68+
type = "error",
69+
duration = 20
70+
)
71+
return(FALSE)
72+
}
73+
## DOES NOT HAVE coordinates -> get THEM FROM Cell index IP
74+
if(!is.element("X", names(ip)) ){
75+
ncell <- ip$Ncell
76+
r2 <- terra::rast(rasters$FUEL)
77+
cr <- terra::xyFromCell(r2, ncell)
78+
crv <- terra::vect(cr)
79+
terra::crs(crv) <- terra::crs(r2)
80+
crv2 <- crv |> terra::project("EPSG:4326")
81+
crvc <- as.data.frame(crv2@pntr$coordinates())
82+
names(crvc) <- c("X","Y")
83+
ignitionPointsCoords(cbind(ip, crvc) )
84+
} else{
85+
ignitionPointsCoords(ip )
86+
}
87+
}
88+
if(anyNA(ip$Ncell)){
89+
showNotification(
90+
paste0("In file ", ignitionFiles[[1]] , " some invalid ignition points found, please check file format."),
91+
type = "warning",
92+
duration = 15
93+
)
94+
}
95+
return(TRUE)
96+
}

inst/app/helperFunctions.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' @examples
1010
center <- function(r){
1111
e <- terra::ext(r)
12+
1213
list(
1314
lng = (e$xmin + e$xmax) / 2,
1415
lat = (e$ymin + e$ymax) / 2

inst/app/server.R

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,24 @@ and raster %s",
794794
})
795795

796796
## TABLE IGNITION and edits ----
797+
##
798+
observeEvent(input$chooseIgnitionFile, {
799+
req(input$chooseIgnitionFile)
800+
df <-tryCatch({
801+
read.csv(input$chooseIgnitionFile)
802+
}, warning=function(e){
803+
showNotification(e$message, type="warning", duration = 19)
804+
NULL
805+
}, error=function(e){
806+
showNotification(e$message, type="error", duration = 19)
807+
NULL
808+
})
809+
req(df)
810+
ignitionPointsCoords(df)
811+
})
812+
797813
output$ignitionInfo <- renderDT({
814+
print(input$chooseIgnitionFile)
798815
# req(ignitionPointsCoords())
799816
ip <- ignitionPointsCoords()
800817
if(is.null(ip)){
@@ -843,6 +860,7 @@ and raster %s",
843860

844861
save_table_ignition_final <- function(overwrite=F){
845862
df <- isolate(ignitionPointsCoords())
863+
print(df)
846864
if(overwrite && isTruthy(input$chooseIgnitionFile)) {
847865
write.csv(df, input$chooseIgnitionFile, quote=FALSE, row.names = F)
848866
} else {
@@ -1082,24 +1100,26 @@ and raster %s",
10821100
loadInstances()
10831101

10841102
})
1085-
## download dataset -----
1086-
observeEvent(input$downloadfolder, {
1087-
req(input$inputfolder)
1088-
fp <- file.path("data", input$inputfolder )
1089-
req(fp!="data")
1090-
zip(sprintf("%s.zip", fp), list.files(fp, full.names = T) )
1091-
})
1092-
1093-
1103+
## download dataset -----
1104+
output$downloadfolder_dataset <- downloadHandler(
1105+
filename = function() {
1106+
sprintf("%s.zip", basename(input$inputfolder))
1107+
},
1108+
content = function(file) {
1109+
req(input$inputfolder)
1110+
zip(file,
1111+
list.files(input$inputfolder, full.names = T) )
1112+
}
1113+
)
10941114

1095-
## upload dataset -----
1096-
observeEvent(input$zipfileload, {
1097-
req(input$zipfileload)
1115+
## upload dataset -----
1116+
observeEvent(input$zipfileload_dataset, {
1117+
req(input$zipfileload_dataset)
10981118

10991119

1100-
path <- file.path("data", tools::file_path_sans_ext(input$zipfileload$name) )
1120+
path <- file.path("data", tools::file_path_sans_ext(input$zipfileload_dataset$name) )
11011121
if(dir.exists(path)){
1102-
showNotification(paste("Directory ", input$zipfileload$name, " exists, please delete it first or change the name of the zip file!"),
1122+
showNotification(paste("Directory ", input$zipfileload_dataset$name, " exists, please delete it first or change the name of the zip file!"),
11031123
type = "error",duration = 15
11041124
)
11051125
return(NULL)

inst/app/ui.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ ui <- shinydashboardPlus::dashboardPage(
153153
class="btn-sm", title="Remove file (cannot be undone)"),
154154
downloadButton("download_table_ignition", label = NULL, icon = icon("download"), class="btn-sm",
155155
title="Download table in CSV file format"),
156-
actionButton("upload_table_ignition", label = NULL, icon = icon("upload"), class="btn-sm", title="Upload your table(make sure it is in the same format as the required input format for Cell2Fire)"),
157-
div(style="display:none;", fileInput("upload_table_ignition_input", label = NULL, buttonLabel = NULL, width = 10, accept = c(".csv", ".xlsx", ".xls") ) )
156+
# actionButton("upload_table_ignition", label = NULL, icon = icon("upload"), class="btn-sm", title="Upload your table(make sure it is in the same format as the required input format for Cell2Fire)"),
157+
div(style="display:inline;", fileInput("upload_table_ignition_input",
158+
label = NULL, buttonLabel = icon("upload"),
159+
width = 20, accept = c(".csv", ".xlsx", ".xls") ) )
158160
),
159161
div( style = "overflow-x: auto;", DTOutput("ignitionInfo") )
160162
# ,# collapsible=TRUE, collapsed = TRUE,
@@ -283,11 +285,11 @@ ui <- shinydashboardPlus::dashboardPage(
283285
shiny::fluidRow(
284286

285287
shiny::column(4, div(
286-
shiny::fileInput("zipfileload", label=NULL, buttonLabel = icon("upload") ),
288+
shiny::fileInput("zipfileload_dataset", label=NULL, buttonLabel = icon("upload") ),
287289
title="add a zip file with all necessary files - please see <a href=https://github.com/fpirotti/Cell2FireR/blob/master/README.md target=_blank> 📖 documentation HERE</a> on how to prepare it"
288290
) ),
289291
shiny::column(3, div(
290-
shiny::actionButton("downloadfolder" , NULL,
292+
shiny::downloadButton("downloadfolder_dataset" , NULL,
291293
icon = icon("download") ), title="Download dataset") ),
292294
shiny::column(3, div(
293295
shiny::actionButton("deletefolder", NULL,

0 commit comments

Comments
 (0)