Inspired by Mike Bostock's Let's Make a Map, we want to make a map too using matta. We will display the communes of Santiago, Chile. To do that we will perform the following steps:
In [1]:
import matta
matta.init_javascript(path='https://rawgit.com/carnby/matta/master/matta/libs/')
Out[1]:
In [2]:
!rm -fr data
!mkdir data
!wget http://siit2.bcn.cl/obtienearchivo?id=repositorio/10221/10396/1/division_comunal.zip -O data/division_comunal.zip
In [3]:
!unzip data/division_comunal.zip -d data/
In [4]:
!ogrinfo data/division_comunal.shp 'division_comunal' | head -n 30
Now we use ogr2ogr to convert the shapefile into GeoJSON.
Notes:
santiago-comunas.json in case it exists (that is, when we re-run the notebook :) ).-clipdst option to specify a bounding box obtained in this site.-t_srs EPSG:4326-o option to convert the data coordinates to (longitude,latitude) pairs.
In [5]:
!rm data/santiago-comunas.json
!ogr2ogr -where "NOM_PROV IN ('Santiago', 'Maipo', 'Cordillera')" -f GeoJSON \
-clipdst -70.828155 -33.635036 -70.452573 -33.302953 -t_srs EPSG:4326-o \
data/santiago-comunas.json data/division_comunal.shp
In [6]:
!topojson -p --id-property NOM_COM -s 0 -o data/topojson-santiago-comunas.json data/santiago-comunas.json
In [7]:
import json
import unicodedata
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
with open('data/topojson-santiago-comunas.json', 'r') as f:
stgo = json.load(f)
for g in stgo['objects']['santiago-comunas']['geometries']:
g['id'] = strip_accents(g['id'].upper())
g['properties']['id'] = g['id']
In [8]:
stgo['objects']['santiago-comunas']['geometries'][7]
Out[8]:
In [9]:
from matta import topojson
topojson(geometry=stgo)
In [10]:
import requests
wikipage = requests.get('https://es.wikipedia.org/wiki/Anexo:Comunas_de_Santiago_de_Chile')
wikipage
Out[10]:
In [11]:
%load_ext autoreload
%autoreload 2
In [12]:
import pandas as pd
df = pd.read_html(wikipage.text, attrs={'class': 'sortable'}, header=0)[0]
df.head()
Out[12]:
Data is not clean. Fortunately, we just want the IDH column, which should be easy to convert to a meaningful float.
In [13]:
df['Comuna'] = [strip_accents(c).replace('?', '').upper() for c in df['Comuna']]
df['IDH'] = [float(c.split()[0].replace(',', '.')) for c in df['IDH?']]
del df['IDH?']
df.head()
Out[13]:
In [14]:
df.IDH.describe()
Out[14]:
We use seaborn to create a color palette.
In [15]:
%matplotlib inline
import seaborn as sns
palette = sns.color_palette("GnBu_d", 5)
sns.palplot(palette)
In [16]:
from matta.scales import threshold_scale
scale = threshold_scale(df.IDH, palette, extend_by=0.05)
scale
Out[16]:
In [17]:
topojson(geometry=stgo, area_dataframe=df, area_feature_name='Comuna', area_value='IDH', area_color_scale_domain=scale['domain'],
area_color_scale_range=scale['range'], area_color_scale_extent=scale['extent'], leaflet=False)
In [18]:
topojson(geometry=stgo, mark_dataframe=df, mark_feature_name='Comuna', mark_value='Pobreza?',
mark_color='indigo', mark_scale=0.5)
In [19]:
topojson(geometry=stgo, area_dataframe=df, area_feature_name='Comuna', area_value='IDH', area_color_scale_domain=scale['domain'],
area_color_scale_range=scale['range'], area_color_scale_extent=scale['extent'],
mark_dataframe=df, mark_feature_name='Comuna', mark_value='Pobreza?', mark_color='indigo', mark_scale=0.5,
mark_max_ratio=15, mark_min_ratio=0, mark_opacity=0.5, leaflet=True)
The mixture of both choropleth and symbol map does not make sense in our case. But surely you have a more interesting use case!
You can see an example of scaffolded visualizations using matta.topojson here.