1

I set up a WMS with QGIS SERVER. I am querying it via a web app based on OpenLayers.

Everytime I click on my map, I send a GetFeatureInfo to a specific layer passing the coordinates where I clicked, and the response is rendered in a popup. So far so good.

The problem is if I click where there's no layer's feature in the map, I'd like no popup to show up, but instead I get an "empty-table-response" from the WMS which is in turn rendered in my popup, and this is annoying.

Here is an example of a request with some interesting data:

https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo&FORMAT=image%2Fpng&TRANSPARENT=true&QUERY_LAYERS=piste_sci&LAYERS=piste_sci&INFO_FORMAT=text%2Fhtml&I=50&J=50&CRS=EPSG%3A3857&STYLES=&WIDTH=101&HEIGHT=101&BBOX=1164063.3256063634%2C5842307.561011146%2C1165993.360570564%2C5844237.595975346

And here is one with empty data which I don't want to display in the popup:

https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo&FORMAT=image/png&TRANSPARENT=true&QUERY_LAYERS=piste_sci&LAYERS=piste_sci&INFO_FORMAT=text/html&I=50&J=50&CRS=EPSG:3857&STYLES=&WIDTH=101&HEIGHT=101&BBOX=1161292.4833310258,5842938.166494499,1163222.5182952264,5844868.201458699

This is how I am requesting the data in OL (only the relevant part):

function getInfoUrl(evt, view, lyr) {
    var coordinate = evt.coordinate;
    var url = CORS_PREFIX;
    var viewResolution = /** @type {number} */ (view.getResolution());
    if (lyr.get('type') != 'base') {
        var visible = lyr.getVisible();
        if (visible) {
            url += lyr.get('source').getGetFeatureInfoUrl(evt.coordinate, viewResolution, 'EPSG:3857', {
                'INFO_FORMAT': 'text/xml'
            });
            return url;
        }
    }
}

map.on('singleclick', function(evt) {
        // display info in fancyAlert
        for (let i = 0; i < ol_layers.length; ++i) {
            let layer = ol_layers[i];
            let text = getInfoUrl(evt, view, layer);
            if (text) {

                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        fancyAlert(this.responseText, 'info', 'Layer Info');
                    }
                };
                xhttp.open("GET", text, true);
                xhttp.send();
            }
        }
    })

How should I avoid the second response to show up? I thought a request to an empty feature area should trigger no response or at least a WMS exception. Maybe this is not a standard but it's what I was used to.

1

If it's not your own service it's unlikely you can change the output. In that specific case you could check if the output contains at least two html tables before displaying it

if (this.readyState == 4 && this.status == 200) {
    if (this.responseText.indexOf('</TABLE>') != this.responseText.lastIndexOf('</TABLE>')) {
        fancyAlert(this.responseText, 'info', 'Layer Info');
    }
}
  • it is my service. In that case, is there an alternative? Anyway, your solution might be good enough, I'll test it asap and tell you if it works fine for my case. – umbe1987 Jan 26 at 8:44
  • 1
    It seems similar to other WMS have I checked. Although the output varies between services it is always a status 200 response with valid html (typically comments and styles, but in your case a header table) and a client always needs to check if there is also displayable data embedded it to avoid an empty popup. – Mike Jan 26 at 10:52

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.