The code below enables me to add polygons I draw on a leaflet map to tables in my database. However I have some difficulties since the geometry column of my table is called "geom" and the geometry column created with the leaflet map is called "geometry".
I get a warning message like this:
"Error in : Failed to initialise COPY: ERREUR: the column "geometry" does not exists"
ui <- fluidPage(leafletOutput("map"))
server <- function(input, output, session){
output$map <- renderLeaflet({
leaflet() %>%
addTiles(group = "OSM") %>%
addDrawToolbar(targetLayerId = NULL, targetGroup = NULL,
polygonOptions = drawPolygonOptions(showArea = TRUE, metric=TRUE, shapeOptions=drawShapeOptions(fillColor="aquamarine", fillOpacity=0.8, clickable = TRUE), repeatMode=FALSE),
polylineOptions = FALSE,
circleOptions = FALSE,
rectangleOptions = FALSE,
markerOptions = FALSE,
circleMarkerOptions = FALSE,
singleFeature = FALSE,
editOptions = FALSE)})
observeEvent(input$map_draw_new_feature, {
polygon_coordinates <- input$map_draw_new_feature$geometry$coordinates[[1]]
drawn_polygon <- Polygon(do.call(rbind,lapply(polygon_coordinates,function(x){c(x[[1]][1],x[[2]][1])})))
sps <- SpatialPolygons(list(Polygons(list(drawn_polygon),"drawn_polygon")))
created_polygon <- st_as_sf(sps, CRS("+proj=longlat +datum=WGS84"))
st_write(created_polygon , pool, "mytable", append = TRUE)
})
}
shinyApp(ui, server)
How can I solve this without altering the column in my database ?
EDIT:
I tried something ugly like this:
created_polygon$geom <- created_polygon$geometry
created_polygon<- created_polygon%>% st_drop_geometry()
But I get this warning message
"Warning: Error in : COPY returned error: ERROR: Geometry type (Polygon) does not match column type (MultiPolygon)"