3

I have multiple layers in a map with layer group now onclick I want to get the GetFeatureInfoUrl for the different layers on which I am clicking but the problem is that I am getting only the Info for the last layer appended not the others.

Here is my code:

 var wms_layer;
    var map = new ol.Map({
        target: 'map',
        layers: [
        new ol.layer.Group({
            'title': 'Base maps',
            layers: [
            new ol.layer.Tile({
                title: 'Water color',
                type: 'base',
                visible: false,
                source: new ol.source.Stamen({
                    layer: 'watercolor'
                })
            }),
            new ol.layer.Tile({
                title: 'OSM',
                type: 'base',
                visible: true,
                source: new ol.source.OSM({
                    attributions: [
                    'All maps © <a href="https://www.opencyclemap.org/">OpenCycleMap</a>',
                    ol.source.OSM.ATTRIBUTION
                    ],
                    url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png'
                })
            })
            ]
        }),
        new ol.layer.Group({
            title: 'Hisar Data Layers',
            layers: [
            new ol.layer.Tile({
                title: 'Habitations',
                source: new ol.source.TileWMS({
                    url: 'http://192.168.2.2:8080/geoserver/HissarFullData/wms?service',
                    params: {
                        'LAYERS': 'HissarFullData:habitations',
                        'VERSION': '1.1.0',
                        'FORMAT': 'image/png',
                    },
                    serverType: 'geoserver'
                })
            }),
           wms_layer = new ol.layer.Tile({
                title: 'Hisar PWD',
                source: new ol.source.TileWMS({
                    url: 'http://192.168.2.2:8080/geoserver/HissarFullData/wms?service',
                    params: {
                        'LAYERS': 'HissarFullData:pwd',
                        'VERSION': '1.1.0',
                        'FORMAT': 'image/png',
                    },
                    serverType: 'geoserver'
                })
            }),
            new ol.layer.Tile({
                title: 'Hisar Block',
                source: new ol.source.TileWMS({
                    url: 'http://192.168.2.2:8080/geoserver/HissarFullData/wms?service',
                    params: {
                        'LAYERS': 'HissarFullData:block_boundary',
                        'VERSION': '1.1.0',
                        'FORMAT': 'image/png',
                    },
                    serverType: 'geoserver'
                })
            })
            ]
        })
        ],
        view: new ol.View({
            center: ol.proj.transform([75.7217, 29.1492], 'EPSG:4326', 'EPSG:3857'),
            zoom: 10,
            minZoom: 5,
            maxZoom: 17
        })
    });
    var layerSwitcher = new ol.control.LayerSwitcher({
        tipLabel: 'Layers' // Optional label for button
    });
    map.addControl(layerSwitcher);
    var viewProjection = map.getView().getProjection();
    var viewResolution = map.getView().getResolution();
    map.on('click', function (evt) {
        var url = wms_layer.getSource().getGetFeatureInfoUrl(
            evt.coordinate, viewResolution, viewProjection,
            {
                'INFO_FORMAT': 'text/html',
            });
        if (url) {
            document.getElementById('info').innerHTML =
            '<iframe style="width:100%;height:110px;border:0px;" id="iframe" seamless src="' + url + '"></iframe>';
            $('#myModal').modal('show');
        }
    });

Whenever I click on the map I always getting info of the HissarFullData:block_boundary layer. I am using Layer switcher with this .

8
  • Try if adding &feature_count=10 helps. Have you checked that query has all the layers listed in &layers and in &query_layers? You can use Firebug or similar for that.
    – user30184
    Feb 8, 2017 at 7:01
  • Hi let me check will ping back Feb 8, 2017 at 7:06
  • @user30184 nothing changed.. in console it showing last year name only on click .. click function i have used is right ?? Feb 8, 2017 at 7:09
  • You must first make sure that you have a list of all layers in "layers" and "query_layers" in the request that your code generates. Something related in gis.stackexchange.com/questions/24711/….
    – user30184
    Feb 8, 2017 at 7:29
  • @user30184 i have list of all layers now what to do next Feb 8, 2017 at 7:37

2 Answers 2

3

As mentioned by @user30184 in comments the important parameter here is QUERY_LAYERS&

To do a GetFeatureInfo request for multiple layers you need to list all those layers you want information on in the QUERY_LAYERS& parameter.

So consider these queries:

In this the GetMap request is based on the GBR_BGS_625k_BLS layer LAYERS=GBR_BGS_625k_BLS& and the GetFeatureInfo request queries for information on the same layer QUERY_LAYERS=GBR_BGS_625k_BLS&

http://ogc.bgs.ac.uk/cgi-bin/BGS_Bedrock_and_Superficial_Geology/wms?REQUEST=GetFeatureInfo&SERVICE=WMS&VERSION=1.3.0&LAYERS=GBR_BGS_625k_BLS&STYLES=default&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&CRS=EPSG:4326&BBOX=49.3249168839067,-10.3982024209475,61.0455548096319,3.17166214686146&WIDTH=1020&HEIGHT=881&I=736&J=652&info_format=text/html&QUERY_LAYERS=GBR_BGS_625k_BLS&

In the next example the GetMap request is the same (it serves the purpose of providing an image to be used to provide an image coordinate I=736&J=652& but this time we want information for three layers QUERY_LAYERS=GBR_BGS_625k_BLS,GBR_BGS_625k_SLT,GBR_BGS_625k_BLT&

http://ogc.bgs.ac.uk/cgi-bin/BGS_Bedrock_and_Superficial_Geology/wms?REQUEST=GetFeatureInfo&SERVICE=WMS&VERSION=1.3.0&LAYERS=GBR_BGS_625k_BLS&STYLES=default&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&CRS=EPSG:4326&BBOX=49.3249168839067,-10.3982024209475,61.0455548096319,3.17166214686146&WIDTH=1020&HEIGHT=881&I=736&J=652&info_format=text/html&QUERY_LAYERS=GBR_BGS_625k_BLS,GBR_BGS_625k_SLT,GBR_BGS_625k_BLT&

You don't have to query the layer that is used to generate the GetMap request even, so GetMap is based on GBR_BGS_625k_BLS, but GetFeatureInfo is for GBR_BGS_625k_SLT and GBR_BGS_625k_BLT

http://ogc.bgs.ac.uk/cgi-bin/BGS_Bedrock_and_Superficial_Geology/wms?REQUEST=GetFeatureInfo&SERVICE=WMS&VERSION=1.3.0&LAYERS=GBR_BGS_625k_BLS&STYLES=default&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&CRS=EPSG:4326&BBOX=49.3249168839067,-10.3982024209475,61.0455548096319,3.17166214686146&WIDTH=1020&HEIGHT=881&I=736&J=652&info_format=text/html&QUERY_LAYERS=GBR_BGS_625k_SLT,GBR_BGS_625k_BLT&

So in your example case if you want all layers to be queried independently of which WMS layer is clicked the code may look something like:

map.on('click', function (evt) {
    var url = wms_layer.getSource().getGetFeatureInfoUrl(
        evt.coordinate, viewResolution, viewProjection,
            {
                'INFO_FORMAT': 'text/html',
                'QUERY_LAYERS': 'HissarFullData:block_boundary,HissarFullData:habitations,HissarFullData:pwd'
            });
        ...
11
  • 1
    how do you build this request using getGetFeatureInfoUrl? from what I can see this can only be used with one source at a time.
    – JasonBK
    Dec 6, 2017 at 14:18
  • 2
    A single GetFeatureInfo request can only be used against a single web service. You can though query multiple layers in that source. If you have multiple sources (different services) then you'll need a different GFI for each source. One way around this would be to set up you own WMS and cascade all your separate WMS sources through this, in other words create your own single source
    – nmtoken
    Dec 6, 2017 at 14:49
  • ok thanks..I am hoping to find a way to solve the original question's problem from the front end, using Open Layers 3..I wont necessarily have control over the web map services
    – JasonBK
    Dec 6, 2017 at 15:28
  • My get around option doesn't require you to have any control of the original services, just some server with WMS software configured. The original question only references a single WMS source
    – nmtoken
    Dec 6, 2017 at 16:20
  • 1
    For this to work, you'll also need FEATURE_COUNT parameter set to the number of layers you want. May 18, 2018 at 10:56
1

You have to use the LAYERS params if you want to filter your layers with QUERY_LAYERS, here is how i handle it

   clickHandler(event) {
    const viewResolution = this.mMap.getView().getResolution();
    const viewProjection = this.mMap.getView().getProjection();
    const { layers } = this.props
    var coordinate = event.coordinate;

    var url = ""
    let content = this.popupContent
    let popup = this.popup
    let queryLayers = ''

    if(layers){
        layers.forEach(function(lyr,i){
            queryLayers += `cahibi1:${lyr.Title}${(i<layers.length-1)?',' :''}`
        })
        console.log(queryLayers)
    }
    this.mMap.getLayers().forEach(function (lyr) {
        if (lyr.getSource()["getGetFeatureInfoUrl"] !== undefined) {

            url = lyr.getSource().getGetFeatureInfoUrl(
                coordinate, viewResolution, viewProjection,
                { 
                    'INFO_FORMAT': 'text/html' ,
                    'QUERY_LAYERS':queryLayers,
                    'LAYERS':  queryLayers
                });
            console.log('url', url)
            if (url) {
                popup.setPosition(coordinate);
                content.innerHTML =
                    '<iframe seamless src="' + url + '"></iframe>';
            }
        }
    })
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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