|
| 1 | +""" |
| 2 | +Measure plugin |
| 3 | +-------------- |
| 4 | +
|
| 5 | +Coordinate, linear, and area measure control for folium maps. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | + |
| 10 | +from branca.element import MacroElement, Figure, JavascriptLink, CssLink |
| 11 | + |
| 12 | +from jinja2 import Template |
| 13 | + |
| 14 | + |
| 15 | +class MeasureControl(MacroElement): |
| 16 | + """Adds a measurem widget on the map. |
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + position: location of the widget |
| 20 | + default is 'topright'. |
| 21 | +
|
| 22 | + primary_length_unit and secondary_length_unit: length units |
| 23 | + defaults are 'meters' and 'miles' respectively. |
| 24 | +
|
| 25 | + primary_area_unit and secondary_area_unit: ara units |
| 26 | + defaults are 'sqmeters' and 'acres' respectively. |
| 27 | +
|
| 28 | + For more information see https://github.com/ljagis/leaflet-measure |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, position='topright', primary_length_unit='meters', |
| 32 | + secondary_length_unit='miles', primary_area_unit='sqmeters', |
| 33 | + secondary_area_unit='acres'): |
| 34 | + """Coordinate, linear, and area measure control""" |
| 35 | + super(MeasureControl, self).__init__() |
| 36 | + self._name = 'MeasureControl' |
| 37 | + |
| 38 | + options = { |
| 39 | + 'position': position, |
| 40 | + 'primaryLengthUnit': primary_length_unit, |
| 41 | + 'secondaryLengthUnit': secondary_length_unit, |
| 42 | + 'primaryAreaUnit': primary_area_unit, |
| 43 | + 'secondaryAreaUnit': secondary_area_unit, |
| 44 | + } |
| 45 | + self.options = json.dumps(options) |
| 46 | + |
| 47 | + self._template = Template(""" |
| 48 | + {% macro script(this, kwargs) %} |
| 49 | + var {{this.get_name()}} = new L.Control.Measure( |
| 50 | + {{ this.options }}); |
| 51 | + {{this._parent.get_name()}}.addControl({{this.get_name()}}); |
| 52 | +
|
| 53 | + {% endmacro %} |
| 54 | + """) # noqa |
| 55 | + |
| 56 | + def render(self, **kwargs): |
| 57 | + super(MeasureControl, self).render() |
| 58 | + |
| 59 | + figure = self.get_root() |
| 60 | + assert isinstance(figure, Figure), ('You cannot render this Element ' |
| 61 | + 'if it is not in a Figure.') |
| 62 | + |
| 63 | + figure.header.add_child( |
| 64 | + JavascriptLink('https://cdn.rawgit.com/ljagis/leaflet-measure/2.1.7/dist/leaflet-measure.js')) # noqa |
| 65 | + |
| 66 | + figure.header.add_child( |
| 67 | + CssLink('https://cdn.rawgit.com/ljagis/leaflet-measure/2.1.7/dist/leaflet-measure.css')) # noqa |
0 commit comments