I use GeoServer to get my WMS data. Everything works great for showing data in OpenLayers.

GeoServer has the ability to create SQL views and use parameters to return certain geometry. These parameters are called viewparams. This could be an example of a SQL view:

select *
from myTable
where age = %ageParam%

When using viewparams you are able to pass the value for the 'ageParam' parameter. If a parameter is not set, a predefined default value will be used. More information about this topic can be found here.

In OpenLayers however, "getFeature" control does not take the 'viewparams' parameter into account. This means it always returns features with the default parameter values.

Is this a bug, a missing feature or something I just missed.

Edit 1

Here is a screencast I made to visualise the problem.


As you can see, the layer is redrawn with new points but the selectFeature control still returns the old features.

Edit 2

<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Query typeName="test:point_query" xmlns:test="http://test.boedy.p:8080/wms">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<gml:Envelope xmlns:gml="http://www.opengis.net/gml">
<gml:lowerCorner>490488.82657346 6800062.1058854</gml:lowerCorner>
<gml:upperCorner>491071.65891403 6800556.5579121</gml:upperCorner>

**edit **

This is the JavaScript code for the layer and controller

test = new OpenLayers.Layer.WMS("test",
        'layers': 'test:test_query',
        viewparams: 'x:'+myX+';y:'+myY,
        maxfeatures: 2000,
        ratio: 1,
        transparent: true,
        isBaseLayer: false}

selectCtrl = new OpenLayers.Control.GetFeature(
            protocol: OpenLayers.Protocol.WFS.fromWMSLayer(test),
            clickout: true, toggle: false,
            multiple: false, hover: false,
            box: true,
            toggleKey: "shiftKey", // ctrl key removes from selection
            multipleKey: "shiftKey",
            eventListeners: {
                featureselected: onFeatureSelect,
                featureunselected: onFeatureUnselect
  • can you post an example url?
    – Ian Turton
    Oct 26, 2011 at 15:29
  • Please don't put tags in the title.
    – underdark
    Oct 26, 2011 at 21:46
  • @iant I can't give you an example url, because It's still in development and only locally accessible. I've added a short video displaying the problem instead. I hope that's of some use.
    – Boedy
    Oct 27, 2011 at 7:16
  • I meant could you post an example of the URL that is being sent by the client (preferably to a default layer) so we can try to reproduce the problem locally.
    – Ian Turton
    Oct 27, 2011 at 7:34
  • @iant Edit2 displays the payload. I'm not sure if that's the URL you were talking about.
    – Boedy
    Oct 27, 2011 at 7:57

2 Answers 2


Looking at the source of the WFS.fromWMSLayer function, it looks like you should pass in your viewparams as an additional option:

        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(test, 
                                                       {viewparams: 'x:'+myX+';y:'+myY}),

Check the URL that is called when you select a feature - and hopefully the viewparams will be added to the URL.

If you wanted to apply a filter to a WFS layer you can play around with the filter code and the XML it generates on this demo.

Once you have the filter code correct then set the WFS layer's filter filter property.

        wfsLayer.filter = my_filter;

As you don't have a WFS layer you could try the following - the parameters may be copied across - you'd have to test.

        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(test, 
                                                       {filter: my_filter}),
  • This doesn't seem to have any effect. I'm doubting if this is possible in openlayers or with the WFS protocol at all. I was able to get viewparams working with WMS, but not with WFS.
    – Boedy
    Oct 28, 2011 at 10:22
  • To clarify - the viewparams need to be sent as part of the URL or as part of the OGC Filter XML for it to work correctly? Oct 28, 2011 at 10:24
  • I'm not sure. I'm guessing it should be a part of the payload/OGC Filter. Geoserver suggests it's possible with WFS, but doesn't give an example on it's usage.
    – Boedy
    Oct 28, 2011 at 11:06
  • Are you suggesting I should switch to filters? So instead of filtering via sql use an openlayers filter?
    – Boedy
    Oct 28, 2011 at 13:52
  • An OL filter is simply XML that is sent to the server where the actual filtering occurs. OL itself doesn't filter the features. Oct 28, 2011 at 14:11

Using the WMSGetFeatureInfo method, the way to pass along viewparams is by using

  • vendorparams and
  • and assigning the viewparams in the eventlistener before the callback function retrieving the "click result" from the WMS layer

    click_info = new OpenLayers.Control.WMSGetFeatureInfo({
        title: 'Identify features by clicking',
        layers: queryableMapLayers,
        infoFormat: "application/vnd.ogc.gml",
        queryVisible: true,
        autoActivate: true,
        eventListeners: {
            beforegetfeatureinfo: function(event) { 
            this.vendorParams = { viewparams: "word:"+startWord}; 
            getfeatureinfo: function(e) {
                var items = [];
                Ext.each(e.features, function(feature) {
                        xtype: "propertygrid",
                        title: feature.attributes.text,
                new GeoExt.Popup({
                    title: "Feature Info",
                    width: 200,
                    height: 300,
                    layout: "accordion",
                    autoscroll: true,
                    map: viewPort.mapPanel.map,
                    location: viewPort.mapPanel.map.getLonLatFromPixel(e.xy),
                    items: items

Thanks to this post!

Your Answer

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

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