0

I have two layers of WMS layers stacked on top of each other and each contains its own feature information. Upper layer has style=line and bottom layer has style=color, and some lines of the upper layer will cover the features of the layer below, like this:

enter image description here

Each color of the layer below corresponds to a separate feature (skin color = "features":[{"type":1..., grey = "features":[{"type":2..., red = "features":[{"type":3...). I want when I click on the feature of the upper layer, it will show the FeatureInfo of the upper layer and three FeatureInfo of the layer below to table and show marker number in feature of below layer, maybe look like this:

enter image description here

How do i get the information of the lower layers that are in the area feature of the upper layer?

Relevant existing code:

Add layer to map:

//below layer
var belowLayer= new ol.layer.Image({
            view: new ol.View({
            projection: 'EPSG:4326'
            }),
            source: new ol.source.ImageWMS({
            url: 'http://localhost:8081/geoserver/dbpgis/wms',
            params: {LAYERS: 'dbpgis:belowlayer', 'TILED': true},
            serverType: 'geoserver',
            ratio: 1,
            }),
            visible: true,
            title: 'below'
        });
        map.addLayer(belowLayer)

       //upper layer
        var view = new ol.View({
            projection: 'EPSG:4326',
        });
        var source = new ol.source.ImageWMS({
            url: 'http://localhost:8081/geoserver/dbpgis/wms',
            params: {LAYERS: 'dbpgis:upperlayer', 'TILED': true},
            serverType: 'geoserver',
            ratio: 1,
            format: new ol.format.GeoJSON()
        });
        var upperLayer= new ol.layer.Image({
            view: view,
            source: source,
            // minZoom: 17,
            visible: true,
            title: 'upper',
        });
        map.addLayer(upperLayer);

getFeatureInfo:

//get featureinfo for upper layer
var content = document.getElementById('info');     
        map.on('click', function (evt) {
            overlayPopup.hide();
            content.innerHTML = '';
            var viewResolution = mapView.getResolution();
            var url = source.getFeatureInfoUrl(
                evt.coordinate,
                viewResolution,
                'EPSG:3857',
                {
                    'INFO_FORMAT': 'application/json',
                    'propertyName': 'feature_upper' 
                },
            );
            if (url) {
                $.getJSON(url, function(data){
                    var feature = data.features[0];
                    var props = feature.properties;
                    content.innerHTML = "<h5>Upper layer : </h5><p>" + props.feature_upper +"</p>"
                                        
                    // overlayPopup.show(content.innerHTML);
                    // console.log(props);
                })
            }else{
                alert('error');
            }
            // console.log(url);
        });  

        //get featureinfo of below layer
        var dataBelow = document.getElementById('info_qpk');
        map.on('click', function(evt){
            dataBelow .innerHTML = '';
            viewData = map.getView();
            var url = belowLayer.getSource().getFeatureInfoUrl(
            evt.coordinate,
            viewData.getResolution(),
            'EPSG:3857',
            {
            'INFO_FORMAT': 'application/json',
            'propertyName':'feature_type_1,...'
            },
            );
            // console.log(url);
            if(url){
            $.getJSON(url, function(data){
                var feature = data.features[0];
                var props = feature.properties;
                dataQPK.innerHTML = "<h5>Below layer : </h5><p>" + props.feature_type_1+ "</p>" + "<hr>"+...
                // overlayPopup.show(dataBelow.innerHTML);
            });
            }
        })

Here is my code, this only get data info at click position.

9
  • Are all the WMS layers from the same service? If so certainly doable with WMS GetFeatureInfo request, you need to list all the layers that make up the GetMap request (comma separated list in LAYERS), then for all the layers you want to query you need to list them in the QUERY_LAYERS parameter.
    – nmtoken
    Sep 14, 2022 at 9:15
  • @nmtoken Yes, all the WMS layer from GeoServer. But currently only get the farture information at the mouse click position. I want to get information of the lower layers in the feature area of the upper layer
    – Jayzz
    Sep 14, 2022 at 9:29
  • 1
    By default GetFeatureInfo only works on a point location in the map image, GeoServer offers a 'vendor option' buffer with width in pixels, so you may be able to get feature information beyond your clicked point.
    – nmtoken
    Sep 14, 2022 at 9:30
  • @nmtoken can it get information of the below layer features (skin color = "features":[{"type":1..., grey = "features":[{"type":2..., red = "features":[{"type":3...) in the feature area of the upper layer?
    – Jayzz
    Sep 14, 2022 at 9:34
  • 1
    There's also 'vendor option' cql_filter where you can specify geometry, see docs.geoserver.org/latest/en/user/services/wms/… and docs.geoserver.org/latest/en/user/filter/…
    – TomazicM
    Sep 14, 2022 at 10:02

1 Answer 1

0

If you have access to the Geoserver in question, why not create a layer group from both layers, and send the getFeatureInfo request to the layer group?

To quote the documentation,

Note: If you are sending a GetFeatureInfo request against a layergroup, all the layers in that layergroup must be set as “Queryable” to get a result (See WMS Settings on Layers page)

Response will be a dictionary with features from both layers at the particular location. You only need to parse them accordingly.

2
  • Thanks for your answer. But my two layers contain different columns of data so when grouping the layer i can't query the propertyName I want because they are different. And this only get data info at click position. How do i click on the feature area of the upper layer but still get the features of the lower layer that are in that area? Thanks you a lot again.
    – Jayzz
    Sep 15, 2022 at 2:08
  • 1
    No need to create a group layer, you can query separate layers in a single GetFeatureInfo request
    – nmtoken
    Sep 15, 2022 at 7:25

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.