on_layer_activation

Maps.on_layer_activation(func, layer=None, persistent=False, **kwargs)

Attach a callback that is executed if the associated layer is activated.

Useful to β€œlazily” populate layers with features that are expensive to create (e.g. fetching data from files etc.).

Parameters:
  • func (callable) –

    The callable to use. The call-signature is:

    >>> def func(m, **kwargs):
    >>>    # m... the Maps-object used for calling this function
    

    NOTE: The Maps-object that is passed to the function is determined by the β€˜layer’ argument!

  • layer (str or None, optional) –

    If provided, a NEW layer will be created and passed to the execution of the function. Otherwise, the calling Maps-object is used.

    To clarify: The following two code-snippets are equivalent:

    >>> m = Maps()
    >>> m2 = m.new_layer("my_layer")
    >>> m2.on_layer_activation(func)
    
    >>> m = Maps()
    >>> m.on_layer_activation(func, layer="my_layer")
    

  • persistent (bool, optional) – Indicator if the function should be called only once (False) or if it should be called each time the layer is activated (True). The default is False.

  • kwargs – Additional keyword-arguments passed to the call of the function.

See also

Maps.layer

The layer-name associated with the Maps-object

Maps.fetch_layers

Fetch and cache all layers of the map

Examples

>>> m = Maps()
>>> m.add_feature.preset.coastline()
>>>
>>> def f(m, ocean_color, coastline_color):
>>>     print(f"EOmaps: creating features for the layer {m.layer}")
>>>     m.add_feature.preset.coastline(ec=coastline_color)
>>>     m.add_feature.preset.ocean(fc=ocean_color)
>>>
>>> # create a new (initially empty) layer "ocean"
>>> m2 = m.new_layer("ocean")
>>> # add features to the layer only if it is activated
>>> m2.on_layer_activation(f, ocean_color="b", coastline_color="r")
>>> s = m.util.layer_selector()