1

I am building a Shiny app which should display environmental layers on a Leaflet map, and I'd like some of them to be pulled from the Environment Agency's (UK) WMS service.

I can view the layer in QGIS, but have had no luck doing the same in R Leaflet.

One dataset I'd like to include is Risk of flooding from surface water and you can view the WMS info at: https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms?REQUEST=GetCapabilities&SERVICE=WMS

My first attempt has been with addWMSTiles() from leaflet, and then thinking this may not be a tiled service (how can I tell?) I also tried addWMS() from leaflet.extras2 package. Both functions work with examples supplied in the vignettes or by other SO users, but neither works with my layers.

      leaflet() %>% 
        setView(lng = -1.8, lat = 53.6, zoom = 10) %>% 
          addProviderTiles(providers$CartoDB.Positron) %>% 
          
      addWMSTiles("https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms",
        layers = "Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance",
        group = "Leaflet test",
        options = WMSTileOptions(format = "image/png",
                                 info_format = "text/html",
                                 srs = "EPSG:4326",
                                 transparent = TRUE))%>%
          
          addWMS(baseUrl = "https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms",
                 layers ="Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance",
                 group = "Leaflet Extra test",
                 options = WMSTileOptions(
                     transparent = TRUE,
                     format = "image/png",
                     info_format = "text/html")) %>% 

          addLayersControl(overlayGroups = c("Leaflet test", "Leaflet Extra test"))

I only see the background map (whether I preview my app in RStudio or browser): empty leaflet map

I also tried not setting the WMSTileOptions() at all, and conversely tried to specify the CRS (with srs = "EPSG:4326" which I think from the WMS GetCapabilities should be available). I think I may be specifying the URL or layer name wrong but I can't think what else to do.

For comparison, the QGIS output: correct QGIS map

2
  • Leaflet's default projection is EPSG:3857 related > gis.stackexchange.com/questions/48949/…
    – Mapperz
    Sep 22, 2021 at 17:47
  • @Mapperz ah see, I knew that and then forgot about it! Thanks for the reminder. But, given that my layer seems only available in EPSG:4326 or 27700 (British National Grid), is there anything I can do to capture the WMS map, reproject and display with Leaflet?
    – Sandy AB
    Sep 22, 2021 at 18:59

2 Answers 2

1

It seems that the problem is due to the name of the layer. Replace that by the ordinal number, and your script will work:

leaflet() %>% 
  setView(lng = -1.8, lat = 53.6, zoom = 10) %>% 
  addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
  
  addWMSTiles("https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms",
              layers = 1,
              group = "Leaflet test",
              options = WMSTileOptions(format = "image/png",
                                       info_format = "text/html",
                                       srs = "EPSG:4326",
                                       transparent = TRUE))%>%
  
  addWMS(baseUrl = "https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms",
         layers = 1,
         group = "Leaflet Extra test",
         options = WMSTileOptions(
           transparent = TRUE,
           format = "image/png",
           info_format = "text/html")) %>% 
  
  addLayersControl(overlayGroups = c("Leaflet test", "Leaflet Extra test")) 
1
  • Wow - so simple. Didn't realise you could use a number instead. Thanks!
    – Sandy AB
    Nov 11, 2021 at 17:12
0

There is a way to generate a leaflet web page from a QGIS project. Install the plugin qgis2web, then it will create a new icon on your toolbar named "Create web map", click on it and will open an interface to export your QGIS project to OpenLayers, Leaflet or Mapbox. Leave everything as default, just specify the path to export on "Export" tab and will generate code for you to consume the cited WMS service.

An example of code looks like this:

<script>       

        var mymap = L.map('mapid', {crs: L.CRS.EPSG4326}).setView([51.505, -0.09], 13);       

        mymap.createPane('pane_Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance_1');

        mymap.getPane('pane_Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance_1').style.zIndex = 401;

        var layer_Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance_1 = L.WMS.layer("https://environment.data.gov.uk/spatialdata/risk-of-flooding-from-surface-water-hazard-1-percent-annual-chance/wms", "1", {
            pane: 'pane_Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance_1',
            format: 'image/png',
            uppercase: true,
            transparent: true,
            continuousWorld : true,
            tiled: true,
            info_format: 'text/html',
            opacity: 1,
            identify: false,
            attribution: '',
        });
        mymap.addLayer(layer_Risk_of_Flooding_from_Surface_Water_Hazard_1_percent_annual_chance_1);
        setBounds();        

    </script>

In plane JS, i'm not sure if is compatible with R leaflet.

1
  • Thanks for this, however I am looking for a completely R-based solution if possible. I'd like my Shiny app to be able to request the information directly from the provider.
    – Sandy AB
    Sep 28, 2021 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.