|
| 1 | +from folium.elements import JSCSSMixin |
| 2 | +from folium.map import Layer |
| 3 | + |
| 4 | +from jinja2 import Template |
| 5 | + |
| 6 | + |
| 7 | +class SideBySideLayers(JSCSSMixin, Layer): |
| 8 | + """ |
| 9 | + Creates a SideBySideLayers that takes two Layers and adds a sliding |
| 10 | + control with the leaflet-side-by-side plugin. |
| 11 | +
|
| 12 | + Uses the Leaflet leaflet-side-by-side plugin https://github.com/digidem/leaflet-side-by-side |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + layer_left: Layer. |
| 17 | + The left Layer within the side by side control. |
| 18 | + Must be created and added to the map before being passed to this class. |
| 19 | + layer_right: Layer. |
| 20 | + The left Layer within the side by side control. |
| 21 | + Must be created and added to the map before being passed to this class. |
| 22 | +
|
| 23 | + Examples |
| 24 | + -------- |
| 25 | + >>> sidebyside = SideBySideLayers(layer_left, layer_right) |
| 26 | + >>> sidebyside.add_to(m) |
| 27 | + """ |
| 28 | + |
| 29 | + _template = Template(u""" |
| 30 | + {% macro script(this, kwargs) %} |
| 31 | + var {{ this.get_name() }} = L.control.sideBySide( |
| 32 | + {{ this.layer_left.get_name() }}, {{ this.layer_right.get_name() }} |
| 33 | + ).addTo({{ this._parent.get_name() }}); |
| 34 | + {% endmacro %} |
| 35 | + """) |
| 36 | + |
| 37 | + default_js = [ |
| 38 | + ('leaflet.sidebyside', |
| 39 | + 'https://cdn.jsdelivr.net/gh/digidem/leaflet-side-by-side@gh-pages/leaflet-side-by-side.min.js'), |
| 40 | + ] |
| 41 | + |
| 42 | + def __init__(self, layer_left, layer_right): |
| 43 | + super().__init__(control=False) |
| 44 | + self._name = 'SideBySideLayers' |
| 45 | + self.layer_left = layer_left |
| 46 | + self.layer_right = layer_right |
0 commit comments