0

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?

  • CQL supports the IN clause. You might alternatively be able to create the functionality you want using OpenLayers.Filter.Function. – drunkenwagoner Apr 21 '15 at 2:23
  • @drunkenwagoner, thanks for your answer... I never used OpenLayers.Filter.Function. how would you suggest doing this? thanks – joaorodr84 Apr 22 '15 at 20:49
2

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!

  • hi @meehocz, thanks for your answer. Indeed, it might be the best solution, but I would prefer avoiding the overriding of methods from the OpenLayers core... do you think it would be possible with OpenLayers.Filter.Function? I never used this lass, so I don't know its potential – joaorodr84 Apr 22 '15 at 20:51
  • 1
    Hi @joaorodr84, if you don't want to override OL core, ThomasG77's answer is a nice way to go. However, if you override (or better said extend) the library carefully (I mean think about all possible consequences), there's no problem with that and you can use it in future. I have a separate file just for these OL extensions, so the original library is not touched. – meehocz Apr 24 '15 at 7:22
1

I really didn't look in the OpenLayers 2 API but you just need to know JavaScript to make it better. Just use array manipulation functions at Mozilla Developer Network documentation (MDN)

var towns = ['Paris', 'London', 'Lisbon'];
var inFilters = towns.map(function(el) {
  return new OpenLayers.Filter.Comparison({
    property: 'city',
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    value: el
  })
});

var filter = new OpenLayers.Filter.Logical({
    filters: inFilters,
    type: OpenLayers.Filter.Logical.OR
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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