1+ ``` {code-cell} ipython3
2+ ---
3+ nbsphinx: hidden
4+ ---
5+ import folium
6+ ```
7+
18## Using ` GeoJson `
29
310### Loading data
411
512Let us load a GeoJSON file representing the US states.
613
714``` {code-cell} ipython3
8- import json
9-
10- import requests
11-
12- url = (
13- "https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
14- )
15- us_states = f"{url}/us-states.json"
16-
17- geo_json_data = json.loads(requests.get(us_states).text)
15+ geo_json_data = folium.example_data.us_states_geojson()
1816```
1917
2018It is a classical GeoJSON ` FeatureCollection ` (see https://en.wikipedia.org/wiki/GeoJSON ) of the form :
@@ -44,14 +42,9 @@ It is a classical GeoJSON `FeatureCollection` (see https://en.wikipedia.org/wiki
4442 ]
4543 }
4644
47- +++
48-
4945A first way of drawing it on a map, is simply to use ` folium.GeoJson ` :
5046
5147``` {code-cell} ipython3
52- import folium
53-
54-
5548m = folium.Map([43, -100], zoom_start=4)
5649
5750folium.GeoJson(geo_json_data).add_to(m)
@@ -64,7 +57,9 @@ Note that you can avoid loading the file on yourself ; in simply providing a fil
6457``` {code-cell} ipython3
6558m = folium.Map([43, -100], zoom_start=4)
6659
67- folium.GeoJson(us_states).add_to(m)
60+ filepath = folium.example_data.get_path("us_states.json")
61+
62+ folium.GeoJson(filepath).add_to(m)
6863
6964m
7065```
@@ -74,7 +69,7 @@ You can pass a geopandas object.
7469``` {code-cell} ipython3
7570import geopandas
7671
77- gdf = geopandas.read_file(us_states )
72+ gdf = geopandas.read_file(filepath )
7873
7974m = folium.Map([43, -100], zoom_start=4)
8075
@@ -153,10 +148,7 @@ Let's imagine we want to draw a choropleth of unemployment in the US.
153148First, we may load the data:
154149
155150``` {code-cell} ipython3
156- import pandas as pd
157-
158- US_Unemployment_Oct2012 = f"{url}/US_Unemployment_Oct2012.csv"
159- unemployment = pd.read_csv(US_Unemployment_Oct2012)
151+ unemployment = folium.example_data.us_unemployment_pandas_dataframe()
160152
161153unemployment.head(5)
162154```
0 commit comments