Spatial CRUD and attribute editing in shiny

Dear Community.

I have tried several times to create a Spatial Polygon CRUD app that allow me to:

  1. Create, Edit and Delete Spatial Polygons.
  2. Create, Edit and Delete the Attributes of the polygons saved.

I have opened several issue here:

but i cannot still get a realistic solution for this.

I will report here what i have done so far, and hope for help.

library(shiny)
library(sp)
library(sf)
library(leaflet)
library(leaflet.extras)
library(leafpm)
library(FRK)
library(DT)
library(mapedit)
library(spData)
library(rpostgis)
library(tidyverse)
library(RPostgres)
library(rgdal)

ui <- fluidPage(

  titlePanel("CRUD Spatial data & Attributes"),

  sidebarLayout(
    sidebarPanel(

      plotOutput("plot"),
      actionButton("plot_action",
                   label = "Plot")
    ),

    mainPanel(
      editModUI("editor")
    )
  )
)

server <- function(input, output) {

  world <- reactive({
    data<-spData::world
    data<-subset(data, name_long=="Italy")

    return(data)

  })

  map<-leaflet() %>%
    addTiles() %>%
    addProviderTiles(providers$OpenStreetMap,
                     options = tileOptions(minZoom = 2, maxZoom = 15)) %>%
    addProviderTiles(providers$Esri.WorldImagery,
                     options = tileOptions(minZoom = 15, maxZoom = 20),
                     group = "Esri.WorldImagery") %>%
    addPmToolbar(targetGroup = "name_long",
                 toolbarOptions = pmToolbarOptions(drawMarker = F,
                                                   drawPolygon = TRUE,
                                                   drawPolyline = F,
                                                   drawCircle = F,
                                                   drawRectangle = F,
                                                   editMode = TRUE,
                                                   cutPolygon = T,
                                                   removalMode = TRUE,
                                                   position = "topleft"))

  edits <- callModule(editMod,
                      "editor",
                      targetLayerId = "name_long",
                      leafmap = map)

  ns <- shiny::NS("editor")

  observe({
    req(world())
    proxy.lf <- leafletProxy(ns("map"))

    bounds <- world() %>%
      st_bbox() %>%
      as.character()

    proxy.lf %>%
      fitBounds(bounds[1], bounds[2], bounds[3], bounds[4]) %>%
      leaflet::addPolygons(data = world(),
                           weight = 3,
                           opacity = 1,
                           fill = FALSE,
                           color = 'red',
                           fillOpacity = 1,
                           smoothFactor = 0.01,
                           layerId  = "name_long")
  })

  polygons<-eventReactive(input$plot_action, {

    if (is.null(edits()$finished)) {
      finished<-as_Spatial(world())
    } else {
      finished<-edits()$finished
      finished<-as_Spatial(finished)
    }

    if (is.null(edits()$edited)) {

      if (is.null(edits()$deleted)) {
        polygons<-finished
      } else {
        deleted<-edits()$deleted
        print(deleted)
      }

    }

    return(polygons)

    # if (is.null(edits()$edited)) {
    #   edited<-NULL
    # } else {
    #   edited<-edits()$edited
    #   edited<-as_Spatial(edited)
    # }
    #
    # if (is.null(edits()$deleted)) {
    #   deleted<-NULL
    # } else {
    #   deleted<-edits()$deleted
    #   deleted<-as_Spatial(deleted)
    # }
    #
    # if (is.null(deleted)) {
    #
    # } else {
    #
    #   if (is.null(edited)) {
    #     finished<-subset(finished, edit_id!=deleted$edit_id)
    #   } else {
    #     edited<-subset(edited, edit_id!=deleted$edit_id)
    #     finished<-subset(finished, edit_id!=deleted$edit_id)
    #   }
    # }

    polygons<-raster::union(finished,
                            edited)

    return(polygons)


    # if (is.null(edited)) {
    #   edited<-finished
    # } else {
    #   edited<-raster::union(edited,
    #                         finished)
    # }
    #
    # edited$edit_id<-tidyr::replace_na(edited$edit_id, replace="1")
    #
    # if (is.null(edits()$deleted)) {
    #
    #   edited$Campo<-seq(from=1, by=1, to=nrow(edited))
    #
    #   data_to_filter<-edited@data %>%
    #     dplyr::group_by(edit_id) %>%
    #     slice_tail()
    #
    #   for (i in seq(from=1, by=1, to=nrow(data_to_filter))) {
    #
    #     if (i==1) {
    #       edited_one<-subset(edited, Campo==data_to_filter$Campo[i])
    #     } else {
    #       edited_two<-subset(edited, Campo==data_to_filter$Campo[i])
    #
    #       edited_one<-raster::union(edited_one,
    #                                 edited_two)
    #
    #       rm(edited_two)
    #     }
    #
    #   }
    #
    # } else {
    #
    #   deleted<-edits()$deleted
    #   deleted<-as_Spatial(deleted)
    #
    #   edited<-subset(edited, edit_id!=deleted$edit_id)
    #
    #   edited$Campo<-seq(from=1, by=1, to=nrow(edited))
    #
    #   data_to_filter<-edited@data %>%
    #     dplyr::group_by(edit_id) %>%
    #     slice_tail()
    #
    #   for (i in seq(from=1, by=1, to=nrow(data_to_filter))) {
    #
    #     if (i==1) {
    #       edited_one<-subset(edited, Campo==data_to_filter$Campo[i])
    #     } else {
    #       edited_two<-subset(edited, Campo==data_to_filter$Campo[i])
    #
    #       edited_one<-raster::union(edited_one,
    #                                 edited_two)
    #
    #       rm(edited_two)
    #     }
    #
    #   }
    #
    # }
    # return(edited_one)

  })

  output$plot<-renderPlot({
    req(polygons())
    plot(polygons())
  })

}

# Run the application
shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.