Leaflet: Add recursive to L.LayerGroup.eachLayer()

Created on 19 Apr 2016  Â·  9Comments  Â·  Source: Leaflet/Leaflet

It might be useful to add an new option to _L.LayerGroup.eachLayer()_ method for recursive behavior?
This could greatly improve management and research possibilities on complex data.

for example:

....geojson loading...

var geo1 = L.geoJson(Geo1);
var geo2 = L.geoJson(Geo2);
var geo3 = L.geoJson(Geo3);
var geo4 = L.geoJson(Geo4);

var group1 = L.LayerGroup([geo1, geo2]);
var group2 = L.LayerGroup([geo3, geo4]);
var groupAll = L.LayerGroup([group1, group2]);

groupAll.eachLayer(function(lay, layParent) {
   console.log(lay, layParent);
}, {
 recursive: true
//new option
})
feature needs review

Most helpful comment

I propose something like:

L.LayerGroup.include({
    eachLayerRecursive: function(method, context) {
        this.eachLayer(function(layer) {
            if (layer._layers)
                layer.eachLayerRecursive(method, context);
            else
                method.call(context, layer);
        });
    }
});

Run method on all LayerGroup tree leaves but not on branches.

All 9 comments

Sounds like a cool little feature, but I'd rather have a new function call for recursively traversing layer groups (maybe something like getSingleLayers() and eachSingleLayer()). It should be straightforward to implement in very few lines of code, so I'll encourage any newbies to submit a pull request. cc @yourfirstpr

I can implement this in a new pull req but before I wanted to be sure it was a useful thing for more users

_getSubLayers()_ or _getAllLayers()_ ?

How about eachLayerRecursive? It's not a get-method, since it will use a callback to visit each layer, so I'd keep that out of the name. Also, since it's more or less the same method as eachLayer, it would be nice if the name makes that clear.

+1

Hi,

It seems to me that getSingleLayers() / eachSingleLayer() convey a different meaning from eachLayerRecursive():

  • getSingleLayers() / eachSingleLayer() sound like acting only on non-group layers, which is interesting in probably most situations (see Leaflet/Leaflet.markercluster#624 for getting non-group layers, stefanocudini/leaflet-search#101 for applying a method on them). However, it might be ambiguous whether they just skip group layers or if they continue recursively on children of group layers? => getSingleLayersRecursive() and eachSingleLayerRecursive()?
  • eachLayerRecursive() sounds like doing the same as eachLayer() at the beginning (i.e. applying the method to _child group_ layers as well), but continuing to children of those group layers. Hence it sounds like it does more than eachSingleLayerRecursive().
  • Similarly, getAllLayers() sounds like it returns all group and non-group layers. => getLayersRecursive()?

| Result | Get | Apply |
| :-: | :-: | :-: |
| Direct children only (current methods) | getLayers() | eachLayer() |
| Direct non-group only | getSingleLayers() | eachSingleLayer() |
| Recursively non-group only | getSingleLayersRecursive() | eachSingleLayerRecursive() |
| Recursively group and non-group | getLayersRecursive() | eachLayerRecursive() |

I propose something like:

L.LayerGroup.include({
    eachLayerRecursive: function(method, context) {
        this.eachLayer(function(layer) {
            if (layer._layers)
                layer.eachLayerRecursive(method, context);
            else
                method.call(context, layer);
        });
    }
});

Run method on all LayerGroup tree leaves but not on branches.

HI guys, I've been following this issue for a while and I realized that once we add recursion to eachLayer, we should also add recursion or a method with recursion to removeLayer (both on Map and LayerGroup), to hasLayer and to getLayer.

Doesn't make sense be able to execute one function recursively on all layers and "sub-layers" and not be able to manipulate those "sub-layers".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brambow picture brambow  Â·  3Comments

piehei picture piehei  Â·  3Comments

broofa picture broofa  Â·  4Comments

pgeyman picture pgeyman  Â·  3Comments

onethread picture onethread  Â·  3Comments