diff --git a/examples/GeoJSON_and_choropleth.ipynb b/examples/GeoJSON_and_choropleth.ipynb
index 734a76689..bfc6d5481 100644
--- a/examples/GeoJSON_and_choropleth.ipynb
+++ b/examples/GeoJSON_and_choropleth.ipynb
@@ -9,7 +9,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "0.10.1+9.g3c75288.dirty\n"
+ "0.11.0+16.ge442d52.dirty\n"
]
}
],
@@ -31,6 +31,7 @@
"\n",
"## Using `GeoJson`\n",
"\n",
+ "### Loading data\n",
"\n",
"Let us load a GeoJSON file representing the US states."
]
@@ -98,10 +99,10 @@
{
"data": {
"text/html": [
- "
"
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 3,
@@ -114,8 +115,6 @@
"\n",
"folium.GeoJson(geo_json_data).add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_0.html'))\n",
- "\n",
"m"
]
},
@@ -134,10 +133,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 4,
@@ -150,8 +149,6 @@
"\n",
"folium.GeoJson(us_states).add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_1.html'))\n",
- "\n",
"m"
]
},
@@ -170,10 +167,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 5,
@@ -192,7 +189,46 @@
" gdf,\n",
").add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_3.html'))\n",
+ "m"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Click on zoom\n",
+ "\n",
+ "You can enable an option that if you click on a part of the geometry the map will zoom in to that.\n",
+ "\n",
+ "Try it on the map below:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "m = folium.Map([43, -100], zoom_start=4)\n",
+ "\n",
+ "folium.GeoJson(\n",
+ " geo_json_data,\n",
+ " zoom_on_click=True\n",
+ ").add_to(m)\n",
"\n",
"m"
]
@@ -201,6 +237,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "### Styling\n",
+ "\n",
"Now this is cool and simple, but we may be willing to choose the style of the data.\n",
"\n",
"You can provide a function of the form `lambda feature: {}` that sets the style of each feature.\n",
@@ -213,19 +251,19 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 6,
+ "execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -243,8 +281,6 @@
" }\n",
").add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_3.html'))\n",
- "\n",
"m"
]
},
@@ -257,19 +293,19 @@
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 7,
+ "execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -287,8 +323,6 @@
" }\n",
").add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_4.html'))\n",
- "\n",
"m"
]
},
@@ -305,7 +339,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 9,
"metadata": {
"scrolled": true
},
@@ -374,7 +408,7 @@
"4 CA 10.1"
]
},
- "execution_count": 8,
+ "execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -399,7 +433,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -415,10 +449,10 @@
"3.2 10.3 "
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 9,
+ "execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -444,7 +478,7 @@
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -453,7 +487,7 @@
"7.1"
]
},
- "execution_count": 10,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -473,19 +507,19 @@
},
{
"cell_type": "code",
- "execution_count": 11,
+ "execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 11,
+ "execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -507,8 +541,6 @@
"\n",
"folium.LayerControl().add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_5.html'))\n",
- "\n",
"m"
]
},
@@ -516,12 +548,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Of course, if you can create and/or use a dictionnary providing directly the good color. Thus, the finishing seems faster:"
+ "Of course, if you can create and/or use a dictionary providing directly the good color. Thus, the finishing seems faster:"
]
},
{
"cell_type": "code",
- "execution_count": 12,
+ "execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -530,19 +562,16 @@
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
- "text/html": [
- ""
- ],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 13,
+ "execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -559,11 +588,7 @@
" 'dashArray': '5, 5',\n",
" 'fillOpacity': 0.9,\n",
" }\n",
- ").add_to(m)\n",
- "\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_6.html'))\n",
- "\n",
- "m"
+ ").add_to(m)"
]
},
{
@@ -575,19 +600,19 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 14,
+ "execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -596,8 +621,6 @@
"colormap.caption = 'Unemployment color scale'\n",
"colormap.add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_7.html'))\n",
- "\n",
"m"
]
},
@@ -609,24 +632,24 @@
"\n",
"Now if you want to get faster, you can use the `Choropleth` class. Have a look at it's docstring, it has several styling options.\n",
"\n",
- "You can use it in providing a file name (`geo_path`) :"
+ "Just like the `GeoJson` class you can provide it a filename, a dict, or a geopandas object."
]
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
- "execution_count": 15,
+ "execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -635,49 +658,11 @@
"m = folium.Map([43, -100], zoom_start=4)\n",
"\n",
"folium.Choropleth(\n",
- " geo_data=geopandas.read_file(us_states),\n",
+ " geo_data=us_states,\n",
" fill_opacity=0.3,\n",
" line_weight=2,\n",
").add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_7.html'))\n",
- "\n",
- "m"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Or in providing a GeoJSON string (`geo_str`) :"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- ""
- ],
- "text/plain": [
- ""
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "m = folium.Map([43, -100], zoom_start=4)\n",
- "\n",
- "folium.Choropleth(geo_data=us_states).add_to(m)\n",
- "\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_8.html'))\n",
- "\n",
"m"
]
},
@@ -696,10 +681,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 17,
@@ -717,9 +702,6 @@
" key_on='feature.id',\n",
").add_to(m)\n",
"\n",
- "\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_9.html'))\n",
- "\n",
"m"
]
},
@@ -738,10 +720,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 18,
@@ -761,9 +743,6 @@
" bins=[3, 4, 9, 11]\n",
").add_to(m)\n",
"\n",
- "\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_10.html'))\n",
- "\n",
"m"
]
},
@@ -784,10 +763,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 19,
@@ -811,8 +790,6 @@
" highlight=True\n",
").add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_11.html'))\n",
- "\n",
"m"
]
},
@@ -833,10 +810,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 20,
@@ -860,9 +837,6 @@
" fill_color='YlGn'\n",
").add_to(m)\n",
"\n",
- "\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_10.html'))\n",
- "\n",
"m"
]
},
@@ -889,10 +863,10 @@
{
"data": {
"text/html": [
- ""
+ "Make this Notebook Trusted to load map: File -> Trust Notebook
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 21,
@@ -919,8 +893,6 @@
"\n",
"folium.LayerControl(collapsed=False).add_to(m)\n",
"\n",
- "m.save(os.path.join('results', 'GeoJSON_and_choropleth_11.html'))\n",
- "\n",
"m"
]
}
diff --git a/folium/features.py b/folium/features.py
index 1ea826ebd..8f121294e 100644
--- a/folium/features.py
+++ b/folium/features.py
@@ -354,6 +354,8 @@ class GeoJson(Layer):
embed: bool, default True
Whether to embed the data in the html file or not. Note that disabling
embedding is only supported if you provide a file link or URL.
+ zoom_on_click: bool, default False
+ Set to True to enable zooming in on a geometry when clicking on it.
Examples
--------
@@ -409,9 +411,13 @@ class GeoJson(Layer):
e.target.setStyle({{ this.get_name() }}_highlighter(e.target.feature));
},
{%- endif %}
+ {%- if this.zoom_on_click %}
click: function(e) {
- {{ this.parent_map.get_name() }}.fitBounds(e.target.getBounds());
+ if (typeof e.target.getBounds === 'function') {
+ {{ this.parent_map.get_name() }}.fitBounds(e.target.getBounds());
+ }
}
+ {%- endif %}
});
};
var {{ this.get_name() }} = L.geoJson(null, {
@@ -438,7 +444,8 @@ class GeoJson(Layer):
def __init__(self, data, style_function=None, highlight_function=None, # noqa
name=None, overlay=True, control=True, show=True,
- smooth_factor=None, tooltip=None, embed=True, popup=None):
+ smooth_factor=None, tooltip=None, embed=True, popup=None,
+ zoom_on_click=False):
super(GeoJson, self).__init__(name=name, overlay=overlay,
control=control, show=show)
self._name = 'GeoJson'
@@ -449,6 +456,7 @@ def __init__(self, data, style_function=None, highlight_function=None, # noqa
self.smooth_factor = smooth_factor
self.style = style_function is not None
self.highlight = highlight_function is not None
+ self.zoom_on_click = zoom_on_click
self.data = self.process_data(data)