How to get a layer from a feature in Openlayers 3?
Asked Answered
S

3

14

I can't find a way to go from a feature in a selection event to a layer that it may be a part of without traversing all the features of all my map layers, or storing an artificial layer ID within every feature at creation. Is this just not possible yet?

ol.js 3.7.0 ol.interaction.Selection -> click -> callback( event ){ event.selected[0] }

In another part of my app, I would like to go from the feature to the layer to determine the style being used on the feature, specifically whether or not it's visible.

ol.Feature.getStyle() || ol.Feature -> (layer?) -> getStyle()

Suburbicarian answered 8/7, 2015 at 15:52 Comment(1)
Jonatas's answer for the selection, to use the filter, works with the additional caveat that I have to store all of the items that pass through 'filter' function in a local array, and in the select event handler (callback), match the selected feature with one of these array items and clear the array.Suburbicarian
B
12

You could try with the filter function:

var select = new ol.interaction.Select({
    condition:  ...,
    filter: function(feature, layer){
        console.info(feature);
        console.info(layer.get('name'));
    }
});

UPDATE

I came up with this prototypied method, it does the job:

http://jsfiddle.net/jonataswalker/r242y7ke/

/**
 * This is a workaround.
 * Returns the associated layer.
 * @param {ol.Map} map.
 * @return {ol.layer.Vector} Layer.
 */
ol.Feature.prototype.getLayer = function(map) {
    var this_ = this, layer_, layersToLookFor = [];
    /**
     * Populates array layersToLookFor with only
     * layers that have features
     */
    var check = function(layer){
        var source = layer.getSource();
        if(source instanceof ol.source.Vector){
            var features = source.getFeatures();
            if(features.length > 0){
                layersToLookFor.push({
                    layer: layer,
                    features: features
                });
            }
        }
    };
    //loop through map layers
    map.getLayers().forEach(function(layer){
        if (layer instanceof ol.layer.Group) {
            layer.getLayers().forEach(check);
        } else {
            check(layer);
        }
    });
    layersToLookFor.forEach(function(obj){
        var found = obj.features.some(function(feature){
            return this_ === feature;
        });
        if(found){
            //this is the layer we want
            layer_ = obj.layer;
        }
    });
    return layer_;
};

select.on('select', function(evt){
    var feature = evt.selected[0];
    if(feature){
        var layer = feature.getLayer(map);

        console.info(layer.getStyle());
        console.info(layer.get('name'));
    }
});
Bracteate answered 8/7, 2015 at 17:34 Comment(5)
I saw that, however in another part of my app I would've liked to go from the feature to the layer .. i'll add it to the question. But I'll try this suggestion, if it works.. good enough for now :)Suburbicarian
How about store a reference for later usage?Bracteate
As for storing external references, that's one way to do it, but I'm interested in this question to know if there's a way to go from a feature to a layer within the frameworkSuburbicarian
I'd also like to use the this.selection.on( 'select', someCallBack ); to handle selection events, not the filter call, but whateverSuburbicarian
I'm doing some tests, but it's not easy without some hackish.Bracteate
O
10

In OL 5.3.0, the Select interaction object has the getLayer() function to get the associated layer of the last selected feature. Example:

let selectClick = new Select({});
map.addInteraction(selectClick);

selectClick.on('select', function(e) {
    let featureSelected = e.selected[0];
    let layer = selectClick.getLayer(featureSelected);
    console.log(layer); // here you have the selected layer
});
Oralee answered 12/7, 2019 at 15:13 Comment(0)
I
2

In Openlayers 4 - map.forEachFeatureAtPixel can be used to get at the parent layer of each feature.

See code snippet here: https://mcmap.net/q/829130/-openlayers-3-get-layer-for-selected-feature

Inheritor answered 18/5, 2018 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.