Thanks to visit codestin.com
Credit goes to github.com

Skip to content

addPolygons fails in case the geometry columns has names #595

@lbusett

Description

@lbusett

Adding polygons to a leaflet map derived from an sf object currently fails
(i.e., the polygons are not shown) in case the geometry column of the object has names.

So, for example, this fails:

library(shiny)
library(leaflet)
library(sf)
library(rmapshaper)
### Read in shapefiles  ###
polys_sf<-st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>% 
  st_transform(crs="+init=epsg:4326") %>% 
  ms_simplify(.)

ui<- fluidPage(
  leafletOutput("leaf"))
### Server ###
server<- function(input,output,session){
  ### This creates interactive map ###
  output$leaf<- renderLeaflet({
    leaflet(options = leafletOptions(minZoom = 7))%>%
      addPolygons(data= polys_sf)
  })
}

because ms_simplify adds names to the geometry column:

names(st_geometry(polys_sf))
#>   [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13"
#>  [15] "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27"
#>  [29] "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" "41"
#>  [43] "42" "43" "44" "45" "46" "47" "48" "49" "50" "51" "52" "53" "54" "55"
#>  [57] "56" "57" "58" "59" "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
#>  [71] "70" "71" "72" "73" "74" "75" "76" "77" "78" "79" "80" "81" "82" "83"
#>  [85] "84" "85" "86" "87" "88" "89" "90" "91" "92" "93" "94" "95" "96" "97"
#>  [99] "98" "99"

If however we remove the names by:

names(st_geometry(polys_sf)) = NULL

everything works ok.

Due to this, a check for existence and removal of names in the geometry column should therefore be implemented, probably in normalize-sf.R

(See also https://stackoverflow.com/questions/53227205/polygons-not-getting-plotted-in-leaflet-r-map-since-update and r-spatial/sf#880 (comment))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions