Skip to content

Commit b902e81

Browse files
authored
Merge pull request #406 from VasavanThiru/geodata_on_hover
Geodata on hover
2 parents 4c9d021 + fc982ff commit b902e81

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

examples/GeoData_on_hover.ipynb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"scrolled": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"from ipyleaflet import Map, GeoData, basemaps, LayersControl\n",
12+
"import geopandas\n",
13+
"import json\n",
14+
"\n",
15+
"countries = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))\n",
16+
"rivers = geopandas.read_file(\"https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_rivers_lake_centerlines.zip\")\n",
17+
"\n",
18+
"m = Map(center=(28.6019917,70.9121356), zoom = 3, basemap= basemaps.Esri.WorldTopoMap)\n",
19+
"\n",
20+
"geo_data = GeoData(geo_dataframe = countries,\n",
21+
" style={'color': 'black', 'fillColor': '#366370', 'opacity':0.05, 'weight':1.9, 'dashArray':'2', 'fillOpacity':0.6},\n",
22+
" hover_style={'fillColor': '#b08a3e' , 'fillOpacity': 0.9},\n",
23+
" name = 'Countries')\n",
24+
"\n",
25+
"\n",
26+
"m.add_layer(geo_data)\n",
27+
"m"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"from ipywidgets import Text, HTML\n",
37+
"from ipyleaflet import WidgetControl, GeoJSON \n",
38+
"\n",
39+
"\n",
40+
"html = HTML('''\n",
41+
" <h4> population density</h4>\n",
42+
" Hover over a state\n",
43+
"''')\n",
44+
"html.layout.margin = '0px 20px 20px 20px'\n",
45+
"control = WidgetControl(widget=html, position='topright')\n",
46+
"m.add_control(control)"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"def update_html(feature, **kwargs):\n",
56+
" html.value = '''\n",
57+
" <h5>Population density</h5>\n",
58+
" <h5><b>{}</b></h5>\n",
59+
" {} people \n",
60+
" <h5><b> Continent : {} </b></h5>\n",
61+
" <h15> GDP : {} </h15> \n",
62+
" '''.format(feature['properties']['name'],\n",
63+
" feature['properties']['pop_est'],\n",
64+
" feature['properties']['continent'],\n",
65+
" feature['properties']['gdp_md_est'])\n",
66+
"\n",
67+
"geo_data.on_hover(update_html)"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"from ipyleaflet import FullScreenControl, LayersControl\n",
77+
"m.add_control(FullScreenControl())"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 3",
91+
"language": "python",
92+
"name": "python3"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 3
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython3",
104+
"version": "3.7.3"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 2
109+
}

ipyleaflet/leaflet.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def _handle_leaflet_event(self, _, content, buffers):
253253
def on_load(self, callback, remove=False):
254254
self._load_callbacks.register_callback(callback, remove=remove)
255255

256-
def redraw(self):
256+
def redraw(self):
257257
self.send({'msg':'redraw'})
258258

259259
class LocalTileLayer(TileLayer):
@@ -514,7 +514,7 @@ class GeoData(GeoJSON):
514514
geo_dataframe = Instance('geopandas.GeoDataFrame')
515515

516516
def __init__(self, **kwargs):
517-
super(GeoJSON, self).__init__(**kwargs)
517+
super(GeoData, self).__init__(**kwargs)
518518
self.data = self._get_data()
519519

520520
@observe('geo_dataframe')
@@ -860,7 +860,7 @@ class Map(DOMWidget, InteractMixin):
860860
style = InstanceDict(MapStyle).tag(sync=True, **widget_serialization)
861861
default_style = InstanceDict(MapStyle).tag(sync=True, **widget_serialization)
862862
dragging_style = InstanceDict(MapStyle).tag(sync=True, **widget_serialization)
863-
863+
864864
zoom_control = Bool(True)
865865
zoom_control_instance = ZoomControl()
866866

@@ -905,10 +905,10 @@ def __init__(self, **kwargs):
905905

906906
if self.zoom_control:
907907
self.add_control(self.zoom_control_instance)
908-
908+
909909
if self.attribution_control:
910910
self.add_control(self.attribution_control_instance)
911-
911+
912912
@observe('zoom_control')
913913
def observe_zoom_control(self, change):
914914
if change['new']:

0 commit comments

Comments
 (0)