In [ ]:
%matplotlib inline
import os
from IPython.core.display import Image
import numpy as np
import matplotlib.pyplot as plt
import fiona
import mapnik
import shapely.geometry
## Fiona, mapnik demo
## Live 8.5 * darkblue-b
##
## based on UoLPythonGroup/data-hack-0
In [ ]:
BASE_FOLDER = '/home/user/data/north_carolina/shape'
In [ ]:
c_shapefile = os.path.join(BASE_FOLDER, 'nc_state.shp')
f = fiona.open(c_shapefile)
shps = list(f)
print 'f: ',type(f)
print f.schema
In [ ]:
type(f[2])
In [ ]:
## Use the Shapely geometry classes
## instantiate a Polygon from the Fiona collection
In [ ]:
import shapely.geometry
geo = shapely.geometry.shape(f[0]['geometry'])
geo
In [ ]:
## plt.plot(*geo.xy) ## hmm not implemented
In [ ]:
## Mapnik
## load the Mapnik python interfaces
## read the shapefile directly with Mapnik libs
## use the IPython Image interface as the drawing target
In [ ]:
def show_mapnik(m):
"""Returns an IPython Image of the rendered map."""
im = mapnik.Image(m.width, m.height)
mapnik.render(m, im)
return Image(data=im.tostring('png32'))
In [ ]:
import mapnik
m = mapnik.Map(600, 600)
layer = mapnik.Layer('contour')
layer.datasource = mapnik.Shapefile(file=c_shapefile)
style = mapnik.Style()
rule = mapnik.Rule()
line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('green'),0.4)
rule.symbols.append(line_symbolizer)
m.layers.append(layer)
style.rules.append(rule)
m.append_style('My Style', style)
layer.styles.append('My Style')
m.layers.append(layer)
m.zoom_all()
show_mapnik(m)
In [ ]: