Last active
April 11, 2016 04:12
-
-
Save jwass/10213833 to your computer and use it in GitHub Desktop.
Demonstrate using GeoPandas and geojsonio.py (http://github.com/jwass/geojsonio.py) to view styled data in a few lines. Running the script will open a browser with the final URL: http://geojson.io/#id=gist:/10218442
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import geopandas as gpd | |
import geojsonio | |
# 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 | |
# 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 | |
# Load up the OSM data | |
df = gpd.read_file('cambridge_raw_osm.geojson') | |
# 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. Because the merge() results in a DataFrame and not | |
# a GeoDataFrame, we must re-create the GeoDataFrame. Hopefully this can | |
# be fixed in the future. | |
df_styled = gpd.GeoDataFrame(df.merge(style, on='highway')) | |
# Shoot the output to geojson.io and open in a browser. | |
url = geojsonio.display(df_styled.to_json(na='drop')) | |
print url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Visit http://geojson.io/#id=gist:/10218442 for the final, styled output.