Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
778ce5c
Fixes #341: Added highlight_function functionality for choropleth maps
joshuacano Aug 22, 2016
62a46d3
Fixes #341: Added highlight_function functionality for choropleth maps
joshuacano Aug 22, 2016
65661de
Merge branch 'highlight' of https://github.com/joshuacano/folium into…
joshuacano Aug 22, 2016
c6c9f04
Fixes #341: Added Highlight Test
joshuacano Sep 14, 2016
3ff3117
Fixes #341: Added Highlight switch
joshuacano Sep 14, 2016
dfa7a44
Fixes #341: Added item to Changes
joshuacano Sep 14, 2016
cd90f4c
Fixes #341: Added examples to notebook
joshuacano Sep 14, 2016
8c8c4a8
Fixes #341: Added Highlight Test
joshuacano Sep 14, 2016
5500bbb
Fixes #341: Added Highlight switch
joshuacano Sep 14, 2016
9f5a111
Fixes #341: Added item to Changes
joshuacano Sep 14, 2016
33539fc
Fixes #341: Added examples to notebook
joshuacano Sep 14, 2016
01fb74c
Merge branch 'highlight' of https://github.com/joshuacano/folium into…
joshuacano Sep 22, 2016
e25111c
Merge remote-tracking branch 'upstream/master'
joshuacano Sep 22, 2016
b0120e8
Fixes #341: Added Highlight Test
joshuacano Sep 14, 2016
b0493c4
Fixes #341: Added Highlight switch
joshuacano Sep 14, 2016
203a5d0
Fixes #341: Added item to Changes
joshuacano Sep 14, 2016
4be246f
Fixes #341: Added examples to notebook
joshuacano Sep 14, 2016
8ad8ff0
Fixes #341: Added Highlight Test
joshuacano Sep 14, 2016
5574b10
Merge branch 'highlight' of https://github.com/joshuacano/folium into…
joshuacano Sep 22, 2016
de57eac
Fixes #341: Moved example to renamed file
joshuacano Sep 22, 2016
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
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added options to LayerControl (qingkaikong #473)
- Added text path (talespaiva #451 and #474)
- Update font-awesome to 4.6.3 (ocefpaf #478)
- Add highlight function to GeoJSON, and Chrorpleth (JoshuaCano #341)

Bug Fixes

Expand Down
31 changes: 31 additions & 0 deletions examples/GeoJSON_and_choropleth.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,37 @@
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also enable the highlight function, to enable highlight functionality when you hover over each area."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this cell was not executed.

"source": [
"state_geo = r'us-states.json'\n",
"state_unemployment = r'US_Unemployment_Oct2012.csv'\n",
"\n",
"state_data = pd.read_csv(state_unemployment)\n",
"\n",
"#Let Folium determine the scale\n",
"states = folium.Map(location=[48, -102], zoom_start=3)\n",
"states.choropleth(geo_path=state_geo, data=state_data,\n",
" columns=['State', 'Unemployment'],\n",
" key_on='feature.id',\n",
" fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2,\n",
" legend_name='Unemployment Rate (%)', highlight=True)\n",
"\n",
"states"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
55 changes: 40 additions & 15 deletions examples/folium_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand All @@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand All @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand All @@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -137,7 +137,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -165,10 +165,11 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {
"collapsed": false
},
<<<<<<< HEAD
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we have a conflict remaining here. Don't worry I will fix this in another PR.

"outputs": [
{
"data": {
Expand All @@ -194,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -229,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -322,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -367,7 +368,31 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"state_geo = r'us-states.json'\n",
"state_unemployment = r'US_Unemployment_Oct2012.csv'\n",
"\n",
"state_data = pd.read_csv(state_unemployment)\n",
"\n",
"#Enable Highlighting function, to allow hover highlight function\n",
"states = folium.Map(location=[48, -102], zoom_start=3)\n",
"states.choropleth(geo_path=state_geo, data=state_data,\n",
" columns=['State', 'Unemployment'],\n",
" key_on='feature.id',\n",
" fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2,\n",
" legend_name='Unemployment Rate (%)', highlight=True)\n",
"\n",
"states"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand All @@ -394,13 +419,13 @@
" key_on='feature.id',\n",
" fill_color='BuPu', fill_opacity=0.7, line_opacity=0.5,\n",
" legend_name='Unemployment Rate (%)',\n",
" reset=True)\n",
" reset=True, highli)\n",
"states2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -453,7 +478,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -497,7 +522,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {
"collapsed": false
},
Expand Down
48 changes: 43 additions & 5 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ class GeoJson(Layer):
>>> GeoJson(geojson, style_function=style_function)
"""
def __init__(self, data, style_function=None, name=None,
overlay=True, control=True, smooth_factor=None):
overlay=True, control=True, smooth_factor=None,
highlight_function=None):
super(GeoJson, self).__init__(name=name, overlay=overlay,
control=control)
self._name = 'GeoJson'
Expand Down Expand Up @@ -332,19 +333,55 @@ def __init__(self, data, style_function=None, name=None,

if style_function is None:
def style_function(x):
return {}
return {}

self.style_function = style_function

self.highlight = highlight_function is not None

if highlight_function is None:
def highlight_function(x):
return {}

self.highlight_function = highlight_function

self.smooth_factor = smooth_factor

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

{% if this.highlight %}
{{this.get_name()}}_onEachFeature = function onEachFeature(feature, layer) {
layer.on({
mouseout: function(e) {
e.target.setStyle(e.target.feature.properties.style);},
mouseover: function(e) {
e.target.setStyle(e.target.feature.properties.highlight);},
click: function(e) {
{{this._parent.get_name()}}.fitBounds(e.target.getBounds());}
});
};
{% endif %}

var {{this.get_name()}} = L.geoJson(
{% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %}
{% if this.smooth_factor is not none %}
, {smoothFactor:{{this.smooth_factor}}}
{% endif %}).addTo({{this._parent.get_name()}});
{% if this.smooth_factor is not none or this.highlight %}
, {
{% if this.smooth_factor is not none %}
smoothFactor:{{this.smooth_factor}}
{% endif %}

{% if this.highlight %}
{% if this.smooth_factor is not none %}
,
{% endif %}
onEachFeature: {{this.get_name()}}_onEachFeature
{% endif %}
}
{% endif %}
).addTo({{this._parent.get_name()}});
{{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});

{% endmacro %}
""") # noqa

Expand All @@ -363,6 +400,7 @@ def style_data(self):

for feature in self.data['features']:
feature.setdefault('properties', {}).setdefault('style', {}).update(self.style_function(feature)) # noqa
feature.setdefault('properties', {}).setdefault('highlight', {}).update(self.highlight_function(feature)) # noqa
return json.dumps(self.data, sort_keys=True)

def _get_self_bounds(self):
Expand Down
23 changes: 19 additions & 4 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
ClickForMarker, TopoJson, PolyLine, MultiPolyLine,
)


def initialize_notebook():
"""Initialize the IPython notebook display elements."""
warnings.warn("%s is deprecated and no longer required." %
("initialize_notebook",),
FutureWarning, stacklevel=2)
pass


class Map(LegacyMap):
"""Create a Map with Folium and Leaflet.js

Expand Down Expand Up @@ -510,7 +508,8 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
data=None, columns=None, key_on=None, threshold_scale=None,
fill_color='blue', fill_opacity=0.6, line_color='black',
line_weight=1, line_opacity=1, legend_name="",
topojson=None, reset=False, smooth_factor=None):
topojson=None, reset=False, smooth_factor=None,
highlight=None):
"""
Apply a GeoJSON overlay to the map.

Expand Down Expand Up @@ -582,6 +581,8 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
How much to simplify the polyline on each zoom level. More means
better performance and smoother look, and less means more accurate
representation. Leaflet defaults to 1.0.
highlight: boolean, default False
Enable highlight functionality when hovering over a GeoJSON area.

Returns
-------
Expand All @@ -598,6 +599,12 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
... threshold_scale=[0, 20, 30, 40, 50, 60])
>>> m.choropleth(geo_path='countries.json',
... topojson='objects.countries')
>>> m.choropleth(geo_path='geo.json', data=df,
... columns=['Data 1', 'Data 2'],
... key_on='feature.properties.myvalue',
... fill_color='PuBu',
... threshold_scale=[0, 20, 30, 40, 50, 60],
... highlight=True)

"""
if threshold_scale and len(threshold_scale) > 6:
Expand Down Expand Up @@ -679,6 +686,13 @@ def style_function(x):
"fillColor": color_scale_fun(x)
}


def highlight_function(x):
return {
"weight": line_weight + 2,
"fillOpacity": fill_opacity + .2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These looks like sensible defaults. Thanks!

}

if topojson:
geo_json = TopoJson(
geo_data,
Expand All @@ -689,7 +703,8 @@ def style_function(x):
geo_json = GeoJson(
geo_data,
style_function=style_function,
smooth_factor=smooth_factor)
smooth_factor=smooth_factor,
highlight_function=highlight_function if highlight else None)

self.add_child(geo_json)

Expand Down
Loading