-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwtss_shiny_app.R
More file actions
315 lines (280 loc) · 9.41 KB
/
Copy pathwtss_shiny_app.R
File metadata and controls
315 lines (280 loc) · 9.41 KB
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
library(shiny)
library(plotly)
library(leaflet)
library(leaflet.extras)
library(leaflet.extras2)
library(jsonlite)
library(httr)
library(httr2)
library(base64enc)
library(R6)
library(shinyjs)
library(shinydashboard)
library(logger) # Add logging library
source("wtss/wtss_client.R")
# Set up logging configuration
log_dir <- "logs"
if (!dir.exists(log_dir)) {
dir.create(log_dir)
}
log_file <- file.path(log_dir, paste0("wtss_app_", format(Sys.time(), "%Y%m%d"), ".log"))
log_threshold(TRACE)
log_appender(appender_file(log_file))
# Initialize the WTSS client
wtss_inpe <- "https://data.inpe.br/bdc/wtss/v4/"
client <- WTSSClient$new(base_url = wtss_inpe)
# Get available collections for the dropdown
capabilities <- client$get_capabilities()
available_collections <- sapply(capabilities$available_collections, function(x) x$name)
# UI Definition
ui <- fluidPage(
useShinyjs(),
tags$head(
tags$style(HTML("
.sidebar {
height: calc(100vh - 20px);
overflow-y: auto;
padding: 15px;
background-color: #f8f9fa;
position: fixed;
width: 300px;
left: 15px;
top: 10px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.map-container {
height: 100vh;
margin-left: 330px;
padding: 0;
}
#plotPanel {
z-index: 1000;
}
.well {
background-color: white;
border: none;
box-shadow: none;
}
"))
),
# Sidebar with form
div(class = "sidebar",
wellPanel(
h2("WTSS Explorer", style = "margin-top: 0; color: #2c3e50;"),
hr(),
# Product Selection
selectInput("product", "Select Satellite Product",
choices = available_collections,
selected = NULL),
# Summarise Geometry Checkbox
checkboxInput("summariseGeometry", "Summarise Geometry", value = FALSE),
# Dynamic Bands Selection
uiOutput("bandSelector"),
# Date Range Selection
dateRangeInput("dateRange", "Select Date Range",
start = Sys.Date() - 365,
end = Sys.Date(),
format = "yyyy-mm-dd"),
# Submit Button
actionButton("getData", "Get Time Series",
class = "btn-primary btn-block",
style = "margin-top: 20px;")
)
),
# Main panel with map
div(class = "map-container",
leafletOutput("map", height = "100%"),
# Plot panel
div(
id = "plotPanel",
style = "display: none;", # Initially hidden
absolutePanel(
class = "panel panel-default",
fixed = TRUE,
draggable = TRUE,
top = 60,
right = 20,
width = "500px",
height = "400px",
style = "background-color: white; padding: 15px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1);",
plotlyOutput("timeSeries", width = '100%', height = '350px'),
actionButton("closePlot", "Close", class = "btn-sm btn-danger")
)
)
)
)
# Server Definition
server <- function(input, output, session) {
# Log application start
log_info("WTSS Explorer application started")
# Reactive values to store drawn features and selected data
rv <- reactiveValues(
drawnFeatures = NULL,
timeSeriesData = NULL
)
# Dynamic band selector based on selected product
output$bandSelector <- renderUI({
req(input$product)
log_info("User selected product: {input$product}")
collection_info <- client$get_collection_info(input$product)
band_names <- sapply(collection_info$bands, function(x) x$name)
# Allow single selection if summariseGeometry is checked
selectInput("bands", "Select Band/Index",
choices = band_names,
selected = band_names[1],
multiple = !input$summariseGeometry)
})
# Initialize the map
output$map <- renderLeaflet({
log_info("Initializing map view")
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OpenStreetMap") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Satellite") %>%
addLayersControl(
baseGroups = c("OpenStreetMap", "Satellite"),
options = layersControlOptions(collapsed = TRUE)
) %>%
addDrawToolbar(
targetGroup = 'Features',
editOptions = editToolbarOptions(),
polylineOptions = FALSE,
rectangleOptions = TRUE,
circleOptions = TRUE,
markerOptions = TRUE,
polygonOptions = TRUE
) %>%
setView(lng = -51.9253, lat = -14.2350, zoom = 4)
})
# Handle drawn features
observeEvent(input$map_draw_new_feature, {
feature_type <- input$map_draw_new_feature$properties$feature_type
log_info("User drew a new {feature_type} on the map")
rv$drawnFeatures <- input$map_draw_new_feature
})
# Handle deleted features
observeEvent(input$map_draw_deleted_features, {
log_info("User deleted drawn features from the map")
rv$drawnFeatures <- NULL
hide("plotPanel")
})
# Log band selection changes
observeEvent(input$bands, {
log_info("User selected bands: {paste(input$bands, collapse=', ')}")
})
# Log date range changes
observeEvent(input$dateRange, {
log_info("User selected date range: {input$dateRange[1]} to {input$dateRange[2]}")
})
# Get Time Series Data
observeEvent(input$getData, {
req(rv$drawnFeatures, input$product, input$bands, input$dateRange)
log_info("User requested time series data for product: {input$product}")
# Convert the drawn feature to the required format
feature_type <- rv$drawnFeatures$properties$feature_type
coordinates <- rv$drawnFeatures$geometry$coordinates
# Prepare geometry based on feature type
if (feature_type == "polygon") {
geom <- list(
type = "Polygon",
coordinates = coordinates
)
} else if (feature_type == "marker") {
geom <- list(
type = "Point",
coordinates = coordinates
)
} else if (feature_type == "circle") {
center <- coordinates
radius <- rv$drawnFeatures$properties$radius
# Convert circle to polygon points
points <- lapply(seq(0, 360, by = 10), function(angle) {
x <- center[[1]] + radius * cos(angle * pi / 180)
y <- center[[2]] + radius * sin(angle * pi / 180)
c(x, y)
})
# Close the polygon
points[[length(points) + 1]] <- points[[1]]
geom <- list(
type = "Polygon",
coordinates = list(points)
)
}
# Get time series data
tryCatch({
log_info("Requesting time series data from WTSS service")
if (input$summariseGeometry) {
# Use summarize_get for summarized geometry
summary_data <- client$summarize_get(
collectionId = input$product,
geom = geom,
attributes = input$bands,
start_datetime = paste0(format(input$dateRange[1], "%Y-%m-%d"), "T00:00:00Z"),
end_datetime = paste0(format(input$dateRange[2], "%Y-%m-%d"), "T00:00:00Z")
)
# Access the values dynamically
band_name <- input$bands # Get the dynamic band name
df <- parseSummaryDataToDF(summary_data)
rv$timeSeriesData <- data.frame(
date = df[['date']],
max = df[[paste0(band_name, '_max')]],
min = df[[paste0(band_name, '_min')]],
avg = df[[paste0(band_name, '_mean')]]
)
plot <- plot_ly(data = rv$timeSeriesData, x = ~date) %>%
add_trace(y = ~max, name = "Max", type = "scatter", mode = "lines") %>%
add_trace(y = ~min, name = "Min", type = "scatter", mode = "lines") %>%
add_trace(y = ~avg, name = "Average", type = "scatter", mode = "lines") %>%
layout(title = "Summarized Time Series Data",
xaxis = list(title = "Date"),
yaxis = list(title = "Values"))
} else {
response <- client$timeseries_get(
collectionId = input$product,
geom = geom,
attributes = input$bands,
start_datetime = paste0(format(input$dateRange[1], "%Y-%m-%d"), "T00:00:00Z"),
end_datetime = paste0(format(input$dateRange[2], "%Y-%m-%d"), "T00:00:00Z")
)
# Parse and store the data
rv$timeSeriesData <- parseGetTimeSeriesToDF(response)
plot <- plot_ly()
for(band in input$bands) {
plot <- plot %>% add_trace(
data = rv$timeSeriesData,
x = ~date,
y = as.formula(paste0("~", band)),
name = band,
type = "scatter",
mode = "lines"
)
}
}
log_info(paste0("Successfully retrieved and parsed time series data", ", columns:", paste(names(rv$timeSeriesData), collapse=';')))
# Show the plot panel
show("plotPanel")
log_info("Displaying time series plot")
output$timeSeries <- renderPlotly({
plot
})
}, error = function(e) {
log_error("Error getting time series data: {e$message}")
showNotification(
paste("Error getting time series data:", e$message),
type = "error"
)
})
})
# Close plot panel
observeEvent(input$closePlot, {
log_info("User closed the time series plot")
hide("plotPanel")
})
# Log when session ends
session$onSessionEnded(function() {
log_info("User session ended")
})
}
# Run the application
shinyApp(ui = ui, server = server)