In [1]:
import pandas as pd
import json
import sys
sys.path.append('..')
import folium
print (folium.__file__)
print (folium.__version__)
Let us load a GeoJSON file reprenseting the US states.
In [2]:
geo_json_data = json.load(open('us-states.json'))
It is a classical GeoJSON FeatureCollection
(see https://en.wikipedia.org/wiki/GeoJSON) of the form :
{
"type" : "FeatureCollection",
"features" : [
{
"properties" : {"name": "Alabama"},
"id" : "AL",
"type" : "Feature",
"geometry" : {
"type": "Polygon",
"coordinates": [[[-87.359296, 35.00118], ...]]
}
},
{
"properties" : {"name": "Alaska"},
"id" : "AK",
"type" : "Feature",
"geometry" : {
"type": "MultiPolygon",
"coordinates": [[[[-131.602021, 55.117982], ... ]]]
}
},
...
]
}
A first way of drawing it on a map, is simply to use folium.GeoJson
:
In [3]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(geo_json_data).add_to(m)
m
Out[3]:
Note that you can avoid loading the file on yourself ; in simply providing a file object.
In [4]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(open('us-states.json')).add_to(m)
m
Out[4]:
If you provide only the filename, Folium will consider you don't want to embed the data in the map. You'll need to copy the file on the server if you want it to be rendered.
In [5]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson('us-states.json').add_to(m)
m
Out[5]:
Now this is cool and simple, but we may be willing to choose the style of the data.
You can provide a function of the form lambda feature: {}
that sets the style of each feature.
For possible options, see:
Point
and MultiPoint
, see http://leafletjs.com/reference.html#marker
In [6]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(
geo_json_data,
style_function=lambda feature: {
'fillColor': '#ffff00',
'color' : 'black',
'weight' : 2,
'dashArray' : '5, 5'
}
).add_to(m)
m
Out[6]:
What's cool in providing a function, is that you can specify a style depending on the feature. For example, if you want to visualize in green all states whose name contains the letter 'E', just do:
In [7]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(
geo_json_data,
style_function=lambda feature: {
'fillColor': 'green' if 'e' in feature['properties']['name'].lower() else '#ffff00',
'color' : 'black',
'weight' : 2,
'dashArray' : '5, 5'
}
).add_to(m)
m
Out[7]:
Wow, this looks almost like a choropleth. To do one, we just need to compute a color for each state.
Let's imagine we want to draw a choropleth of unemployment in the US.
First, we may load the data:
In [8]:
unemployment = pd.read_csv('./US_Unemployment_Oct2012.csv')
unemployment.head(5)
Out[8]:
Now we need to create a function that maps one value to a RGB color (of the form #RRGGBB
).
In [9]:
from matplotlib.cm import YlGn
data_min = unemployment.Unemployment.min()
data_max = unemployment.Unemployment.max()
def color_function(x):
y = (x-data_min)/(data_max-data_min) # We transform x to a value between 0 and 1.
color_tuple = YlGn(y, bytes=True)[:3] # We convert ot to a tuple of colors.
return "#%02x%02x%02x" % color_tuple # We convert it to a color string.
We need also to convert the table into a dictionnary, in order to map a feature to it's unemployment value.
In [10]:
unemployment_dict = unemployment.set_index('State')['Unemployment']
unemployment_dict['AL']
Out[10]:
Now we can do the choropleth.
In [11]:
import numpy as np
In [12]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(
geo_json_data,
style_function=lambda feature: {
'fillColor': color_function(unemployment_dict[feature['id']]),
'color' : 'black',
'weight' : 1,
'dashArray' : '5, 5',
'fillOpacity' : .9,
}
).add_to(m)
m
Out[12]:
Of course, if you can create and/or use a dictionnary providing directly the good color. Thus, the finishing seems faster:
In [13]:
color_dict = {key: color_function(unemployment_dict[key]) for key in unemployment_dict.keys()}
In [14]:
m = folium.Map([43,-100], zoom_start=4)
folium.GeoJson(
geo_json_data,
style_function=lambda feature: {
'fillColor': color_dict[feature['id']],
'color' : 'black',
'weight' : 1,
'dashArray' : '5, 5',
'fillOpacity' : .9,
}
).add_to(m)
m
Out[14]:
Note that adding a color legend may be a good idea.
In [15]:
folium.ColorScale(
np.linspace(data_min, data_max,6).tolist(),
'YlGn',
'Unemployment color scale').add_to(m)
m
Out[15]:
In [16]:
m = folium.Map([43,-100], zoom_start=4)
m.choropleth(
geo_path='us-states.json',
fill_color='red',
fill_opacity=0.3,
line_weight=2,
)
m
Out[16]:
Or in providing a GeoJSON string (geo_str
) :
In [17]:
m = folium.Map([43,-100], zoom_start=4)
m.choropleth(geo_str=open('us-states.json').read())
m
Out[17]:
Then, in playing with keyword arguments, you can get a choropleth in (almost) one line :
In [18]:
m = folium.Map([43,-100], zoom_start=4)
m.choropleth(
geo_str=open('us-states.json').read(),
data=unemployment,
columns=['State', 'Unemployment'],
key_on='feature.id',
fill_color='YlGn',
)
m
Out[18]:
A cool thing: you can force the color scale to a given number of bins, in providing a threshold_scale
argument.
In [19]:
m = folium.Map([43,-100], zoom_start=4)
m.choropleth(
geo_str=open('us-states.json').read(),
data=unemployment,
columns=['State', 'Unemployment'],
key_on='feature.id',
fill_color='YlGn',
threshold_scale = [3,4,9,10]
)
m
Out[19]:
That's all folks !
Hope it'll be useful to you. Don't hesitate to provide a feedback on what can be improved, which method do you prefer, etc.