[GIS] Using IN filters in OpenLayers

comparisonfilteropenlayers-2

Is there a way to use IN filters (like the IN operator from SQL) in OpenLayers 2?

For example, let's say, I want a filter like this:

city = 'Paris' OR city = 'London' OR city = 'Lisbon'

In OpenLayers, I would have to combine logical and comparison filters and it would be a big filter for only 3 cities. It would be something like this:

var filter = new OpenLayers.Filter.Logical({
    filters: [
        new OpenLayers.Filter.Comparison({
            property: 'city',
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            value: 'Paris'
        }),
        new OpenLayers.Filter.Comparison({
            property: 'city',
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            value: 'London'
        }),
        new OpenLayers.Filter.Comparison({
            property: 'city',
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            value: 'Lisbon'
        })
    ],
    type: OpenLayers.Filter.Logical.OR
});

I was wondering if there is a way to do it like this:

var filter = new OpenLayers.Filter.Comparison({
    property: 'city',
    type: OpenLayers.Filter.Comparison.IN,
    values: ['Paris', 'London', 'Lisbon']
});

I know that it does not work with OpenLayers, but how can I make it work?

Best Answer

You have to override the OpenLayers.Filter.Comparison.prototype.evaluate function in a way, that it will test whether this.value is an array and if yes, test whether got contains exp (for the IN case). To make it more clean, you should add your own OpenLayers.Filter.Comparison.IN comparator to the enumerable.

See the original code to override in OpenLayers install package under lib\OpenLayers\Filter\Comparison.js:112, it's quite pretty!

Related Question