|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +from branca.element import MacroElement |
| 4 | + |
| 5 | +from folium.elements import JSCSSMixin |
| 6 | +from folium.template import Template |
| 7 | +from folium.utilities import parse_options |
| 8 | + |
| 9 | + |
| 10 | +class TreeLayerControl(JSCSSMixin, MacroElement): |
| 11 | + """ |
| 12 | + Create a Layer Control allowing a tree structure for the layers. |
| 13 | + See https://github.com/jjimenezshaw/Leaflet.Control.Layers.Tree for more |
| 14 | + information. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + base_tree : dict |
| 19 | + A dictionary defining the base layers. |
| 20 | + Valid elements are |
| 21 | +
|
| 22 | + children: list |
| 23 | + Array of child nodes for this node. Nothing special. |
| 24 | + label: str |
| 25 | + Text displayed in the tree for this node. It may contain HTML code. |
| 26 | + layer: Layer |
| 27 | + The layer itself. This needs to be added to the map. |
| 28 | + name: str |
| 29 | + Text displayed in the toggle when control is minimized. |
| 30 | + If not present, label is used. It makes sense only when |
| 31 | + namedToggle is true, and with base layers. |
| 32 | + radioGroup: str, default '' |
| 33 | + Text to identify different radio button groups. |
| 34 | + It is used in the name attribute in the radio button. |
| 35 | + It is used only in the overlays layers (ignored in the base |
| 36 | + layers), allowing you to have radio buttons instead of checkboxes. |
| 37 | + See that radio groups cannot be unselected, so create a 'fake' |
| 38 | + layer (like L.layersGroup([])) if you want to disable it. |
| 39 | + Default '' (that means checkbox). |
| 40 | + collapsed: bool, default False |
| 41 | + Indicate whether this tree node should be collapsed initially, |
| 42 | + useful for opening large trees partially based on user input or |
| 43 | + context. |
| 44 | + selectAllCheckbox: bool or str |
| 45 | + Displays a checkbox to select/unselect all overlays in the |
| 46 | + sub-tree. In case of being a <str>, that text will be the title |
| 47 | + (tooltip). When any overlay in the sub-tree is clicked, the |
| 48 | + checkbox goes into indeterminate state (a dash in the box). |
| 49 | + overlay_tree: dict |
| 50 | + Similar to baseTree, but for overlays. |
| 51 | + closed_symbol: str, default '+', |
| 52 | + Symbol displayed on a closed node (that you can click to open). |
| 53 | + opened_symbol: str, default '-', |
| 54 | + Symbol displayed on an opened node (that you can click to close). |
| 55 | + space_symbol: str, default ' ', |
| 56 | + Symbol between the closed or opened symbol, and the text. |
| 57 | + selector_back: bool, default False, |
| 58 | + Flag to indicate if the selector (+ or −) is after the text. |
| 59 | + named_toggle: bool, default False, |
| 60 | + Flag to replace the toggle image (box with the layers image) with the |
| 61 | + 'name' of the selected base layer. If the name field is not present in |
| 62 | + the tree for this layer, label is used. See that you can show a |
| 63 | + different name when control is collapsed than the one that appears |
| 64 | + in the tree when it is expanded. |
| 65 | + collapse_all: str, default '', |
| 66 | + Text for an entry in control that collapses the tree (baselayers or |
| 67 | + overlays). If empty, no entry is created. |
| 68 | + expand_all: str, default '', |
| 69 | + Text for an entry in control that expands the tree. If empty, no entry |
| 70 | + is created |
| 71 | + label_is_selector: str, default 'both', |
| 72 | + Controls if a label or only the checkbox/radiobutton can toggle layers. |
| 73 | + If set to `both`, `overlay` or `base` those labels can be clicked |
| 74 | + on to toggle the layer. |
| 75 | + **kwargs |
| 76 | + Additional (possibly inherited) options. See |
| 77 | + https://leafletjs.com/reference.html#control-layers |
| 78 | +
|
| 79 | + Examples |
| 80 | + -------- |
| 81 | + >>> import folium |
| 82 | + >>> from folium.plugins.treelayercontrol import TreeLayerControl |
| 83 | + >>> from folium.features import Marker |
| 84 | +
|
| 85 | + >>> m = folium.Map(location=[46.603354, 1.8883335], zoom_start=5) |
| 86 | + >>> osm = folium.TileLayer("openstreetmap").add_to(m) |
| 87 | +
|
| 88 | + >>> marker = Marker([48.8582441, 2.2944775]).add_to(m) |
| 89 | +
|
| 90 | + >>> overlay_tree = { |
| 91 | + ... "label": "Points of Interest", |
| 92 | + ... "selectAllCheckbox": "Un/select all", |
| 93 | + ... "children": [ |
| 94 | + ... { |
| 95 | + ... "label": "Europe", |
| 96 | + ... "selectAllCheckbox": True, |
| 97 | + ... "children": [ |
| 98 | + ... { |
| 99 | + ... "label": "France", |
| 100 | + ... "selectAllCheckbox": True, |
| 101 | + ... "children": [ |
| 102 | + ... {"label": "Tour Eiffel", "layer": marker}, |
| 103 | + ... ], |
| 104 | + ... } |
| 105 | + ... ], |
| 106 | + ... } |
| 107 | + ... ], |
| 108 | + ... } |
| 109 | +
|
| 110 | + >>> control = TreeLayerControl(overlay_tree=overlay_tree).add_to(m) |
| 111 | + """ |
| 112 | + |
| 113 | + default_js = [ |
| 114 | + ( |
| 115 | + "L.Control.Layers.Tree.min.js", |
| 116 | + "https://cdn.jsdelivr.net/npm/leaflet.control.layers.tree@1.1.0/L.Control.Layers.Tree.min.js", # noqa |
| 117 | + ), |
| 118 | + ] |
| 119 | + default_css = [ |
| 120 | + ( |
| 121 | + "L.Control.Layers.Tree.min.css", |
| 122 | + "https://cdn.jsdelivr.net/npm/leaflet.control.layers.tree@1.1.0/L.Control.Layers.Tree.min.css", # noqa |
| 123 | + ) |
| 124 | + ] |
| 125 | + |
| 126 | + _template = Template( |
| 127 | + """ |
| 128 | + {% macro script(this,kwargs) %} |
| 129 | + L.control.layers.tree( |
| 130 | + {{this.base_tree|tojavascript}}, |
| 131 | + {{this.overlay_tree|tojavascript}}, |
| 132 | + {{this.options|tojson}} |
| 133 | + ).addTo({{this._parent.get_name()}}); |
| 134 | + {% endmacro %} |
| 135 | + """ |
| 136 | + ) |
| 137 | + |
| 138 | + def __init__( |
| 139 | + self, |
| 140 | + base_tree: Union[dict, list, None] = None, |
| 141 | + overlay_tree: Union[dict, list, None] = None, |
| 142 | + closed_symbol: str = "+", |
| 143 | + opened_symbol: str = "-", |
| 144 | + space_symbol: str = " ", |
| 145 | + selector_back: bool = False, |
| 146 | + named_toggle: bool = False, |
| 147 | + collapse_all: str = "", |
| 148 | + expand_all: str = "", |
| 149 | + label_is_selector: str = "both", |
| 150 | + **kwargs |
| 151 | + ): |
| 152 | + super().__init__() |
| 153 | + self._name = "TreeLayerControl" |
| 154 | + kwargs["closed_symbol"] = closed_symbol |
| 155 | + kwargs["openened_symbol"] = opened_symbol |
| 156 | + kwargs["space_symbol"] = space_symbol |
| 157 | + kwargs["selector_back"] = selector_back |
| 158 | + kwargs["named_toggle"] = named_toggle |
| 159 | + kwargs["collapse_all"] = collapse_all |
| 160 | + kwargs["expand_all"] = expand_all |
| 161 | + kwargs["label_is_selector"] = label_is_selector |
| 162 | + self.options = parse_options(**kwargs) |
| 163 | + self.base_tree = base_tree |
| 164 | + self.overlay_tree = overlay_tree |
0 commit comments