Skip to content

Add Rectangle Marker and Polygon. #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions folium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'Map',
'initialize_notebook',
'CircleMarker',
'RectangleMarker',
'Polygon',
'FeatureGroup',
'FitBounds',
'Icon',
Expand Down
104 changes: 104 additions & 0 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,110 @@ def __init__(self, location, radius=500, color='black',
""")


class RectangleMarker(Marker):
def __init__(self, bounds, color='black', weight=1, fill_color='black',
fill_opacity=0.6, popup=None):
"""Creates a RectangleMarker object for plotting on a Map.

Parameters
----------
bounds: tuple or list, default None
Latitude and Longitude of Marker (southWest and northEast)
color: string, default ('black')
Edge color of a rectangle.
weight: float, default (1)
Edge line width of a rectangle.
fill_color: string, default ('black')
Fill color of a rectangle.
fill_opacity: float, default (0.6)
Fill opacity of a rectangle.
popup: string or folium.Popup, default None
Input text or visualization for object.

Returns
-------
folium.features.RectangleMarker object

Example
-------
>>> RectangleMarker(bounds=[[35.681, 139.766], [35.691, 139.776]],
color="blue", fill_color="red", popup='Tokyo, Japan')
"""
super(RectangleMarker, self).__init__(bounds, popup=popup)
self._name = 'RectangleMarker'
self.color = color
self.weight = weight
self.fill_color = fill_color
self.fill_opacity = fill_opacity
self._template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.rectangle([[{{this.location[0]}}, {{this.location[1]}}],
[{{this.location[2]}}, {{this.location[3]}}]],
{
color:'{{ this.color }}',
fillColor:'{{ this.fill_color }}',
fillOpacity:{{ this.fill_opacity }},
weight:{{ this.weight }}
}).addTo({{this._parent.get_name()}});

{% endmacro %}
""")


class Polygon(Marker):
def __init__(self, locations, color='black', weight=1, fill_color='black',
fill_opacity=0.6, popup=None, latlon=True):
"""Creates a Polygon object for plotting on a Map.

Parameters
----------
locations: tuple or list, default None
Latitude and Longitude of Polygon
color: string, default ('black')
Edge color of a polygon.
weight: float, default (1)
Edge line width of a polygon.
fill_color: string, default ('black')
Fill color of a polygon.
fill_opacity: float, default (0.6)
Fill opacity of a polygon.
popup: string or folium.Popup, default None
Input text or visualization for object.

Returns
-------
folium.features.Polygon object

Example
-------
>>> loc= [[35.6762, 139.7795],
[35.6718, 139.7831],
[35.6767, 139.7868],
[35.6795, 139.7824],
[35.6787, 139.7791]]
Polygon(loc, color="blue", weight=10, fill_color="red",
fill_opacity=0.5, popup="Tokyo, Japan"))
"""
super(Polygon, self).__init__((_locations_mirror(locations) if not latlon else
_locations_tolist(locations)), popup=popup)
self._name = 'Polygon'
self.color = color
self.weight = weight
self.fill_color = fill_color
self.fill_opacity = fill_opacity
self._template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.polygon({{this.location}},
{
color: '{{ this.color }}',
fillColor: '{{ this.fill_color }}',
fillOpacity: {{ this.fill_opacity }},
weight: {{ this.weight }}
}).addTo({{this._parent.get_name()}});
{% endmacro %}
""")


class LatLngPopup(MacroElement):
"""
When one clicks on a Map that contains a LatLngPopup,
Expand Down
4 changes: 2 additions & 2 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from branca.six import text_type, binary_type

from .map import LegacyMap, Icon, Marker, Popup, FitBounds
from .features import (WmsTileLayer, RegularPolygonMarker, Vega, GeoJson,
CircleMarker, LatLngPopup,
from .features import (WmsTileLayer, RegularPolygonMarker, Vega,
GeoJson, CircleMarker, LatLngPopup,
ClickForMarker, TopoJson, PolyLine, MultiPolyLine,
)

Expand Down
7 changes: 7 additions & 0 deletions folium/templates/polygon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var {{ Polygon }} = L.polygon({{location}},
{
color:'{{ color }}',
fillColor:'{{ fill_color }}',
fillOpacity:{{ fill_opacity }},
weight:{{ weight }}
});
8 changes: 8 additions & 0 deletions folium/templates/rectangle_marker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var {{ RectangleMarker }} = L.rectangle([[{{location[0]}}, {{location[1]}}],
[{{location[2]}}, {{location[3]}}]],
{
color:'{{ color }}',
fillColor:'{{ fill_color }}',
fillOpacity:{{ fill_opacity }},
weight:{{ weight }}
});
89 changes: 88 additions & 1 deletion tests/test_folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import branca.element

from folium.map import Popup, Marker, FitBounds, FeatureGroup
from folium.features import GeoJson, TopoJson, PolyLine, MultiPolyLine
from folium.features import GeoJson, TopoJson, PolyLine, MultiPolyLine, RectangleMarker, Polygon
from folium.plugins import ImageOverlay

rootpath = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -238,6 +238,93 @@ def test_circle_marker(self):
bounds = self.map.get_bounds()
assert bounds == [[45.6, -122.9], [45.7, -122.8]], bounds

def test_rectangle_marker(self):
"""Test rectangle marker additions."""

self.map = folium.Map(location=[45.60, -122.8])
rect_templ = self.env.get_template('rectangle_marker.js')

# Single Rectangle marker.
bounds = [45.60, -122.8, 45.61, -122.7]
self.map.add_child(RectangleMarker(bounds=bounds, popup='Hi'))
marker = list(self.map._children.values())[-1]
rect_1 = rect_templ.render({'RectangleMarker': marker.get_name(),
'location': [45.60, -122.8, 45.61, -122.7],
'color': 'black',
'fill_color': 'black',
'fill_opacity': 0.6,
'weight': 1})
assert (''.join(rect_1.split())[:-1] in
''.join(self.map.get_root().render().split()))

# Second Rectangle marker.
bounds = [45.70, -122.9, 45.75, -122.5]
self.map.add_child(RectangleMarker(bounds=bounds, popup='Hi'))
marker = list(self.map._children.values())[-1]
rect_2 = rect_templ.render({'RectangleMarker': marker.get_name(),
'location': [45.70, -122.9, 45.75, -122.5],
'color': 'black',
'fill_color': 'black',
'fill_opacity': 0.6,
'weight': 1})
assert (''.join(rect_2.split())[:-1] in
''.join(self.map.get_root().render().split()))

bounds = self.map.get_bounds()
assert bounds == [[45.6, -122.9], [45.7, -122.8]], bounds

def test_polygon(self):
"""Test polygon additions."""

self.map = folium.Map(location=[45.60, -122.8])
polygon_templ = self.env.get_template('polygon.js')

# Single Polygon.
locations = [[35.6636, 139.7634],
[35.6629, 139.7664],
[35.6663, 139.7706],
[35.6725, 139.7632],
[35.6728, 139.7627],
[35.6720, 139.7606],
[35.6682, 139.7588],
[35.6663, 139.7627]]
self.map.add_child(Polygon(locations=locations, popup='Hi'))
marker = list(self.map._children.values())[-1]
polygon_1 = polygon_templ.render({'Polygon': marker.get_name(),
'location': locations,
'color': 'black',
'fill_color': 'black',
'fill_opacity': 0.6,
'weight': 1})
assert (''.join(polygon_1.split())[:-1] in
''.join(self.map.get_root().render().split()))

# Second Polygon.
locations = [[35.5636, 138.7634],
[35.5629, 138.7664],
[35.5663, 138.7706],
[35.5725, 138.7632],
[35.5728, 138.7627],
[35.5720, 138.7606],
[35.5682, 138.7588],
[35.5663, 138.7627]]
self.map.add_child(Polygon(locations=locations, color='red',
fill_color='red', fill_opacity=0.7,
weight=3, popup='Hi'))
marker = list(self.map._children.values())[-1]
polygon_2 = polygon_templ.render({'Polygon': marker.get_name(),
'location': locations,
'color': 'red',
'fill_color': 'red',
'fill_opacity': 0.7,
'weight': 3})
assert (''.join(polygon_2.split())[:-1] in
''.join(self.map.get_root().render().split()))

bounds = self.map.get_bounds()
assert bounds == [[[35.5636, 138.7634], [35.5629, 138.7664]],
[[35.6636, 139.7634], [35.6629, 139.7664]]], bounds

def test_poly_marker(self):
"""Test polygon marker."""

Expand Down