In [1]:
import pysal as ps
import geojson as gj
import folium_mapping as fm
First, we need to convert the data into a JSON format. JSON, short for "Javascript Serialized Object Notation," is a simple and effective way to represent objects in a digital environment. For geographic information, the GeoJSON standard defines how to represent geographic information in JSON format. Python programmers may be more comfortable thinking of JSON data as something akin to a standard Python dictionary.
In [2]:
filepath = ps.examples.get_path('south.shp')[:-4]
In [3]:
shp = ps.open(filepath + '.shp')
dbf = ps.open(filepath + '.dbf')
In [4]:
js = fm.build_features(shp, dbf)
Just to show, this constructs a dictionary with the following keys:
In [5]:
js.keys()
Out[5]:
In [6]:
js.type
Out[6]:
In [7]:
js.bbox
Out[7]:
In [8]:
js.features[0]
Out[8]:
Then, we write the json to a file.
In [9]:
with open('./example.json', 'w') as out:
gj.dump(js, out)
Let's look at the columns that we're going to map.
In [11]:
js.features[0].properties.keys()[0:5]
Out[11]:
We can map these attributes by calling them as commands to the choropleth mapping function.
Here's the simplest mapping command:
In [12]:
fm.choropleth_map('./example.json', 'FIPS', 'HR90')
Out[12]:
This produces a map using default classifications and color schemes and saves it to an html file. We set the function to have sane defaults. However, if the user wants to have more control, we have many options available.
In [13]:
fm.choropleth_map('./example.json', 'FIPS', 'HR90', classification = 'Quantiles')
Out[13]:
Most PySAL
classifiers are supprorted.
In [15]:
fm.choropleth_map('./example.json', 'FIPS', 'HR90', classification = 'Jenks Caspall', tiles='Stamen Toner', save=True)
Out[15]:
We support the entire range of builtin basemap types in Folium, but custom tilesets from MapBox are not supported yet.
In [16]:
fm.choropleth_map('./example.json', 'FIPS', 'HR80', classification = 'Jenks Caspall', tiles='Stamen Toner', fill_color = 'PuBuGn', save=True)
Out[16]:
All color schemes are Color Brewer and simply pass through to Folium
on execution.
In [18]:
fm.choropleth_map('./example.json', 'FIPS', 'HR80', classification = 'Equal Interval', classes=6, tiles='Stamen Toner', fill_color='PuBuGn',save=True)
Out[18]:
Folium supports up to 6 classes.
In [19]:
fm.choropleth_map('./example.json', 'FIPS', 'HR90', classification = 'Quantiles', std='HR80' , tiles='Stamen Toner', fill_color='PuBuGn',save=True)
Out[19]:
The goal of the std
argument was to implement a very flexible transformation argument that could take functions, fields, and static items. As of now, it only supports float/int scaling and division by another field, which would support the mapping of spatial rates or population rates.
Overall, the function neatly encapsulates three components:
Due to some pretty strong limitations in Folium
itself, its usefulness for rich, dynamic, and interactive visualizations built from small, atomic functions is probably limited. It would need to be extended quite significantly (or linked with even more packages with more fickle APIs) to usefully generate mixed-mode scientific visualizations. With Folium
's natural linkages to Pandas
, mapping derived quantites is also possible.
But, it's really simple to map quickly from PySAL:
In [22]:
with open('example2.json', 'w') as out:
gj.dump(fm.build_features(ps.open(filepath + '.shp'), ps.open(filepath + '.dbf')), out)
fm.choropleth_map('example2.json', 'FIPS', 'HR70')
Out[22]: