|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +from jinja2 import Template |
| 4 | + |
| 5 | +from folium.elements import JSCSSMixin |
| 6 | +from folium.features import MacroElement |
| 7 | +from folium.vector_layers import path_options |
| 8 | + |
| 9 | + |
| 10 | +class _BaseFromEncoded(JSCSSMixin, MacroElement, ABC): |
| 11 | + """Base Interface to create folium objects from encoded strings. |
| 12 | +
|
| 13 | + Derived classes must define `_encoding_type` property which returns the string |
| 14 | + representation of the folium object to create from the encoded string. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + encoded: str |
| 19 | + The raw encoded string from the Polyline Encoding Algorithm. See: |
| 20 | + https://developers.google.com/maps/documentation/utilities/polylinealgorithm |
| 21 | + **kwargs: |
| 22 | + Object options as accepted by leaflet. |
| 23 | + """ |
| 24 | + |
| 25 | + _template = Template( |
| 26 | + """ |
| 27 | + {% macro script(this, kwargs) %} |
| 28 | +
|
| 29 | + var {{ this.get_name() }} = L.{{ this._encoding_type }}.fromEncoded( |
| 30 | + {{ this.encoded|tojson }}, |
| 31 | + {{ this.options|tojson }} |
| 32 | + ).addTo({{ this._parent.get_name() }}); |
| 33 | +
|
| 34 | + {% endmacro %} |
| 35 | + """ |
| 36 | + ) |
| 37 | + |
| 38 | + default_js = [ |
| 39 | + ( |
| 40 | + "polyline-encoded", |
| 41 | + "https://cdn.jsdelivr.net/npm/polyline-encoded@0.0.9/Polyline.encoded.js", |
| 42 | + ) |
| 43 | + ] |
| 44 | + |
| 45 | + def __init__(self, encoded: str): |
| 46 | + super().__init__() |
| 47 | + self.encoded = encoded |
| 48 | + |
| 49 | + @property |
| 50 | + @abstractmethod |
| 51 | + def _encoding_type(self) -> str: |
| 52 | + """An abstract getter to return the type of folium object to create.""" |
| 53 | + raise NotImplementedError |
| 54 | + |
| 55 | + |
| 56 | +class PolyLineFromEncoded(_BaseFromEncoded): |
| 57 | + """Create PolyLines directly from the encoded string. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + encoded: str |
| 62 | + The raw encoded string from the Polyline Encoding Algorithm. See: |
| 63 | + https://developers.google.com/maps/documentation/utilities/polylinealgorithm |
| 64 | + **kwargs: |
| 65 | + Polyline options as accepted by leaflet. See: |
| 66 | + https://leafletjs.com/reference.html#polyline |
| 67 | +
|
| 68 | + Adapted from https://github.com/jieter/Leaflet.encoded |
| 69 | +
|
| 70 | + Examples |
| 71 | + -------- |
| 72 | + >>> from folium import Map |
| 73 | + >>> from folium.plugins import PolyLineFromEncoded |
| 74 | + >>> m = Map() |
| 75 | + >>> encoded = r"_p~iF~cn~U_ulLn{vA_mqNvxq`@" |
| 76 | + >>> PolyLineFromEncoded(encoded=encoded, color="green").add_to(m) |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, encoded: str, **kwargs): |
| 80 | + self._name = "PolyLineFromEncoded" |
| 81 | + super().__init__(encoded=encoded) |
| 82 | + self.options = path_options(line=True, **kwargs) |
| 83 | + |
| 84 | + @property |
| 85 | + def _encoding_type(self) -> str: |
| 86 | + """Return the name of folium object created from the encoded.""" |
| 87 | + return "Polyline" |
| 88 | + |
| 89 | + |
| 90 | +class PolygonFromEncoded(_BaseFromEncoded): |
| 91 | + """Create Polygons directly from the encoded string. |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + encoded: str |
| 96 | + The raw encoded string from the Polyline Encoding Algorithm. See: |
| 97 | + https://developers.google.com/maps/documentation/utilities/polylinealgorithm |
| 98 | + **kwargs: |
| 99 | + Polygon options as accepted by leaflet. See: |
| 100 | + https://leafletjs.com/reference.html#polygon |
| 101 | +
|
| 102 | + Adapted from https://github.com/jieter/Leaflet.encoded |
| 103 | +
|
| 104 | + Examples |
| 105 | + -------- |
| 106 | + >>> from folium import Map |
| 107 | + >>> from folium.plugins import PolygonFromEncoded |
| 108 | + >>> m = Map() |
| 109 | + >>> encoded = r"w`j~FpxivO}jz@qnnCd}~Bsa{@~f`C`lkH" |
| 110 | + >>> PolygonFromEncoded(encoded=encoded).add_to(m) |
| 111 | + """ |
| 112 | + |
| 113 | + def __init__(self, encoded: str, **kwargs): |
| 114 | + self._name = "PolygonFromEncoded" |
| 115 | + super().__init__(encoded) |
| 116 | + self.options = path_options(line=True, radius=None, **kwargs) |
| 117 | + |
| 118 | + @property |
| 119 | + def _encoding_type(self) -> str: |
| 120 | + """Return the name of folium object created from the encoded.""" |
| 121 | + return "Polygon" |
0 commit comments