Search code examples
rshinyr-leaflet

Cause of warning when trying to simplify polygons?


I've got a leaflet map and I'm plotting some polygons on top of it. I'm trying to use rmapshaper::ms_simplify() to decrease the loading time for the map.

    polygons_ll <- (
      spTransform(polygons, CRS("+proj=longlat +datum=WGS84"))
    )

    polygons_ll <- ms_simplify(polygons_ll)

This is all within a R shiny application, which doesn't run after encountering the warning:

Warning in sp::proj4string(sp) :
  CRS object has comment, which is lost in output

The app was functioning before the addition of the ms_simplify().


Solution

  • After doing some research and trial and error:

    rgdal::set_rgdal_show_exportToProj4_warnings(FALSE)
    

    will suppress the warning.

    I ended up using:

    crs <- CRS("+proj=longlat +datum=WGS84")
    
    polygons_ll <- polygons %>% 
        rgeos::gSimplify(tol=25, topologyPreserve=TRUE) %>% 
        spTransform(crs)
    
    # overwrite untransformed polygons with transformed polygons
    polygons@polygons <- polygons@polygons
    

    Maps are much faster to load now.