Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've been searching the net for a couple of days to find a way, how to check if a wms layer has no features...

Let me explain

I'm using openLayers/geoserver/postgis. I'm using a cql_filter to search a wms layer. If the filter returns some features (points or lines or polygons) then i redraw the layer and they succesfully render.

But, if no features are rendered, I want to alert a message using javascript. The message simply gives some info, like "no matches found, please try again".

I guess I have to check if there are no features, and then alert the message. Right? Or is there another way to do this? Please help, I'm new to web-mapping....

Thanks

UPDATE Uhm, I'm not saying that your answers are bad, but... I was thinking, using a simple getFeatureInfo, with no feature.count parameter, after I redraw my layer. If the text that returns is empty, then there are no features rendered (= no matches found). I came up with this idea, playing with the demo requests on Geoserver, using the getFeatureInfo. The syntax they use there, returns all the features...So, I will do the same thing and if no features returned I get what I want... But how to check for an empty text? Or this idea is completly wrong?

share|improve this question
    
you say "if no features are rendered" but is this really what you want? Nothing rendered could mean there are results but scale-dependencies prevent them from being shown, or some features are outside the current viewing area. If you really just want to know when nothing is rendered you might be able to use some JavaScript (and maybe Canvas) trickery to check colour variation across the returned png. –  tomfumb Jan 15 '13 at 0:53
    
Ok, about the js/canvas trickery, can you please provide some extra info, how to do it, or how to search for an example, a starting point really... –  slevin Jan 15 '13 at 16:33
    
here is a starting point: stackoverflow.com/q/6816565/519575 I generally try to avoid giving suggestions and saying "... but this is a bad idea" - depending on your use case, browser platform, and data this could be appropriate but it seems to me like a last resort. It already smells a bit and I haven't even looked closely at it yet. –  tomfumb Jan 15 '13 at 17:43
    
Not all WMS layers will have the GetFeatureInfo request enabled, so using the results of a GetFeatureInfo request to determine whether or not a layer has any features is a fundamentally flawed methodology. –  nmtoken May 2 at 12:40
add comment

4 Answers 4

You should just do WFS request with resultType=hits. There is no need to create WFS layer, just shoot out GET query. Here is copy-paste from old code:

var wfsUrl = ...
var cqlFilter = ...
var typeName = ...

OpenLayers.Request.GET({
    url: wfsUrl,
    params: {
        SERVICE: 'WFS',
        VERSION: '1.1.0',
        REQUEST: 'GetFeature',
        TYPENAME: typeName,
        CQL_FILTER: cqlFilter,
        RESULTTYPE: 'hits'
    },
    success: function(response) {
        try {
            var xmlDoc = jQuery.parseXML(response.responseText);    
        } catch(err) {
            // Do something...
        }
        var count = $(xmlDoc).find('wfs\\:FeatureCollection').attr('numberOfFeatures');             
        if (typeof count == 'undefined') {
            // Something went wrong?
        } else if (count == 0) {
            // When using this CQL filter, there are no features, do something
        }
    }
});

Ofcourse, you need proxy, when your Geoserver is located on another host or port; and I hope you're familiar with WFS (what's typeName, etc).

share|improve this answer
add comment

I think geoserver has a list for how to do wfs, wms requests and depending on how your geoserver instance is hosted locally, you could do a get request for that wms layer and then if the layer returns null, catch the http request error and continue.

Or you could do a more precise lower level approach and query whatever postgis setup you have as far as if its hosted on a database and check for null values within there.

share|improve this answer
    
You mean a get request, after I redraw the layer, right?Is there a features.count method for WMS layers, or there is only for WFS? –  slevin Jan 14 '13 at 23:27
add comment

As far as I know it isn't possible to do this. A WMS returns a picture of your data so it is (roughly) the same size regardless of how many features are drawn, so there isn't really a clue as to how many features you matched.

If you are filtering a data layer and getting a small number of features back then a WFS makes more sense. A WFS returns the actual features to your client and would allow you to do things like count them before rendering.

share|improve this answer
    
I see you got a point...But I red that WFS is weathered and is going to be replaced by vector layers gradually. So, should I use vector instead of WFS? Changing the type of layers is kind a big deal right now, because I will have to edit thousand lines of code... –  slevin Jan 15 '13 at 16:30
    
do you mean a vector layer with a WFS protocol? if so then yes that is what you should use. –  iant Jan 15 '13 at 16:35
    
I was thinking, using a simple getFeatureInfo, with no feature.count parameter, after I redraw my layer. If the text that returns is empty, then there are no features rendered (= no matches found). I came up with this idea, playing with the demo requests on Geoserver, using the getFeatureInfo. The syntax they use there, returns all the features...So, I will do the same thing and if no features returned I get what I want... But how to check for an empty text? Or this idea is completly wrong? –  slevin Jan 16 '13 at 18:48
add comment

Another way to do this would be with an ajax call to a routine that ran the filter against your database. A little bit more coding server and client, but you would know for sure.

You could even extend this to do the ajax call before issuing the wfs request. Then you would know how much data to expect back if that was of any value. If nothing returned, don't issue the wfs request at all.

Cheers

Mark

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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