[GIS] WFS GetFeature control doesn’t take ‘viewparams’ into account

geoservergetfeatureopenlayers-2wfs

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.

http://screencast.com/t/RgrQvaoyoqz

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">
<ogc:BBOX>
<ogc:PropertyName>geometry</ogc:PropertyName>
<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>
</gml:Envelope>
</ogc:BBOX>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>

**edit **

This is the JavaScript code for the layer and controller

test = new OpenLayers.Layer.WMS("test",
    "http://test.boedy.p:8080/wms", 
    {
        '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
            }
        }
    );

Best Answer

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}),
Related Question