I am having difficulty adding a layer of polygons to my existing leaflet map

33 Views Asked by At

I have created a leaflet map, "plot" that runs pretty well. I needed to add a new layer of polygons once the map fly's to selected coordinates. After a drop-down input is selected, that is, "county", my leaflet map flies to that county's coordinates. To display new polygon data, I thought I could simply use addPolygons using selected data but the app doesn't run and produces the following warnings:

"Warning: sf layer is not long-lat data Warning: Error in to_ring.default: Don't know how to get polygon data from object of class XY,POINT,sfg".

Here is part of my code:

#a reactive expression for schools selection 
selected_schools <- reactive({
schools_mapping %>% filter( County == 
selected_county() & LEVEL == selected_level() 
& Status == selected_type()) 
})

#a reactive that selects county coordinates
 selected_coords <- 
reactive({ 
county_mapping_primary[
county_mapping_primary$COUNTY == 
input$county_dropdown, c("lon","lat")]
})




#Observe the dropdown input change and 
 highlight/fly to selected county 
 observeEvent(selected_county(),{

leafletProxy("plot", session) %>%
  flyTo(lng=unique(selected_coords()$lon), lat=unique(selected_coords()$lat), zoom=11) %>% #fly to selected county
  addPolygons(data= selected_schools(),#add schools layer data
              fillColor =  "white", 
              color = "#BDBDC3",
              fillOpacity = 1,
              weight = 1)

})

I have checked the data format using st_crs and its in WGS84 format. Here is a snapshot of the data being selected from in my code "selected_schools"enter image description here. It contains geometries and is correctly formated

0

There are 0 best solutions below