Adding WMS (IGN) to Leaflet

leafletproj4jswms

I'm using Leaflet to display agricultural plots obtain from a WMS.

Here is the link to the WMS : https://wxs.ign.fr/agriculture/geoportail/r/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities

Here's what I've done so far :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
    integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
    crossorigin=""/>
    <script src="https://unpkg.com/[email protected]/dist/leaflet.js"
    integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
    crossorigin=""></script>
    <script data-key="agriulture" src="\node_modules\geoportal-extensions-leaflet\dist\GpPluginLeaflet.js"></script>
    <script src="\node_modules\geoportal-extensions-leaflet\dist\GpPluginLeaflet.css"></script>
    <script src="\node_modules\proj4\dist\proj4.js"></script>
    <script src="\node_modules\proj4leaflet\src\proj4leaflet.js"></script>

    <title>TEST MAP</title>
    <style>
        #map { height: 500px; }
    </style>
</head>
<body>
    <div id="map"></div>
    <script>
        var map = L.map('map',{crs : L.CRS.EPSG3857}).setView([45.5 , 4.5], 13);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            maxZoom: 19,
            attribution: '&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
        }).addTo(map);

        var wmsLayer = L.tileLayer.wms('https://wxs.ign.fr/agriculture/geoportail/r/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities', {
            layers: 'LANDUSE.AGRICULTURE2020',
        }).addTo(map);

    </script>
</body>  
</html>

The OSM tile is displaying well, but not the WMS layer.

In the console, I get this error :

wms:1 GET https://wxs.ign.fr/agriculture/geoportail/r/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities&service=WMS&request=GetMap&layers=LANDUSE.AGRICULTURE2020&styles=&format=image%2Fjpeg&transparent=false&version=1.1.1&width=256&height=256&srs=EPSG%3A3857&bbox=494088.9508353794,5699144.828942743,498980.9206456307,5704036.798752994
400 (BadRequest)

<ServiceExceptionReport xmlns="http://www.opengis.net/ogc">
<script> try { Object.defineProperty(navigator, "globalPrivacyControl", { value: false, configurable: false, writable: false }); document.currentScript.parentElement.removeChild(document.currentScript); } catch(e) {}; </script>
<ServiceException code="MissingParameterValue"> Parametre CRS absent. </ServiceException>
</ServiceExceptionReport>

As there is a CRS params missing, I've tried something with proj4 and proj4leaflet but without success.

Best Answer

You are making a WMS version 3 request, which needs to use a CRS parameter while you are sending an SRS parameter.

So either use version 1.1.1 and an SRS parameter or change the SRS to a CRS.

In general the best practice is to allow the client code to negotiate the version with the server (so leave the version parameter out of the getCapabilitites request). Though with out checking the source of the leaflet plugin I can't say if this would work in this case.

Related Question