1

I am working with Openlayers to consume WMS from GeoServer. I have loaded multiple layers and get attributes for one layer using getGetFeatureInfoUrl. But this approach gets attributes for the specific layer on which getGetFeatureInfoUrl method is called.

I have overlapping layers , hence I need to get attributes from all overlapping layer at a specific point.

Below is how I am doing it :

var wmsSource = new ol.source.TileWMS({
    url: url,
    params: { 'LAYERS': 'localhost:village_maps', 'TILED': true },
    serverType: 'geoserver',
    // Countries have transparency, so do not fade tiles:
    transition: 0,
    transparent: true,
    opacity: 0.5,
    //format: new ol.format.GeoJSON()
});
map.on('singleclick', function (evt) {

      var viewResolution = (view.getResolution());
      var url = wmsSource.getGetFeatureInfoUrl(
        evt.coordinate, viewResolution, 'EPSG:3857',
        { 'INFO_FORMAT': 'application/json'}); //text/html

      if (url) {
          var parser = new ol.format.GeoJSON();
          var lookup = {};
          $.ajax({
              url: url,
              dataType: 'json',
              method: 'GET',
              success: function (response) {
                  var result = parser.readFeatures(response);
                  //if ((result.length > 0) && (wmsLayer.getVisible() == true)) {
                  if (result.length > 0)
                  {
                      var coord = evt.coordinate;
                      var content = '<p>';

                      for (var i = 0, ii = result.length; i < ii; ++i)
                      {
                          // Gets attributes from wmsSource only, But I have multiple other sources as well !
                          var text = result[i];
                          var props = text.getProperties();
                          var txt = text.getId();
                          content = content + '<strong>ID:' + txt + '</strong><br>';
                          content = content + '<strong>District:' + props.district + '</strong><br>';

                      }

                      content = content + '</p>';
                      content_element.innerHTML = content;
                      overlay.setPosition(coord);
                  }
              }
          });
      }        
  });

Is it like for every layer I need to url again and then render the json ? or is there a better way to accomplish this.

  • Instead of multiple OpenLayers TileWMS layers you can specify a list of WMS layer names from the same service in the LAYERS params option of a single OpenLayers TileWMS layer. You can also specify multiple WMS layer names in in the getGetFeatureInfoUrl QUERY_LAYERS params option. – Mike Aug 24 at 10:01
  • Can you please share an example to add multiple layers with Single TileWMS layer , Will I be able to hide individual layer then ? For now I am adding multiple layer like : var layers = [ EsriLayer, OSMLayer, VillaMapLayer, NalaLyr, WBMLyr, LTLLyr, HTLLyr, BTLLyr ]; and then map like : var map = new ol.Map({ layers: layers, target: 'map', view: view }); I am able to hide individual layers setting its visibility to false. – user684805 Aug 24 at 10:30
2

Example of multiple WMS layers in a single OpenLayers layer

For this service http://ogc.bgs.ac.uk/cgi-bin/BGS_Bedrock_and_Superficial_Geology/wms?service=WMS&version=1.3.0&request=GetCapabilities this would display 2 of the 6 sublayers:

    source: new ol.source.TileWMS({
        url: 'http://ogc.bgs.ac.uk/cgi-bin/BGS_Bedrock_and_Superficial_Geology/wms',
        params: {
            'FORMAT': 'image/png',
            'LAYERS': 'GBR_BGS_625k_BLS,GBR_BGS_625k_SLS',
            'TRANSPARENT': 'TRUE'
        },
        attributions: bgsAttrib,
    }),

To show and hide individual WMS layers update the LAYERS parameter, for example to hide the SLS layer and only show BLS:

source.updateParams({'LAYERS': 'GBR_BGS_625k_BLS'});
  • Thanks ! this worked .. Here In this case if I update params and hide a layer then in my getGetFeatureInfoUrl method also I need to update the layers .. Is there any way that I can all visible layers at that time and assign them to 'QUERY_LAYERS'. Also , Geoserver returns entire table columns can I configure Geoserver in a way to return only desired columns ? Thanks in advance ! – user684805 Aug 24 at 11:40
  • My bad ! I dont even need to mention 'QUERY_LAYERS' as my source itself have multiple layers. Without specifying 'QUERY_LAYERS' it worked with hidden layer as well. – user684805 Aug 24 at 11:45
  • Can you please assist me with response returned by GeoServer , it sends entire table column rather some specific columns needed. – user684805 Aug 24 at 11:47
  • These links might help if you want to use info_format=text/html mollweide.wordpress.com/2013/08/01/… docs.geoserver.org/latest/en/user/tutorials/GetFeatureInfo/… If using info_format=application/json some servers may support the propertyName parameter docs.geoserver.org/latest/en/user/services/wms/… – Mike Aug 24 at 12:20

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.