OpenLayers CQL Filter – How to Use cql_filter in OpenLayers.Control.WMSGetFeatureInfo

cql-filtergeoservergetfeatureinfoopenlayers-2

I am a newbie and I am using OpenLayers, GeoServer 2.1.3. I have a
function that works to filter the markers on the map with a CQL_FILTER
function. But when I click on a marker it shows a popup with
featureInfo information. If there are hidden markers on the same
coordinate (from the CQL_FILTER function) every marker shows a popup,
even if I have filtered them out (I thought until now that is!) .

So I expect after what I have researched that I also need to filter
the getFeatureInfo click that sends the cql_filter with the WMS call.
The markers are on the same layer but I only want a popup to fire on
the filtered markers. Any advice to where I start would be be
appreciated. Here is my function that redraws the layers with the
params from a search box. So how do I use this also in my click event?

function CQLfilter(sok,cat){
var mLayers = map.layers;
switch (cat)
{
case 'SN':
 param = "scientificname LIKE '%"+ sok + "%' ";
 break;
case 'NNG':
 param = "norsknavngruppe LIKE '%"+ sok + "%' ";
 break;
case 'NNA':
 param = "norsknavnart LIKE '%"+ sok + "%' ";
 break;
case 'PROJ':
 param2 = new Array()
 param2.push("prosjektnummer LIKE '%" + sok + "%'")
 param2.push("prosjektnavn LIKE '%" + sok + "%'")
 param = param2.join(" OR ");
 break;
default:
 param2 = new Array()
 param2.push("scientificname LIKE '%" + sok + "%'")
 param2.push("norsknavngruppe LIKE '%" + sok + "%'")
 param2.push("norsknavnart LIKE '%" + sok + "%'")
 param2.push("collector LIKE '%" + sok + "%'")
 param = param2.join(" OR ");
 break;
}
for(var a = 4; a < (mLayers.length); a++ ){
 mLayers[a].mergeNewParams({'CQL_FILTER': param});
 mLayers[a].setVisibility(true);
}}

### FROM HERE IS WHERE I NEED HELP!!

Here is my code that generates the popup and the wmsGetFeatureInfo. I
guess it is here I must also do some kind of filtering on the wms
request but not sure how to do it. How and where do I use the code
from the CQLfilter function?

         info = new OpenLayers.Control.WMSGetFeatureInfo({
         url: 'http://kart.naturkart.no/geoserver/wms',
         title: 'Identify features by clicking',
         queryVisible: true,
         infoFormat:'application/vnd.ogc.gml',
         eventListeners: {
             getfeatureinfo: function(event) {
                 if (popup) {
                     map.removePopup(popup);
                 }
                 var contentHtml = '';
                 // Manage the features
                 if (event.features.length > 0) {
                     for (var i = 0; i < event.features.length; i++) {
                         var feature = event.features[i];
                         // Identify the type
                         if (feature.gml.featureType ==
'ikke_rodlistet' || feature.gml.featureType == 'kritisk_truet' ||
feature.gml.featureType == 'nar_truet' || feature.gml.featureType ==
'regionalt_utdodd' ||
                                 feature.gml.featureType == 'saarbar' ||
                                 feature.gml.featureType == 'sterkt_truet') {
                             // Create HTML content for this feature type
                             // Fetch the feature attributes and some
conditional output of the popup - could be useful to someone :-)
                             var locality = feature.attributes['locality'];
                             country = feature.attributes['country'];
                             scientificname =
feature.attributes['scientificname'];
                             kingdom = feature.attributes['kingdom'];
                             norsknavnart =
feature.attributes['norsknavnart']!= null ?
feature.attributes['norsknavnart'] + ' | ':'';
                             norsknavngruppe =
feature.attributes['norsknavngruppe']!= null?
feature.attributes['norsknavngruppe'] + ' | ':'';
                             collector =  feature.attributes['collector'];
                             kommentar =
feature.attributes['kommentar']!= null?
feature.attributes['kommentar'] + ' | ':'';
                             dateinterval = feature.attributes['dateinterval'];
                             status = feature.attributes['status'];
                             statusimg = '<img
src="/images/svg/bab_'+status+'.svg" width="10" />'; // Conditionally
show a (status) icon in the popup
                             contentHtml = contentHtml + '<h2>' +
scientificname +' '+ statusimg + '</h2>';
                             contentHtml = contentHtml +norsknavnart
+ norsknavngruppe + ' Reg.dato: ' + dateinterval + '<br/>';
                             contentHtml = contentHtml + kommentar +
' Reg. av: ' + collector + '<br />';
                             contentHtml = contentHtml + '<span
class="popItem"><a
href="http://babkart.no/?x='+feature.attributes['x']+'&amp;y='+feature.attributes['y']+'">Direkte
link</a> til dette artsfunn.</span><hr />';
                             }
                     }
                 } else {
                     // Don't show any popup if no features.
                     return; alert(map.getLonLatFromPixel(event.xy));
                 }
                 var popup = new Popup(
                         "chicken",
                         map.getLonLatFromPixel(event.xy),
                         null,
                         contentHtml,
                         null,
                         true
                         );
                 popup.autoSize = true;
                 map.addPopup(popup, true);
                 Ext.ux.Lightbox.register("a[rel^=lightbox]",true);
             }
         }
     });
     map.addControl(info);
     info.activate();

~asle benoni

Best Answer

did you pass the CQL filter to the WMS GetFeatureInfo request too? If not then any record in the whole dataset at that location will be returned.

Related Question