[GIS] OpenLayers: Combine multiple OGC filters

filterogcopenlayers-2

I have two OGC filters client side:

<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
    <ogc:PropertyIsEqualTo matchCase="true">
        <ogc:PropertyName>code</ogc:PropertyName>
        <ogc:Literal>secret</ogc:Literal>
    </ogc:PropertyIsEqualTo>
</ogc:Filter>   

<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
    <ogc:Intersects>
        <ogc:PropertyName>geom</ogc:PropertyName>
        <gml:Box xmlns:gml="http://www.opengis.net/gml">
            <gml:coordinates decimal="." cs="," ts=" ">1298543.0367787,7107400.531374 1299312.4070614,7203464.9016567</gml:coordinates>
        </gml:Box>
    </ogc:Intersects>
</ogc:Filter>

I have both filters represented as strings and I want to combine them into one filter. I can see in the documentation that OpenLayers can create a Filter Object from a string, but it doesn't seem to work for me.

Am I doing something wrong and/or how should I do it?

Best Answer

To read a filter from a string should look something like:

var my_filter_xml, parser, xml;

parser = new OpenLayers.Format.Filter.v1_1_0 ();
xml = new OpenLayers.Format.XML();
my_filter_xml = '<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsEqualTo matchCase="true"><ogc:PropertyName>code</ogc:PropertyName><ogc:Literal>secret</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter>'; //your filter string

var x = xml.read(my_filter_xml).documentElement
var filter1 = parser.read(x);
console.log(filter1);

This should return you an OpenLayers.Filter object. Do this for both your filters so you have two filter objects.

Finally create a new "parent" filter (which can be AND, OR, etc.) which combines the two:

    var parent_filter = new OpenLayers.Filter.Logical({
        type: OpenLayers.Filter.Logical.AND,
        filters: [filter1, filter2]
    });