app_server <- function(input, output, session) { loaded_content <- shiny::reactiveVal(NULL) tool_choices_by_category <- list( convert = c( "NetCDF to TIFF" = "netcdf_to_tiff", "ASCII to TIFF" = "ascii_to_tiff", "TIFF to NetCDF" = "tiff_to_netcdf", "TIFF to ASCII" = "tiff_to_ascii", "SHP to TIFF" = "shp_to_tiff", "TIFF to SHP" = "tiff_to_shp" ), raster = c( "Create raster stack" = "create_raster_stack", "Rasterise vector" = "rasterize" ), vector = c( "Vectorise raster" = "vectorize" ), qc = c( "Raster quality control" = "raster_qc_tool" ) ) tool_context_map <- list( create_raster_stack = "Combine several aligned raster layers into one stack. Example: stack annual rainfall, temperature, and canopy cover surfaces before habitat modelling.", rasterize = "Convert polygons or points into raster cells. Example: turn vegetation mapping polygons into a planning grid for site suitability.", vectorize = "Convert raster patches into polygons or points. Example: turn a classified habitat raster into patch boundaries for reporting.", netcdf_to_tiff = "Extract gridded climate or ocean data into GeoTIFF. Example: convert monthly NetCDF climate layers into TIFFs for local analysis.", ascii_to_tiff = "Bring legacy ASCII grids into a standard raster format. Example: convert an old DEM grid to TIFF before stacking it with newer layers.", tiff_to_netcdf = "Export raster outputs into NetCDF for model sharing. Example: package a derived climate surface for downstream time-series workflows.", tiff_to_ascii = "Export a GeoTIFF to ASCII grid for older tools. Example: prepare a resistance surface for a legacy connectivity workflow.", shp_to_tiff = "Convert vector boundaries or classes to raster cells. Example: rasterise soil polygons onto the same grid as environmental covariates.", tiff_to_shp = "Convert raster classes into vector features. Example: turn a binary habitat suitability raster into polygons for review and editing.", raster_qc_tool = "Run a quick raster quality-control report before modelling. Example: check CRS, extent, resolution, and bands before stacking predictors." ) workflow_context_map <- list( raster_qc = "Check CRS, extent, resolution, band count, names, and missing values. Example: verify predictor rasters before building a species distribution model." ) output$map <- leaflet::renderLeaflet({ leaflet::leaflet() |> leaflet::addProviderTiles(leaflet::providers$CartoDB.Positron) }) shiny::observeEvent(input$shared_files, { req(input$shared_files$datapath) content <- load_uploaded_spatial(input$shared_files) loaded_content(content) update_map_preview(content$spatial) }) shiny::observeEvent(input$tool_category, { shiny::updateSelectInput( session, "tool_name", choices = tool_choices_by_category[[input$tool_category]], selected = unname(tool_choices_by_category[[input$tool_category]][1]) ) }, ignoreInit = FALSE) update_map_preview <- function(spatial) { map <- leaflet::leafletProxy("map") map |> leaflet::clearGroup("layer") if (inherits(spatial, "SpatRaster")) { xmin <- terra::xmin(spatial) xmax <- terra::xmax(spatial) ymin <- terra::ymin(spatial) ymax <- terra::ymax(spatial) preview <- spatial[[1]] values <- terra::values(preview, mat = FALSE, na.rm = TRUE) value_range <- stats::quantile(values, probs = c(0.02, 0.98), na.rm = TRUE, names = FALSE) palette <- leaflet::colorNumeric( palette = c("#0b3c49", "#1f7a8c", "#70a9a1", "#f6bd60", "#ee6c4d"), domain = value_range, na.color = "transparent" ) map |> leaflet::addRasterImage( x = raster::raster(preview), colors = palette, opacity = 0.85, group = "layer" ) |> leaflet::fitBounds(xmin, ymin, xmax, ymax) return(invisible(NULL)) } vector <- sf::st_as_sf(spatial) geom_type <- unique(as.character(sf::st_geometry_type(vector))) bounds <- sf::st_bbox(vector) if (all(geom_type %in% c("POINT", "MULTIPOINT"))) { map |> leaflet::addCircleMarkers(data = vector, group = "layer", color = "#c2410c") } else { map |> leaflet::addPolygons(data = vector, group = "layer", color = "#c2410c", fillOpacity = 0.2) } map |> leaflet::fitBounds(bounds$xmin, bounds$ymin, bounds$xmax, bounds$ymax) } output$layer_summary <- shiny::renderUI({ content <- loaded_content() req(content) metadata <- get_spatial_metadata(content$spatial) shiny::tagList( shiny::h4("Layer summary"), shiny::htmlOutput("layer_summary_text") ) }) output$layer_summary_text <- shiny::renderText({ content <- loaded_content() req(content) metadata <- get_spatial_metadata(content$spatial) paste( paste("Loaded as:", gsub("_", " ", content$kind)), paste("Type:", metadata$type), paste("CRS:", metadata$crs %||% "missing"), sep = "\n" ) }) output$metadata_table <- shiny::renderTable({ content <- loaded_content() req(content) metadata <- get_spatial_metadata(content$spatial) data.frame( field = names(metadata), value = vapply(metadata, function(value) paste(value, collapse = ", "), character(1)), stringsAsFactors = FALSE ) }) output$attribute_table <- DT::renderDataTable({ content <- loaded_content() req(content) if (inherits(content$spatial, "SpatRaster")) { return(DT::datatable(data.frame(note = "Attribute tables are only shown for vector layers."))) } DT::datatable(get_attribute_table(content$spatial)) }) output$tool_status <- shiny::renderPrint({ invisible(NULL) }) output$tool_context <- shiny::renderUI({ shiny::tags$div( class = "context-tip", tool_context_map[[input$tool_name %||% "create_raster_stack"]] ) }) shiny::observeEvent(input$run_tool, { content <- loaded_content() req(content, nzchar(input$tool_output)) in_path <- content$primary_path out_path <- input$tool_output result <- switch( input$tool_name, raster_qc_tool = { raster_input <- if (inherits(content$spatial, "SpatRaster")) content$spatial else stop("Load a raster or raster stack first.", call. = FALSE) raster_quality_report(raster_input) }, create_raster_stack = { if (!identical(content$kind, "raster_stack")) { stop("Load multiple raster files first to build a raster stack.", call. = FALSE) } create_raster_stack(content$paths, out_path) }, rasterize = { raster <- rasterize_vector(in_path) terra::writeRaster(raster, out_path, overwrite = TRUE, filetype = "GTiff") }, vectorize = { vector <- vectorize_raster(in_path) sf::st_write(vector, out_path, delete_dsn = TRUE, quiet = TRUE) }, netcdf_to_tiff = netcdf_to_tiff(in_path, out_path), ascii_to_tiff = ascii_to_tiff(in_path, out_path), tiff_to_netcdf = tiff_to_netcdf(in_path, out_path), tiff_to_ascii = tiff_to_ascii(in_path, out_path), shp_to_tiff = shp_to_tiff(in_path, out_path), tiff_to_shp = tiff_to_shp(in_path, out_path) ) output$tool_status <- shiny::renderPrint({ list(status = "completed", output = out_path, result = result) }) }) output$workflow_status <- shiny::renderPrint({ invisible(NULL) }) output$workflow_context <- shiny::renderUI({ shiny::tags$div( class = "context-tip", workflow_context_map[[input$workflow_name %||% "raster_qc"]] ) }) output$workflow_report <- shiny::renderUI({ shiny::tagList() }) shiny::observeEvent(input$run_workflow, { content <- loaded_content() req(content) raster_input <- if (inherits(content$spatial, "SpatRaster")) content$spatial else stop("Load a raster or raster stack for workflows.", call. = FALSE) result <- run_named_workflow( workflow_name = input$workflow_name, raster = raster_input, vector = NULL, target_crs = input$workflow_crs, output_path = input$workflow_output ) output$workflow_status <- shiny::renderPrint(result) report <- result[["quality_report"]] if (!is.null(report)) { output$workflow_report <- shiny::renderUI({ shiny::tagList( shiny::h4("Quality report"), DT::dataTableOutput("workflow_quality_report") ) }) output$workflow_quality_report <- DT::renderDataTable(DT::datatable(report)) } }) }