From d59d514268b4e9e5c9c312a1d5f3ddfc6e34d08c Mon Sep 17 00:00:00 2001 From: vjonquiere Date: Mon, 1 Dec 2025 21:15:09 +0100 Subject: [PATCH] conflicts --- .github/workflows/deploy.yml | 45 +++++++++++++ src/server.R | 120 +++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 src/server.R diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..2ce2c31 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,45 @@ +name: Deploy to GitHub Pages + +on: + push: + branches: [ main, master ] + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - name: Set up R + uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install -y libgdal-dev libgeos-dev libproj-dev libudunits2-dev + + - name: Install R dependencies + run: | + Rscript -e 'install.packages(c("shiny", "leaflet", "bslib", "arrow", "geosphere", "sf", "dplyr", "pak", "RPostgres", "DBI"))' + Rscript -e 'pak::pkg_install("posit-dev/r-shinylive")' + + - name: Export Shinylive app + env: + DB_HOST: ${{ secrets.DB_HOST }} + DB_PORT: ${{ secrets.DB_PORT }} + DB_NAME: ${{ secrets.DB_NAME }} + DB_USER: ${{ secrets.DB_USER }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + run: | + Rscript -e 'shinylive::export(appdir = ".", destdir = "docs")' + touch docs/.nojekyll + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./docs diff --git a/src/server.R b/src/server.R new file mode 100644 index 0000000..a1e3767 --- /dev/null +++ b/src/server.R @@ -0,0 +1,120 @@ +library(shiny) + +fetchDB <- function(input) { + north_lat <- input$map_background$north + south_lat <- input$map_background$south + leafletProxy("map_background") %>% clearMarkers() %>% clearShapes() + + df <- data.frame(c(input$map_background_bounds$west, input$map_background_bounds$east), c(input$map_background_bounds$north, input$map_background_bounds$south)) + colnames(df) <- c("X", "Y") + + data_sf_orig <- st_as_sf( + df, + coords = c("X", "Y"), + crs = 4326 + ) + + data_sf_3035 <- st_transform(data_sf_orig, 3035) + + coords_3035 <- st_coordinates(data_sf_3035) + x_min <- min(coords_3035[, 1]) + x_max <- max(coords_3035[, 1]) + y_min <- min(coords_3035[, 2]) + y_max <- max(coords_3035[, 2]) + + res <- dbSendQuery(conn, sprintf("SELECT * FROM equipment_access WHERE \"X\" >= %.0f AND \"X\" <= %.0f AND \"Y\" >= %.0f AND \"Y\" <= %.0f LIMIT 10000", x_min, x_max, y_min, y_max)) + f <- dbFetch(res) + + if (nrow(f) == 0){ + print("No squared referenced in this area") + return () + } + + data_sf_4326 <- dbCoordsToLeaflet(f) + + + for (elt2 in seq_len(nrow(data_sf_4326))){ + elt <- data_sf_4326[elt2, ] + + #leafletProxy("map_background") %>% addMarkers( + #lng = st_coordinates(elt)[,1], + #lat = st_coordinates(elt)[,2], + #label = elt$Label + #) + bottomRightPoint <- destPoint(st_coordinates(elt)[1,], 135, sqrt(2)*100) + topLeftPoint <- destPoint(st_coordinates(elt)[1,], 315, sqrt(2)*100) + leafletProxy("map_background") %>% addRectangles( + lng1=topLeftPoint[1], + lat1=topLeftPoint[2], + lng2=bottomRightPoint[1], + lat2=bottomRightPoint[2], + color="green" + ) + + } + +} + + +dbCoordsToLeaflet <- function(df){ + data_df_orig <- data.frame( + X = df$X, + Y = df$Y, + Label = df$pop + ) %>% distinct() + + data_sf_orig <- st_as_sf( + data_df_orig, + coords = c("X", "Y"), + crs = 3035 + ) + + data_sf_4326 <- st_transform(data_sf_orig, 4326) + + return (data_sf_4326) + +} + +server <- function(input, output) { + + res <- dbSendQuery(conn, "SELECT * FROM equipment_access LIMIT 10") + f <- dbFetch(res) + + leaf <- leaflet() %>% + addTiles() + + observeEvent(input$map_background_bounds, { + fetchDB(input) + }) + + data_sf_4326 <- dbCoordsToLeaflet(f) + + for (elt2 in seq_len(nrow(data_sf_4326))){ + elt <- data_sf_4326[elt2, ] + + leaf <- addMarkers( + map = leaf, + lng = st_coordinates(elt)[,1], + lat = st_coordinates(elt)[,2], + label = elt$Label + ) + bottomRightPoint <- destPoint(st_coordinates(elt)[1,], 135, sqrt(2)*100) + topLeftPoint <- destPoint(st_coordinates(elt)[1,], 315, sqrt(2)*100) + leaf <- addRectangles( + map = leaf, + lng1=topLeftPoint[1], + lat1=topLeftPoint[2], + lng2=bottomRightPoint[1], + lat2=bottomRightPoint[2], + color="green" + ) + + } + + + print("Server update done!") + + + output$map_background <- renderLeaflet({leaf}) + +}