0

I am trying to add polygons to a map once a user zooms in close to a marker. I have two datasets: one has lat and long for the points where the markers appear, and the other has the polygon defined in a geoJSON format. Ideally, I will subset the polygons table to those with an ID given by a visible marker, but for now, I'd be happy to just get them to draw at all. When I try this:

pointData <- conn %>% tbl(in_schema("SCHEMA", "POINTS")) %>% as.data.frame();
polygonData <- conn %>% tbl(in_schema(" SCHEMA  ", "POLYGONS" )) %>% as.data.frame();

....

  observe({
    colorBy <- input$color
    sizeBy <- input$size
   
   
    leafletProxy("map", data =  pointData   ) %>%
      clearMarkers() %>%
      addCircleMarkers(~LONGITUDE, ~LATITUDE,
                       popup=
                         ~paste("Latitude: ", as.character(LATITUDE) , "<br/>" ,
                                "Longitude: ", as.character(LONGITUDE) , "<BR/>"
                                ),
                       clusterOptions=markerClusterOptions()) %>%
      addPolygons( data= polygonData   , group="POLYGONS" , ~POLYGON ) %>%
      groupOptions( "POLYGONS" , zoomLevels=9:20 )
  })

What I'm left with is an error that Error in derivePolygons: addPolygons must be called with both lng and lat, or with neither. If I specify lat and long, I get a polygon that connects my visible markers, not the polygons defined for those markers in the ~POLYGON column. How do I draw polygons that are defined in a column as a geoJSON description?

=====EDITED=====

I've greatly simplified my code and can get a polygon to render:

library(shiny)
library(leaflet)
library(rjson)
library(sf)
library(geojsonsf)

ui <- fluidPage(
  leafletOutput("map", height="95vh")
)

polygon_json_str <- '{
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [-78.6444771915128, 35.5891446843761], 
          [-78.6444129670176, 35.5891659862555], 
          [-78.6443505067032, 35.5891905401108], 
          [-78.6442900571017, 35.5892182490901], 
          [-78.6442318566906, 35.5892490037235], 
          [-78.6441761352076, 35.5892826827172], 
          [-78.6441231125596, 35.5893191531594], 
          [-78.6440729980033, 35.5893582711212], 
          [-78.6440259893191, 35.5893998822247], 
          [-78.6439822720306, 35.5894438222519], 
          [-78.643942018672, 35.5894899177936], 
          [-78.6439053880286, 35.589537986896], 
          [-78.6438725247852, 35.5895878399172], 
          [-78.643843558597, 35.5896392800882], 
          [-78.6438186037862, 35.5896921044009], 
          [-78.6437977588449, 35.5897461043844], 
          [-78.643781106046, 35.5898010669274], 
          [-78.6437687111187, 35.5898567751198], 
          [-78.6437606229887, 35.589913009108], 
          [-78.643756873585, 35.589969546963],
          [-78.643757477714, 35.5900261655561], 
          [-78.6437624330007, 35.5900826414396], 
          [-78.6437717198978, 35.5901387517283], 
          [-78.6437853017631, 35.5901942749797], 
          [-78.6438031250037, 35.5902489920675],
          [-78.6438251192873, 35.5903026870463], 
          [-78.64486, 35.58999],
          [-78.6444771915128, 35.5891446843761] 
        ]
      ]
    }
  }'

polygon_sf = geojson_sf(polygon_json_str)

server <- function(input, output, session) {
  # Define a JSON string for a polygon
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = -78.644, lat = 35.589, zoom = 10) %>%
      addPolygons(
        data = polygon_sf$geometry,
      )
  })
}

shinyApp(ui, server)


But I can't get the analogous db-output result to do the same:

polygonData <- conn %>% tbl(in_schema(" SCHEMA  ", "POLYGONS" )) %>% as.data.frame();

geosf <- geojson_sf(polygonData$GEOMETRY)
...
addPolygons(map , geosf$geometry , layerid = NULL)

And I get: "Error in validateCoords: addPolygons requires numeric longitude/latitude values"

When I visually compare the two spatial frames I can see no appreciable difference:

Simple feature collection with 1 feature and 0 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -78.64486 ymin: 35.58914 xmax: -78.64376 ymax: 35.5903
Geodetic CRS:  WGS 84
                        geometry
1 POLYGON ((-78.64448 35.5891...

Simple feature collection with 9 features and 0 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -78.64595 ymin: 35.58913 xmax: -78.61305 ymax: 35.61195
Geodetic CRS:  WGS 84
                        geometry
1 POLYGON ((-78.61524 35.6110...
2 POLYGON ((-78.64435 35.5891...
3 POLYGON ((-78.64433 35.5892...
4 POLYGON ((-78.64411 35.5906...
5 POLYGON ((-78.64413 35.5906...
6 POLYGON ((-78.64595 35.5898...
7 POLYGON ((-78.64595 35.5898...
8 POLYGON ((-78.6138 35.61019...
9 POLYGON ((-78.61327 35.6116...

Why does the first one plot and the second one error?

Eddie Rowe
  • 102
  • 2
  • 10
  • Please read [1](https://stackoverflow.com/help/how-to-ask) how do I ask a good question and [2](https://stackoverflow.com/help/minimal-reproducible-example) or [3](https://stackoverflow.com/a/5963610/5784831) how to provide a minimal reproducible example in R. Then we can help you. – Christoph Aug 10 '23 at 17:15

0 Answers0