Skip to content

Commit 01cd285

Browse files
committed
Merge pull request #283 from BibMartin/issue259
RegularPolygonMarker and CircleMarker are instances of Marker
2 parents 1366528 + 891e00f commit 01cd285

3 files changed

Lines changed: 46 additions & 24 deletions

File tree

folium/features.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import json
1010

1111
from .utilities import (color_brewer, _parse_size, legend_scaler,
12-
_locations_mirror, _locations_tolist, image_to_url)
12+
_locations_mirror, _locations_tolist, image_to_url,
13+
text_type, binary_type)
1314

1415
from .element import Element, Figure, JavascriptLink, CssLink, MacroElement
15-
from .map import TileLayer, Icon
16+
from .map import TileLayer, Icon, Marker, Popup
1617

1718

1819
class WmsTileLayer(TileLayer):
@@ -48,7 +49,7 @@ def __init__(self, url, name=None,
4849
""") # noqa
4950

5051

51-
class RegularPolygonMarker(MacroElement):
52+
class RegularPolygonMarker(Marker):
5253
def __init__(self, location, color='black', opacity=1, weight=2,
5354
fill_color='blue', fill_opacity=1,
5455
number_of_sides=4, rotation=0, radius=15, popup=None):
@@ -85,9 +86,8 @@ def __init__(self, location, color='black', opacity=1, weight=2,
8586
8687
For more information, see https://humangeo.github.io/leaflet-dvf/
8788
"""
88-
super(RegularPolygonMarker, self).__init__()
89+
super(RegularPolygonMarker, self).__init__(location, popup=popup)
8990
self._name = 'RegularPolygonMarker'
90-
self.location = location
9191
self.color = color
9292
self.opacity = opacity
9393
self.weight = weight
@@ -96,8 +96,6 @@ def __init__(self, location, color='black', opacity=1, weight=2,
9696
self.number_of_sides = number_of_sides
9797
self.rotation = rotation
9898
self.radius = radius
99-
if popup is not None:
100-
self.add_children(popup)
10199

102100
self._template = Template(u"""
103101
{% macro script(this, kwargs) %}
@@ -420,22 +418,19 @@ def __init__(self, html=None, icon_size=None, icon_anchor=None,
420418
""") # noqa
421419

422420

423-
class CircleMarker(MacroElement):
421+
class CircleMarker(Marker):
424422
def __init__(self, location, radius=500, color='black',
425423
fill_color='black', fill_opacity=0.6, popup=None):
426424
"""
427425
TODO docstring here
428426
429427
"""
430-
super(CircleMarker, self).__init__()
428+
super(CircleMarker, self).__init__(location, popup=popup)
431429
self._name = 'CircleMarker'
432-
self.location = location
433430
self.radius = radius
434431
self.color = color
435432
self.fill_color = fill_color
436433
self.fill_opacity = fill_opacity
437-
if popup is not None:
438-
self.add_children(popup)
439434

440435
self._template = Template(u"""
441436
{% macro script(this, kwargs) %}
@@ -508,7 +503,7 @@ def __init__(self, popup=None):
508503

509504
class PolyLine(MacroElement):
510505
def __init__(self, locations, color=None, weight=None,
511-
opacity=None, latlon=True):
506+
opacity=None, latlon=True, popup=None):
512507
"""
513508
Creates a PolyLine object to append into a map with
514509
Map.add_children.
@@ -525,6 +520,8 @@ def __init__(self, locations, color=None, weight=None,
525520
or not ([[lon, lat]] if False).
526521
Note that the default GeoJson format is latlon=False,
527522
while Leaflet polyline's default is latlon=True.
523+
popup: string or folium.Popup, default None
524+
Input text or visualization for object.
528525
"""
529526
super(PolyLine, self).__init__()
530527
self._name = 'PolyLine'
@@ -533,6 +530,10 @@ def __init__(self, locations, color=None, weight=None,
533530
self.color = color
534531
self.weight = weight
535532
self.opacity = opacity
533+
if isinstance(popup, text_type) or isinstance(popup, binary_type):
534+
self.add_children(Popup(popup))
535+
elif popup is not None:
536+
self.add_children(popup)
536537

537538
self._template = Template(u"""
538539
{% macro script(this, kwargs) %}
@@ -550,7 +551,7 @@ def __init__(self, locations, color=None, weight=None,
550551

551552
class MultiPolyLine(MacroElement):
552553
def __init__(self, locations, color=None, weight=None,
553-
opacity=None, latlon=True):
554+
opacity=None, latlon=True, popup=None):
554555
"""
555556
Creates a MultiPolyLine object to append into a map with
556557
Map.add_children.
@@ -567,6 +568,8 @@ def __init__(self, locations, color=None, weight=None,
567568
or not ([[lon, lat]] if False).
568569
Note that the default GeoJson format is latlon=False,
569570
while Leaflet polyline's default is latlon=True.
571+
popup: string or folium.Popup, default None
572+
Input text or visualization for object.
570573
"""
571574
super(MultiPolyLine, self).__init__()
572575
self._name = 'MultiPolyLine'
@@ -575,6 +578,10 @@ def __init__(self, locations, color=None, weight=None,
575578
self.color = color
576579
self.weight = weight
577580
self.opacity = opacity
581+
if isinstance(popup, text_type) or isinstance(popup, binary_type):
582+
self.add_children(Popup(popup))
583+
elif popup is not None:
584+
self.add_children(popup)
578585

579586
self._template = Template(u"""
580587
{% macro script(this, kwargs) %}

folium/map.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,8 @@ def __init__(self, location, popup=None, icon=None):
363363
----------
364364
location: tuple or list, default None
365365
Latitude and Longitude of Marker (Northing, Easting)
366-
popup: string or tuple, default 'Pop Text'
367-
Input text or visualization for object. Can pass either text,
368-
or a tuple of the form (Vincent object, 'vis_path.json')
369-
It is possible to adjust the width of text/HTML popups
370-
using the optional keywords `popup_width` (default is 300px).
366+
popup: string or folium.Popup, default None
367+
Input text or visualization for object.
371368
icon: Icon plugin
372369
the Icon plugin to use to render the marker.
373370
@@ -377,16 +374,17 @@ def __init__(self, location, popup=None, icon=None):
377374
378375
Example
379376
-------
380-
>>>map.simple_marker(location=[45.5, -122.3], popup='Portland, OR')
381-
>>>map.simple_marker(location=[45.5, -122.3], popup=(vis, 'vis.json'))
382-
377+
>>> Marker(location=[45.5, -122.3], popup='Portland, OR')
378+
>>> Marker(location=[45.5, -122.3], popup=folium.Popup('Portland, OR'))
383379
"""
384380
super(Marker, self).__init__()
385381
self._name = 'Marker'
386382
self.location = location
387383
if icon is not None:
388384
self.add_children(icon)
389-
if popup is not None:
385+
if isinstance(popup, text_type) or isinstance(popup, binary_type):
386+
self.add_children(Popup(popup))
387+
elif popup is not None:
390388
self.add_children(popup)
391389

392390
self._template = Template(u"""

tests/test_features.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
import os
9-
from folium import Map
9+
from folium import Map, Popup
1010
from folium import features
1111
from folium.six import text_type
1212
from folium.element import Element
@@ -79,6 +79,23 @@ def test_figure_double_rendering():
7979
out2 = f.render()
8080
assert out == out2
8181

82+
def test_marker_popups():
83+
m = Map()
84+
features.Marker([45,-180],popup='-180').add_to(m)
85+
features.Marker([45,-120],popup=Popup('-120')).add_to(m)
86+
features.RegularPolygonMarker([45,-60],popup='-60').add_to(m)
87+
features.RegularPolygonMarker([45,0],popup=Popup('0')).add_to(m)
88+
features.CircleMarker([45,60],popup='60').add_to(m)
89+
features.CircleMarker([45,120],popup=Popup('120')).add_to(m)
90+
m._repr_html_()
91+
92+
def test_polyline_popups():
93+
m = Map([43,-100], zoom_start=4)
94+
features.PolyLine([[40,-80],[45,-80]], popup="PolyLine").add_to(m)
95+
features.PolyLine([[40,-90],[45,-90]], popup=Popup("PolyLine")).add_to(m)
96+
features.MultiPolyLine([[[40,-110],[45,-110]]], popup="MultiPolyLine").add_to(m)
97+
features.MultiPolyLine([[[40,-120],[45,-120]]], popup=Popup("MultiPolyLine")).add_to(m)
98+
m._repr_html_()
8299

83100
# DivIcon.
84101
def test_divicon():

0 commit comments

Comments
 (0)