Skip to content

Commit 80be994

Browse files
author
Martin Journois
committed
Fix test_line
1 parent 2431c51 commit 80be994

4 files changed

Lines changed: 105 additions & 57 deletions

File tree

folium/features.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,49 @@ def __init__(self, popup=None):
463463
{{this._parent.get_name()}}.on('click', newMarker);
464464
{% endmacro %}
465465
""")
466+
467+
class PolyLine(MacroElement):
468+
def __init__(self, locations, color=None, weight=None, opacity=None, latlon=True):
469+
"""Creates a PolyLine object to append into a map with Map.add_children.
470+
471+
Parameters
472+
----------
473+
locations: list of points (latitude, longitude)
474+
Latitude and Longitude of line (Northing, Easting)
475+
color: string, default Leaflet's default ('#03f')
476+
weight: float, default Leaflet's default (5)
477+
opacity: float, default Leaflet's default (0.5)
478+
latlon: bool, default True
479+
Whether locations are given in the form [[lat,lon]] or not ([[lon,lat]] if False).
480+
Note that the default GeoJson format is latlon=False,
481+
while Leaflet polyline's default is latlon=True.
482+
483+
examples :
484+
# providing file
485+
GeoJson(open('foo.json'))
486+
487+
# providing dict
488+
GeoJson(json.load(open('foo.json')))
489+
490+
# providing string
491+
GeoJson(open('foo.json').read())
492+
"""
493+
super(PolyLine, self).__init__()
494+
self._name = 'PolyLine'
495+
self.data = [[x[1],x[0]] for x in locations] if not latlon else locations
496+
self.color = color
497+
self.weight = weight
498+
self.opacity = opacity
499+
500+
self._template = Template(u"""
501+
{% macro script(this, kwargs) %}
502+
var {{this.get_name()}} = L.polyline(
503+
{{this.data}},
504+
{
505+
{% if this.color != None %}color: '{{ this.color }}',{% endif %}
506+
{% if this.weight != None %}weight: {{ this.weight }},{% endif %}
507+
{% if this.opacity != None %}opacity: {{ this.opacity }},{% endif %}
508+
});
509+
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
510+
{% endmacro %}
511+
""")

folium/folium.py

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .element import Element, Figure, JavascriptLink, CssLink, Div, MacroElement
2727
from .map import Map, TileLayer, Icon, Marker, Popup
2828
from .features import WmsTileLayer, RegularPolygonMarker, Vega, GeoJson, GeoJsonStyle, MarkerCluster, DivIcon,\
29-
CircleMarker, LatLngPopup, ClickForMarker, ColorScale, TopoJson
29+
CircleMarker, LatLngPopup, ClickForMarker, ColorScale, TopoJson, PolyLine
3030
from .utilities import color_brewer
3131
#import sys
3232
#import base64
@@ -378,55 +378,51 @@ def div_markers(self, locations=None, popups=None,
378378
popup = Popup(popup),
379379
icon = DivIcon(width=marker_size, height=marker_size))
380380
self.add_children(marker)
381-
#
382-
# @iter_obj('line')
383-
# def line(self, locations,
384-
# line_color=None, line_opacity=None, line_weight=None,
385-
# popup=None, popup_width=300):
386-
# """Add a line to the map with optional styles.
387-
#
388-
# Parameters
389-
# ----------
390-
# locations: list of points (latitude, longitude)
391-
# Latitude and Longitude of line (Northing, Easting)
392-
# line_color: string, default Leaflet's default ('#03f')
393-
# line_opacity: float, default Leaflet's default (0.5)
394-
# line_weight: float, default Leaflet's default (5)
395-
# popup: string or tuple, default 'Pop Text'
396-
# Input text or visualization for object. Can pass either text,
397-
# or a tuple of the form (Vincent object, 'vis_path.json')
398-
# It is possible to adjust the width of text/HTML popups
399-
# using the optional keywords `popup_width` (default is 300px).
400-
#
401-
# Note: If the optional styles are omitted, they will not be included
402-
# in the HTML output and will obtain the Leaflet defaults listed above.
403-
#
404-
# Example
405-
# -------
406-
# >>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)])
407-
# >>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)],
408-
# line_color='red', line_opacity=1.0)
409-
#
410-
# """
411-
# count = self.mark_cnt['line']
412-
#
413-
# line_temp = self.env.get_template('polyline.js')
414-
#
415-
# polyline_opts = {'color': line_color, 'weight': line_weight,
416-
# 'opacity': line_opacity}
417-
#
418-
# varname = 'line_{}'.format(count)
419-
# line_rendered = line_temp.render({'line': varname,
420-
# 'locations': locations,
421-
# 'options': polyline_opts})
422-
#
423-
# popup_out = self._popup_render(popup=popup, mk_name='line_',
424-
# count=count, width=popup_width)
425-
#
426-
# add_line = 'map.addLayer({});'.format(varname)
427-
# append = (line_rendered, popup_out, add_line)
428-
# self.template_vars.setdefault('lines', []).append((append))
429-
#
381+
382+
def line(self, locations,
383+
line_color=None, line_opacity=None, line_weight=None,
384+
popup=None, popup_width=300, latlon=True):
385+
"""Add a line to the map with optional styles.
386+
387+
Parameters
388+
----------
389+
locations: list of points (latitude, longitude)
390+
Latitude and Longitude of line (Northing, Easting)
391+
line_color: string, default Leaflet's default ('#03f')
392+
line_opacity: float, default Leaflet's default (0.5)
393+
line_weight: float, default Leaflet's default (5)
394+
popup: string or tuple, default 'Pop Text'
395+
Input text or visualization for object. Can pass either text,
396+
or a tuple of the form (Vincent object, 'vis_path.json')
397+
It is possible to adjust the width of text/HTML popups
398+
using the optional keywords `popup_width` (default is 300px).
399+
latlon: bool, default True
400+
Whether locations are given in the form [[lat,lon]] or not ([[lon,lat]] if False).
401+
Note that the default GeoJson format is latlon=False,
402+
while Leaflet polyline's default is latlon=True.
403+
404+
Note: If the optional styles are omitted, they will not be included
405+
in the HTML output and will obtain the Leaflet defaults listed above.
406+
407+
Example
408+
-------
409+
>>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)])
410+
>>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)],
411+
line_color='red', line_opacity=1.0)
412+
413+
"""
414+
415+
p = PolyLine(locations,
416+
color=line_color,
417+
weight=line_weight,
418+
opacity=line_opacity,
419+
latlon=latlon,
420+
)
421+
422+
if popup is not None:
423+
p.add_children(Popup(popup, max_width=popup_width))
424+
425+
self.add_children(p)
430426
# @iter_obj('multiline')
431427
# def multiline(self, locations, line_color=None, line_opacity=None,
432428
# line_weight=None):

folium/templates/polyline.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var latLngs = [{% for loc in locations %} [{{ loc[0] }}, {{ loc[1] }}], {% endfor %}];
2-
var {{ line }} = L.polyline(latLngs,{
1+
var {{ this.get_name() }} = L.polyline({{locations}},{
32
{% if options.color != None %}color: '{{ options.color }}',{% endif %}
43
{% if options.weight != None %}weight: {{ options.weight }},{% endif %}
54
{% if options.opacity != None %}opacity: {{ options.opacity }},{% endif %}

tests/test_folium.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from folium.element import Html
2121
from folium.map import Popup, Marker, Icon
2222
from folium.features import DivIcon, CircleMarker, LatLngPopup, GeoJson,\
23-
GeoJsonStyle, ColorScale, TopoJson
23+
GeoJsonStyle, ColorScale, TopoJson, PolyLine
2424

2525

2626
rootpath = os.path.abspath(os.path.dirname(__file__))
@@ -560,15 +560,22 @@ def test_line(self):
560560
[[45.5237, -122.6750], [45.5237, -122.6751]],
561561
[[45.5238, -122.6750], [45.5238, -122.6751]]
562562
]
563-
line_rendered = line_temp.render({'line': 'line_1',
564-
'locations': locations,
565-
'options': line_opts})
566563

564+
self.setup()
567565
self.map.line(locations=locations,
568566
line_color=line_opts['color'],
569567
line_weight=line_opts['weight'],
570568
line_opacity=line_opts['opacity'])
571-
assert self.map.template_vars['lines'][0][0] == line_rendered
569+
polyline = [val for key,val in self.map._children.items()\
570+
if isinstance(val,PolyLine)][0]
571+
out = self.map._parent.render()
572+
573+
line_rendered = line_temp.render({'line': 'line_1',
574+
'this':polyline,
575+
'locations': locations,
576+
'options': line_opts})
577+
578+
assert ''.join(line_rendered.split()) in ''.join(out.split())
572579

573580
def test_multi_polyline(self):
574581
"""Test multi_polyline."""

0 commit comments

Comments
 (0)