[GIS] Make only particular features on layer selectable

openlayers

I'm using a select interaction. I can filter the layers which are selectable using a filter function as the layers option key. I want to also filter which features are selectable.

The documentation says "Which features are selected is determined by the condition option". I am therefore trying to write a function for the condition option which can filter out particular features. But I'm not sure it's possible. I think the quoted documentation may be misleading.

The condition function takes an event argument. I first filter out any events which aren't of the click type, but then I want to filter on features. The event has a target property, but it's not clear to me what this is pointing to. It doesn't appear to be a layer or a feature.

I do have the pixel coordinates and the map coordinates at the clicked point. I could use either of these to loop over features at that point in the layer and, if I deem one of them selectable, return true. But what if there are two overlapping features, one of which I want to be selectable and one not?

I'm guessing this condition option is not what I'm looking for, since it is run once per event, not once per possible feature. Is there a way to achieve what I'm looking for?

Best Answer

The feature made it into Openlayers 3 master branch recently from https://github.com/openlayers/ol3/pull/3402 -- it'll be in 3.5.0. The following is working for me:

var selectInteraction = new ol.interaction.Select({
    layers: function(layer) {
        return /* some logic on layer to decide if its features should be considered; return true if yes */;
    },
    filter: function(feature, layer) {
        return /* some logic on a feature and layer to decide if it should be selectable; return true if yes */;
    },
});
Related Question