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 to embed in the notebook.

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

Setup the style guide

Use colors from 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, qualitative, 7-class Set1 (removing the yellow color)
colors = """
widths = range(len(highway), 0, -1)  # Countdown so smallest is width 1
# 'stroke' and 'stroke-width' are the defined properties for line color/width:
style = pd.DataFrame({'stroke': colors,
                      'stroke-width': widths,
                      'highway': highway})

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'))


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

Embed the map right in the notebook here.

In [7]: