geojson.io and IPython notebook

Quickly style and plot a GeoJSON file. Style the data based on the values in a column. Here, use OSM's 'highway' field to plot in different colors.

Use geojsonio.py to embed http://geojson.io in the notebook.

In [14]:
import geopandas as gpd
import geojsonio
import pandas as pd

Setup the style guide

Use colors from http://colorbrewer2.org. I'm using the qualitative, 7-class Set1 (and removed the yellow color since it doesn't work well for lines on this map).

In [15]:
# Set up the simple style spec mapping of highway to color and width
highway = ['motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'residential']
# Colors pasted from colorbrewer2.org, qualitative, 7-class Set1 (removing the yellow color)
colors = """
#e41a1c
#377eb8
#4daf4a
#984ea3
#ff7f00
#a65628
""".split()
widths = range(len(highway), 0, -1)  # Countdown so smallest is width 1
 
# 'stroke' and 'stroke-width' are the defined properties for line color/width:
# http://github.com/mapbox/simplestyle-spec
style = pd.DataFrame({'stroke': colors,
                      'stroke-width': widths,
                      'highway': highway})

style
Out[15]:
highway stroke stroke-width
0 motorway #e41a1c 6
1 trunk #377eb8 5
2 primary #4daf4a 4
3 secondary #984ea3 3
4 tertiary #ff7f00 2
5 residential #a65628 1

Style each geometry

Merge the style columns to the data, merge on the highway column. Because this is an inner join by default, it will drop any rows where highway is not in the style DataFrame.

In [10]:
# Load up the OSM data
df = gpd.read_file('cambridge_raw_osm.geojson')

df_styled = gpd.GeoDataFrame(df.merge(style, on='highway'))

Visualize

Now each row contains 'stroke' and 'stroke-width' based on the 'highway' tag.

Embed the http://geojson.io map right in the notebook here.

In [7]:
geojsonio.embed(df_styled.to_json(na='drop'))
Out[7]: