Skip to content

Commit 5706269

Browse files
ocefpafConengmo
authored andcommitted
Heatmap bugfix, PEP8 fixes, function ports from branca (#1017)
* fix #928: Heatmap fails on incompatible type * various PEP8 fixes * ported two function from branca to folium utilities
1 parent f477275 commit 5706269

15 files changed

+155
-102
lines changed

folium/__init__.py

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,99 @@
55
import sys
66
import warnings
77

8+
import branca
89
from branca.colormap import (ColorMap, LinearColormap, StepColormap)
9-
from branca.element import (CssLink, Div, Element, Figure, Html, IFrame,
10-
JavascriptLink, Link, MacroElement)
10+
from branca.element import (
11+
CssLink,
12+
Div,
13+
Element,
14+
Figure,
15+
Html,
16+
IFrame,
17+
JavascriptLink,
18+
Link,
19+
MacroElement,
20+
)
1121

1222
from folium._version import get_versions
13-
1423
from folium.features import (
15-
ClickForMarker, ColorLine, CustomIcon, DivIcon, GeoJson,
16-
LatLngPopup, RegularPolygonMarker, TopoJson, Vega, VegaLite,
17-
GeoJsonTooltip, Choropleth
24+
Choropleth,
25+
ClickForMarker,
26+
ColorLine,
27+
CustomIcon,
28+
DivIcon,
29+
GeoJson,
30+
LatLngPopup,
31+
RegularPolygonMarker,
32+
TopoJson,
33+
Vega,
34+
VegaLite,
1835
)
19-
20-
from folium.raster_layers import TileLayer, WmsTileLayer
21-
2236
from folium.folium import Map
23-
2437
from folium.map import (
25-
FeatureGroup, FitBounds, Icon, LayerControl, Marker, Popup, Tooltip
38+
FeatureGroup,
39+
FitBounds,
40+
Icon,
41+
LayerControl,
42+
Marker,
43+
Popup,
44+
Tooltip,
2645
)
46+
from folium.raster_layers import TileLayer, WmsTileLayer
47+
from folium.vector_layers import Circle, CircleMarker, PolyLine, Polygon, Rectangle
2748

28-
from folium.vector_layers import Circle, CircleMarker, PolyLine, Polygon, Rectangle # noqa
29-
30-
import branca
3149
if tuple(int(x) for x in branca.__version__.split('.')[:2]) < (0, 3):
3250
raise ImportError('branca version 0.3.0 or higher is required. '
3351
'Update branca with e.g. `pip install branca --upgrade`.')
3452

3553
if sys.version_info < (3, 0):
3654
warnings.warn(
37-
("folium will stop working with Python 2.7 starting Jan. 1, 2019."
38-
" Please transition to Python 3 before this time."
39-
" Check out https://python3statement.org/ for more info."),
55+
('folium will stop working with Python 2.7 starting Jan. 1, 2019.'
56+
' Please transition to Python 3 before this time.'
57+
' Check out https://python3statement.org/ for more info.'),
4058
PendingDeprecationWarning
4159
)
4260

4361
__version__ = get_versions()['version']
4462
del get_versions
4563

4664
__all__ = [
65+
'Choropleth',
66+
'ClickForMarker',
67+
'ColorLine',
68+
'ColorMap',
4769
'CssLink',
70+
'CustomIcon',
4871
'Div',
72+
'DivIcon',
4973
'Element',
74+
'FeatureGroup',
5075
'Figure',
76+
'FitBounds',
77+
'GeoJson',
78+
'GeoJsonStyle',
79+
'GeoJsonTooltip',
5180
'Html',
5281
'IFrame',
82+
'Icon',
5383
'JavascriptLink',
84+
'LatLngPopup',
85+
'LayerControl',
86+
'LinearColormap',
5487
'Link',
5588
'MacroElement',
56-
'ColorMap',
57-
'ColorLine',
58-
'LinearColormap',
59-
'StepColormap',
6089
'Map',
61-
'FeatureGroup',
62-
'FitBounds',
63-
'Icon',
64-
'LayerControl',
6590
'Marker',
91+
'MarkerCluster',
6692
'Popup',
67-
'Tooltip',
93+
'RegularPolygonMarker',
94+
'StepColormap',
6895
'TileLayer',
69-
'ClickForMarker',
70-
'CustomIcon',
71-
'DivIcon',
72-
'GeoJson',
73-
'GeoJsonStyle',
74-
'LatLngPopup',
75-
'MarkerCluster',
96+
'Tooltip',
97+
'TopoJson',
7698
'Vega',
7799
'VegaLite',
78-
'RegularPolygonMarker',
79-
'TopoJson',
80100
'WmsTileLayer',
81-
'Choropleth',
82101
# vector_layers
83102
'Circle',
84103
'CircleMarker',

folium/features.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@
1010
import json
1111
import warnings
1212

13-
import requests
14-
import numpy as np
15-
1613
from branca.colormap import LinearColormap, StepColormap
1714
from branca.element import (Element, Figure, JavascriptLink, MacroElement)
18-
from branca.utilities import (_locations_tolist, _parse_size, image_to_url,
19-
none_max, none_min, color_brewer)
15+
from branca.utilities import color_brewer
2016

2117
from folium.folium import Map
2218
from folium.map import (FeatureGroup, Icon, Layer, Marker, Tooltip)
23-
24-
from folium.utilities import get_bounds
19+
from folium.utilities import (
20+
_iter_tolist,
21+
_parse_size,
22+
get_bounds,
23+
image_to_url,
24+
none_max,
25+
none_min,
26+
)
2527
from folium.vector_layers import PolyLine
2628

2729
from jinja2 import Template
2830

31+
import numpy as np
32+
33+
import requests
34+
2935
from six import binary_type, text_type
3036

3137

@@ -84,7 +90,7 @@ def __init__(self, location, color='black', opacity=1, weight=2,
8490
fill_color='blue', fill_opacity=1, number_of_sides=4,
8591
rotation=0, radius=15, popup=None, tooltip=None):
8692
super(RegularPolygonMarker, self).__init__(
87-
_locations_tolist(location),
93+
_iter_tolist(location),
8894
popup=popup, tooltip=tooltip
8995
)
9096
self._name = 'RegularPolygonMarker'
@@ -684,26 +690,26 @@ def __init__(self, fields, aliases=None, labels=True,
684690
super(GeoJsonTooltip, self).__init__(
685691
text='', style=style, sticky=sticky, **kwargs
686692
)
687-
self._name = "GeoJsonTooltip"
693+
self._name = 'GeoJsonTooltip'
688694

689-
assert isinstance(fields, (list, tuple)), "Please pass a list or " \
690-
"tuple to fields."
695+
assert isinstance(fields, (list, tuple)), 'Please pass a list or ' \
696+
'tuple to fields.'
691697
if aliases is not None:
692698
assert isinstance(aliases, (list, tuple))
693-
assert len(fields) == len(aliases), "fields and aliases must have" \
694-
" the same length."
695-
assert isinstance(labels, bool), "labels requires a boolean value."
696-
assert isinstance(localize, bool), "localize must be bool."
697-
assert 'permanent' not in kwargs, "The `permanent` option does not " \
698-
"work with GeoJsonTooltip."
699+
assert len(fields) == len(aliases), 'fields and aliases must have' \
700+
' the same length.'
701+
assert isinstance(labels, bool), 'labels requires a boolean value.'
702+
assert isinstance(localize, bool), 'localize must be bool.'
703+
assert 'permanent' not in kwargs, 'The `permanent` option does not ' \
704+
'work with GeoJsonTooltip.'
699705

700706
self.fields = fields
701707
self.aliases = aliases
702708
self.labels = labels
703709
self.localize = localize
704710
if style:
705711
assert isinstance(style, str), \
706-
"Pass a valid inline HTML style property string to style."
712+
'Pass a valid inline HTML style property string to style.'
707713
# noqa outside of type checking.
708714
self.style = style
709715

@@ -720,8 +726,8 @@ def render(self, **kwargs):
720726
'than a GeoJson or TopoJson object.')
721727
keys = tuple(x for x in keys if x not in ('style', 'highlight'))
722728
for value in self.fields:
723-
assert value in keys, ("The field {} is not available in the data. "
724-
"Choose from: {}.".format(value, keys))
729+
assert value in keys, ('The field {} is not available in the data. '
730+
'Choose from: {}.'.format(value, keys))
725731
super(GeoJsonTooltip, self).render(**kwargs)
726732

727733

folium/folium.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
import warnings
1313

1414
from branca.element import CssLink, Element, Figure, JavascriptLink, MacroElement
15-
from branca.utilities import _parse_size
1615

1716
from folium.map import FitBounds
1817
from folium.raster_layers import TileLayer
19-
from folium.utilities import _validate_location
18+
from folium.utilities import _parse_size, _validate_location
2019

2120
from jinja2 import Environment, PackageLoader, Template
2221

folium/map.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
from __future__ import (absolute_import, division, print_function)
99

1010
import json
11-
1211
from collections import OrderedDict
1312

1413
from branca.element import CssLink, Element, Figure, Html, JavascriptLink, MacroElement # noqa
1514

16-
from folium.utilities import _validate_coordinates, get_bounds, camelize
15+
from folium.utilities import _validate_coordinates, camelize, get_bounds
1716

1817
from jinja2 import Template
1918

@@ -371,20 +370,20 @@ class Tooltip(MacroElement):
371370
{% endmacro %}
372371
""")
373372
valid_options = {
374-
"pane": (str, ),
375-
"offset": (tuple, ),
376-
"direction": (str, ),
377-
"permanent": (bool, ),
378-
"sticky": (bool, ),
379-
"interactive": (bool, ),
380-
"opacity": (float, int),
381-
"attribution": (str, ),
382-
"className": (str, ),
373+
'pane': (str, ),
374+
'offset': (tuple, ),
375+
'direction': (str, ),
376+
'permanent': (bool, ),
377+
'sticky': (bool, ),
378+
'interactive': (bool, ),
379+
'opacity': (float, int),
380+
'attribution': (str, ),
381+
'className': (str, ),
383382
}
384383

385384
def __init__(self, text, style=None, sticky=True, **kwargs):
386385
super(Tooltip, self).__init__()
387-
self._name = "Tooltip"
386+
self._name = 'Tooltip'
388387

389388
self.text = str(text)
390389

@@ -393,7 +392,7 @@ def __init__(self, text, style=None, sticky=True, **kwargs):
393392

394393
if style:
395394
assert isinstance(style, str), \
396-
"Pass a valid inline HTML style property string to style."
395+
'Pass a valid inline HTML style property string to style.'
397396
# noqa outside of type checking.
398397
self.style = style
399398

@@ -402,11 +401,11 @@ def parse_options(self, kwargs):
402401
kwargs = {camelize(key): value for key, value in kwargs.items()}
403402
for key in kwargs.keys():
404403
assert key in self.valid_options, (
405-
"The option {} is not in the available options: {}."
404+
'The option {} is not in the available options: {}.'
406405
.format(key, ', '.join(self.valid_options))
407406
)
408407
assert isinstance(kwargs[key], self.valid_options[key]), (
409-
"The option {} must be one of the following types: {}."
408+
'The option {} must be one of the following types: {}.'
410409
.format(key, self.valid_options[key])
411410
)
412411
return json.dumps(kwargs)

folium/plugins/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
from folium.plugins.heat_map_withtime import HeatMapWithTime
2222
from folium.plugins.marker_cluster import MarkerCluster
2323
from folium.plugins.measure_control import MeasureControl
24+
from folium.plugins.minimap import MiniMap
2425
from folium.plugins.mouse_position import MousePosition
2526
from folium.plugins.polyline_text_path import PolyLineTextPath
2627
from folium.plugins.scroll_zoom_toggler import ScrollZoomToggler
28+
from folium.plugins.search import Search
2729
from folium.plugins.terminator import Terminator
2830
from folium.plugins.time_slider_choropleth import TimeSliderChoropleth
2931
from folium.plugins.timestamped_geo_json import TimestampedGeoJson
3032
from folium.plugins.timestamped_wmstilelayer import TimestampedWmsTileLayers
31-
from folium.plugins.search import Search
32-
from folium.plugins.minimap import MiniMap
3333

3434
__all__ = [
3535
'BeautifyIcon',
@@ -43,13 +43,13 @@
4343
'HeatMapWithTime',
4444
'MarkerCluster',
4545
'MeasureControl',
46+
'MiniMap',
4647
'MousePosition',
4748
'PolyLineTextPath',
4849
'ScrollZoomToggler',
50+
'Search',
4951
'Terminator',
5052
'TimeSliderChoropleth',
5153
'TimestampedGeoJson',
5254
'TimestampedWmsTileLayers',
53-
'Search',
54-
'MiniMap',
5555
]

folium/plugins/beautify_icon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import json
66

7-
from branca.element import Figure, JavascriptLink, CssLink, MacroElement
8-
9-
from six import iteritems
7+
from branca.element import CssLink, Figure, JavascriptLink, MacroElement
108

119
from jinja2 import Template
1210

11+
from six import iteritems
12+
1313

1414
class BeautifyIcon(MacroElement):
1515
"""

folium/plugins/heat_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import json
66

77
from branca.element import Figure, JavascriptLink
8-
from branca.utilities import none_max, none_min
98

109
from folium.map import Layer
11-
from folium.utilities import _isnan
10+
from folium.utilities import _isnan, _iter_tolist, none_max, none_min
1211

1312
from jinja2 import Template
1413

@@ -65,6 +64,7 @@ def __init__(self, data, name=None, min_opacity=0.5, max_zoom=18,
6564
overlay=True, control=True, show=True):
6665
super(HeatMap, self).__init__(name=name, overlay=overlay,
6766
control=control, show=show)
67+
data = _iter_tolist(data)
6868
if _isnan(data):
6969
raise ValueError('data cannot contain NaNs, '
7070
'got:\n{!r}'.format(data))

folium/plugins/heat_map_withtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22

33
from branca.element import CssLink, Element, Figure, JavascriptLink
4-
from branca.utilities import none_max, none_min
54

65
from folium.map import Layer
6+
from folium.utilities import none_max, none_min
77

88
from jinja2 import Template
99

folium/plugins/marker_cluster.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import (absolute_import, division, print_function)
3+
from __future__ import absolute_import, division, print_function
4+
45
import json
56

67
from branca.element import CssLink, Figure, JavascriptLink

0 commit comments

Comments
 (0)