Skip to content

Commit

Permalink
Add point click shiny app to select municipalities
Browse files Browse the repository at this point in the history
Added a shiny app to select municipalities to monitor in 2025. Results in a shapefile in ./data/output/SelectedMunic/
  • Loading branch information
fjasteen committed Jan 9, 2025
1 parent e71b889 commit 15970c4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/data/observations/output/first_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ NA,NA,"nwvjndbl","V_3600_15",NA,"Limburg","Genk",NA,50.9772656,5.4943478,0,2,NA,
"Slijpebruggeleed",NA,"odcxnmmx","I_8432_1",NA,"West-Vlaanderen","Middelkerke",NA,51.16521,2.83294,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
"Bourgognevaart",NA,"odcxnmmx","I_8460_10",NA,"West-Vlaanderen","Oudenburg",NA,51.15303,3.00184,NA,NA,3,1,NA,NA,1,FALSE,FALSE
"Jobeek",NA,"odcxnmmx","I_8750_3",NA,"West-Vlaanderen","Wingene",NA,51.04451,3.20547,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
"Zuidervaartje",NA,"odcxnmmx","I_8020_8",NA,"West-Vlaanderen","Oostkamp",NA,51.17569,3.23535,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
"Zuidervaartje",NA,"odcxnmmx","I_8020_8",NA,"West-Vlaanderen","Oostkamp",NA,51.1777616,3.2349959,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
NA,NA,"odcxnmmx","I_2220_1",NA,"Antwerpen","Heist-op-den-Berg",NA,51.0906,4.7409,NA,NA,NA,NA,196,NA,1,TRUE,FALSE
NA,NA,"odcxnmmx","I_8432_2",NA,"West-Vlaanderen","Middelkerke",NA,51.1596686,2.9141654,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
NA,NA,"odcxnmmx","I_8460_11",NA,"West-Vlaanderen","Oudenburg",NA,51.1821246,2.9633365,0,NA,NA,NA,NA,NA,1,TRUE,FALSE
Expand All @@ -399,3 +399,4 @@ NA,NA,"odcxnmmx","I_9190_7",NA,"Oost-Vlaanderen","Stekene",NA,51.222892,4.040976
NA,NA,"odcxnmmx","I_9190_10",NA,"Oost-Vlaanderen","Stekene",NA,51.2042535,4.0287827,NA,NA,NA,1,1,NA,1,FALSE,FALSE
"Visvijver",NA,"njiyn2yx","V_2100_10",NA,"Antwerpen","Antwerpen",NA,51.212119,4.470448,0,NA,3,NA,NA,NA,5,TRUE,FALSE
"Homeleswijer",NA,"nwvjndbl","V_3600_17",NA,"Limburg","Genk",NA,50.958981,5.467505,NA,NA,1,NA,NA,NA,1,TRUE,FALSE
NA,NA,"njiyn2yx","V_2100_11",NA,"Antwerpen","Antwerpen",NA,51.215415,4.4634837,0,NA,NA,NA,NA,NA,5,TRUE,FALSE
Binary file added R/data/output/SelectedMunic/SelectedMunic.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions R/data/output/SelectedMunic/SelectedMunic.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["Belge_Lambert_1972",GEOGCS["GCS_Belge_1972",DATUM["D_Belge_1972",SPHEROID["International_1924",6378388.0,297.0]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",150000.01256],PARAMETER["False_Northing",5400088.4378],PARAMETER["Central_Meridian",4.36748666666667],PARAMETER["Standard_Parallel_1",49.8333339],PARAMETER["Standard_Parallel_2",51.1666673333333],PARAMETER["Latitude_Of_Origin",90.0],UNIT["Meter",1.0]]
Binary file added R/data/output/SelectedMunic/SelectedMunic.shp
Binary file not shown.
Binary file added R/data/output/SelectedMunic/SelectedMunic.shx
Binary file not shown.
80 changes: 80 additions & 0 deletions R/src/validated_craywatch_map_gemeenten.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library(osmdata)
library(ggplot2)
library(tidyr)
library(lubridate)
library(shiny)


# Read data
Expand Down Expand Up @@ -132,3 +133,82 @@ ggsave(species_plot, file = "~/GitHub/craywatch/R/data/output/validated_craywatc
num_points <- nrow(craywatch_data_filtered)
print(paste("Number of points included in the map:", num_points))



# Dummy SF data maken
nc <- gemeenten # Voorbeeld polygonen


# UI voor Shiny app
ui <- fluidPage(
h3("Selecteer polygonen met een ggplot achtergrond"),
plotOutput("map", click = "plot_click"),
verbatimTextOutput("selectedPolygons"),
actionButton("save", "Opslaan als Shapefile") # Knop om selectie op te slaan
)

# Serverlogica
server <- function(input, output, session) {
selected <- reactiveVal(rep(FALSE, nrow(nc)))

# Observeer klik en update selectie
observeEvent(input$plot_click, {
click_point <- st_sfc(st_point(c(input$plot_click$x, input$plot_click$y)), crs = st_crs(nc))
clicked <- st_contains(nc, click_point, sparse = FALSE)
if (any(clicked)) {
sel <- selected()
sel[clicked] <- !sel[clicked]
selected(sel)
}
})

# Combineer species_plot en polygonen
output$map <- renderPlot({
nc$selected <- selected() # Voeg selectie toe aan dataset

# Combineer species_plot met polygonen
species_plot +
geom_sf(data = nc, aes(fill = selected), color = "black", alpha = 0.5) + # Polygonen transparant
scale_fill_manual(values = c("FALSE" = "transparent", "TRUE" = "red")) +
labs(fill = "Geselecteerd") +
theme_minimal() # Houd de kaart overzichtelijk
})

# Toon geselecteerde polygonen
output$selectedPolygons <- renderPrint({
if (any(selected())) {
nc[selected(), ]
} else {
"Geen polygonen geselecteerd"
}
})

# Opslaan als Shapefile
observeEvent(input$save, {
# Zorg dat de hoofddirectory bestaat
dir.create("./data/output/SelectedMunic", showWarnings = FALSE, recursive = TRUE)

# Pad naar het bestand
file_path <- "./data/output/SelectedMunic/SelectedMunic.shp"

# Filter geselecteerde polygonen
selected_sf <- nc[selected(), ]

# Controleer of er polygonen geselecteerd zijn
if (nrow(selected_sf) == 0) {
showNotification("Geen polygonen geselecteerd om op te slaan!", type = "error")
return()
}

# Sla op als Shapefile
tryCatch({
st_write(selected_sf, file_path, driver = "ESRI Shapefile", delete_dsn = TRUE)
showNotification("Shapefile succesvol opgeslagen in ./data/output/SelectedMunic/SelectedMunic.shp", type = "message")
}, error = function(e) {
showNotification(paste("Fout bij opslaan:", e$message), type = "error")
})
})
}

# Start de app
shinyApp(ui, server)

0 comments on commit 15970c4

Please sign in to comment.