Skip to content

Commit 7a11469

Browse files
authored
Merge pull request #538 from martinRenou/choropleth_doc
GeoJSON/Choropleth/GeoData improvements
2 parents 5ef7a91 + 2c52c2a commit 7a11469

File tree

4 files changed

+76
-54
lines changed

4 files changed

+76
-54
lines changed

docs/source/api_reference/choropleth.rst

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ Example
4545
m
4646

4747

48-
Information
49-
-----------
48+
Usage
49+
-----
5050

5151
The ``Choropleth`` takes ``geo_data`` and ``choro_data`` as arguments.
5252

@@ -69,23 +69,38 @@ The ``geo_data`` is a `GeoJSON
6969
}]
7070
}
7171
72-
The ``choro_data`` is a dictionary that takes ``'id'`` from ``'features'`` as key and float as value, in order to build the colormap :
72+
The ``choro_data`` is a dictionary that maps an key to a float value, in order to build the colormap :
7373

7474
.. code::
7575
7676
{'AL': 7.1,
7777
'AK': 6.8}
7878
7979
80+
The ``Choropleth`` layer is then created specifying on which key the colormap is applied:
81+
82+
.. code::
83+
84+
Choropleth(
85+
geo_data=geo_data,
86+
choro_data=choro_data,
87+
key_on='id'
88+
)
89+
90+
8091
Attributes
8192
----------
8293

83-
============ ========================== ===========
84-
Attribute Doc Description
85-
============ ========================== ===========
86-
geo_data Data dictionary GeoJSON dictionary
87-
choro_data Choropleth data dictionary Dictionary id/float
88-
value_min Color scale minimum value
89-
value_max Color scale maximum value
90-
colormap Map of color from branca
91-
============ ========================== ===========
94+
============== ========================== ===========
95+
Attribute Default Doc
96+
============== ========================== ===========
97+
geo_data {} Data dictionary
98+
choro_data {} Mapping key -> float data for constructing the colormap
99+
key_on 'id' Key used for the colormap construction
100+
value_min Color scale minimum value
101+
value_max Color scale maximum value
102+
colormap OrRd_06 Map of color from branca
103+
style Style dictionary
104+
hover_style Hover style dictionary
105+
style_callback Styling function that is called for each feature, and should return the feature style. This styling function takes the feature, the colormap function and the key data as arguments.
106+
============== ========================== ===========

docs/source/api_reference/geo_json.rst

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ Example
66

77
.. jupyter-execute::
88

9-
from ipyleaflet import Map, GeoJSON
10-
import json
119
import os
10+
import json
11+
import random
1212
import requests
1313

14+
from ipyleaflet import Map, GeoJSON
15+
1416
if not os.path.exists('europe_110.geo.json'):
1517
url = 'https://github.com/jupyter-widgets/ipyleaflet/raw/master/examples/europe_110.geo.json'
1618
r = requests.get(url)
@@ -20,22 +22,51 @@ Example
2022
with open('europe_110.geo.json', 'r') as f:
2123
data = json.load(f)
2224

25+
def random_color(feature):
26+
return {
27+
'color': 'black',
28+
'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
29+
}
30+
2331
m = Map(center=(50.6252978589571, 0.34580993652344), zoom=3)
24-
geo_json = GeoJSON(data=data, style = {'color': 'green', 'opacity':1, 'weight':1.9, 'dashArray':'9', 'fillOpacity':0.1})
32+
33+
geo_json = GeoJSON(
34+
data=data,
35+
style={
36+
'opacity': 1, 'dashArray': '9', 'fillOpacity': 0.1, 'weight': 1
37+
},
38+
hover_style={
39+
'color': 'white', 'dashArray': '0', 'fillOpacity': 0.5
40+
},
41+
style_callback=random_color
42+
)
2543
m.add_layer(geo_json)
44+
2645
m
2746

2847

48+
Usage
49+
-----
50+
51+
The ``GeoJSON`` layer is a widget, which means that you can update the ``data`` or any other attribute from Python and it will dynamically update the map:
52+
53+
.. code::
54+
55+
geo_json.data = new_data
56+
geo_json.hover_style = new_hover_style
57+
58+
2959
Attributes
3060
----------
3161

32-
============ ===
33-
Attribute Doc
34-
============ ===
35-
data Data dictionary
36-
style Style dictionary
37-
hover_style Hover style dictionary
38-
============ ===
62+
============== ===
63+
Attribute Doc
64+
============== ===
65+
data Data dictionary
66+
style Style dictionary
67+
hover_style Hover style dictionary
68+
style_callback Styling function that is called for each feature, and should return the feature style. This styling function takes the feature as argument.
69+
============== ===
3970

4071
Methods
4172
-------

examples/Choropleth.ipynb

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"layer = ipyleaflet.Choropleth(\n",
3737
" geo_data=geo_json_data,\n",
3838
" choro_data=unemployment,\n",
39-
" colormap=linear.YlOrRd_04, \n",
40-
" border_color='black',\n",
39+
" colormap=linear.YlOrRd_04,\n",
4140
" style={'fillOpacity': 0.8, 'dashArray': '5, 5'}\n",
4241
")"
4342
]
@@ -62,7 +61,8 @@
6261
"def compute_style(feature, colormap, choro_data):\n",
6362
" return {\n",
6463
" 'fillColor': colormap(choro_data),\n",
65-
" 'weight': random.randint(1, 5),\n",
64+
" 'color': 'white',\n",
65+
" 'weight': random.randint(1, 3),\n",
6666
" }"
6767
]
6868
},
@@ -123,15 +123,6 @@
123123
"layer.colormap = linear.RdBu_11"
124124
]
125125
},
126-
{
127-
"cell_type": "code",
128-
"execution_count": null,
129-
"metadata": {},
130-
"outputs": [],
131-
"source": [
132-
"layer.border_color = 'white'"
133-
]
134-
},
135126
{
136127
"cell_type": "code",
137128
"execution_count": null,
@@ -159,20 +150,6 @@
159150
"\n",
160151
"layer.on_hover(update_html)"
161152
]
162-
},
163-
{
164-
"cell_type": "code",
165-
"execution_count": null,
166-
"metadata": {},
167-
"outputs": [],
168-
"source": []
169-
},
170-
{
171-
"cell_type": "code",
172-
"execution_count": null,
173-
"metadata": {},
174-
"outputs": [],
175-
"source": []
176153
}
177154
],
178155
"metadata": {
@@ -191,7 +168,7 @@
191168
"name": "python",
192169
"nbconvert_exporter": "python",
193170
"pygments_lexer": "ipython3",
194-
"version": "3.7.3"
171+
"version": "3.8.2"
195172
}
196173
},
197174
"nbformat": 4,

ipyleaflet/leaflet.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def _validate_style_callback(self, proposal):
547547
raise TraitError('style_callback should be callable (functor/function/lambda)')
548548
return proposal.value
549549

550-
@observe('style', 'style_callback')
550+
@observe('data', 'style', 'style_callback')
551551
def _update_data(self, change):
552552
self.data = self._get_data()
553553

@@ -591,14 +591,13 @@ def on_hover(self, callback, remove=False):
591591

592592

593593
class GeoData(GeoJSON):
594-
595594
geo_dataframe = Instance('geopandas.GeoDataFrame')
596595

597596
def __init__(self, **kwargs):
598597
super(GeoData, self).__init__(**kwargs)
599598
self.data = self._get_data()
600599

601-
@observe('geo_dataframe')
600+
@observe('geo_dataframe', 'style', 'style_callback')
602601
def _update_data(self, change):
603602
self.data = self._get_data()
604603

@@ -619,8 +618,8 @@ def _update_bounds(self, change):
619618
self.value_min = min(self.choro_data.items(), key=lambda x: x[1])[1]
620619
self.value_max = max(self.choro_data.items(), key=lambda x: x[1])[1]
621620

622-
@observe('value_min', 'value_max', 'geo_data', 'choro_data', 'colormap')
623-
def _update_choropleth_data(self, change):
621+
@observe('style', 'style_callback', 'value_min', 'value_max', 'geo_data', 'choro_data', 'colormap')
622+
def _update_data(self, change):
624623
self.data = self._get_data()
625624

626625
@default('colormap')

0 commit comments

Comments
 (0)