View on GitHub

Leaflet-WMS

Download this project as a .zip file Download this project as a tar.gz file

Leaflet-WMS

Travis master branch Travis develop branch stability-stable

OGC WMS client layer for leaflet. Adds GetFeatureInfo requests support to leaflet's L.TileLayer.WMS layer. Supports parsing of GetFeatureInfo responses into GeoJSON format from a variety of other formats.

Methods

getCapabilities

Performs GetCapabilities request to WMS-service on which instance of leaflet's L.TileLayer.WMS layer is configured. Returns element representing received XML-document describing WMS-service capabilities. Real request will be perfermed only at first time call, all subsequent calls will return the cached document element.

Options

Option name Is required Default value Description
done false - Callback which will be called if request succeeds
fail false function(errorThrown) {throw errorThrown;} Callback which will be called if request will fail
always false - Callback which will be called regardless of whether request succeed or not

Example

// Create leaflet map.
var map = new L.Map('map').setView([-41.59490508367679, 146.77734375000003], 7);

// Create & add OSM layer.
var osm = new L.TileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Create & add WMS-layer.
var tasmania = new L.TileLayer.WMS('http://demo.opengeo.org/geoserver/wms', {
  layers: 'topp:tasmania',
  format: 'image/png',
  transparent: true,
  version: '1.3.0',
  crs: L.CRS.EPSG4326
}).addTo(map);

// Perform 'GetCapabilities' request.
tasmania.getCapabilities({
  done: function(capabilities) {
  console.log('getCapabilitiessucceed: ', capabilities);
  },
  fail: function(errorThrown) {
  console.log('getCapabilitiesfailed: ', errorThrown);
  },
  always: function() {
  console.log('getCapabilitiesfinished');
  }
});

getInfoFormat

Performs GetCapabilities request to WMS-service on which instance of leaflet's L.TileLayer.WMS layer is configured, analyzes formats supported by WMS-service for GetFeatureInfo requests, compares them with the formats implemented in the plugin, and finally returns string representing most preferred format for GetFeatureInfo requests. Real request will be perfermed only at first time call, all subsequent calls will return the cached string.

Options

Option name Is required Default value Description
done false - Callback which will be called if request succeeds
fail false function(errorThrown) {throw errorThrown;} Callback which will be called if request will fail
always false - Callback which will be called regardless of whether request succeed or not

Example

// Create leaflet map.
var map = new L.Map('map').setView([-41.59490508367679, 146.77734375000003], 7);

// Create & add OSM layer.
var osm = new L.TileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Create & add WMS-layer.
var tasmania = new L.TileLayer.WMS('http://demo.opengeo.org/geoserver/wms', {
  layers: 'topp:tasmania',
  format: 'image/png',
  transparent: true,
  version: '1.3.0',
  crs: L.CRS.EPSG4326
}).addTo(map);

// Request preferred info format.
tasmania.getInfoFormat({
  done: function(infoFormat) {
  console.log('getInfoFormat succeed: ', infoFormat);
  },
  fail: function(errorThrown) {
  console.log('getInfoFormat failed: ', errorThrown);
  },
  always: function() {
  console.log('getInfoFormat finished');
  }
});

getFeatureInfo

Performs GetFeatureInfo requests. Returns GeoJSON FeatureCollection representing received features info. If preferred info format isn't defined in method options, call to getInfoFormat will be performed first.

Options

Option name Is required Default value Description
latlng true - Geographical point in which request must be performed. Must be instance of leaflet's L.LatLng
infoFormat false - String representing preferred info format. If option won't be defined, call to getInfoFormat will be performed first & returned string will be used as info format
featureCount false 1 Maximum number of features to return in response
done false - Callback which will be called if request succeeds
fail false function(errorThrown) {throw errorThrown;} Callback which will be called if request will fail
always false - Callback which will be called regardless of whether request succeed or not

Supported formats

Format Priority Description
applicaion/geojson 1 Some GIS servers can respond directly in GeoJSON format, so this format has the highest priority
'application/json' 2 Exists in GeoServer published WMS-services, also represents GeoJSON format
'application/vnd.ogc.gml' 3 Some GIS servers can respond in GML format which can describe features geometries & their attributive information as GeoJSON format, but in XML notation
'application/vnd.ogc.gml/3.1.1' 4 Same as 'application/vnd.ogc.gml', but another version of the specification
'application/vnd.ogc.wms_xml' 5 Represents features attributive information in XML notation, but without geometries
'text/xml' 6 Same as 'application/vnd.ogc.wms_xml' but with another mime
'text/html' 7 Represents features attributive information as HTML-table, but without geometries

Example

// Create leaflet map.
var map = new L.Map('map').setView([-41.59490508367679, 146.77734375000003], 7);

// Create & add OSM layer.
var osm = new L.TileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Create & add WMS-layer.
var tasmania = new L.TileLayer.WMS('http://demo.opengeo.org/geoserver/wms', {
  layers: 'topp:tasmania',
  format: 'image/png',
  transparent: true,
  version: '1.3.0',
  crs: L.CRS.EPSG4326
}).addTo(map);

// Perform 'GetFeatureInfo' request.
map.on('click', function(e) {
  tasmania.getFeatureInfo({
    latlng: e.latlng,
    done: function(featureCollection) {
    console.log('getFeatureInfosucceed: ', featureCollection);
  },
  fail: function(errorThrown) {
    console.log('getFeatureInfo failed: ', errorThrown);
  },
  always: function() {
      console.log('getFeatureInfo finished');
  }
  });
});

Demo examples

License

MIT