Skip to content

Instantly share code, notes, and snippets.

@sgillies
Created March 11, 2012 06:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgillies/2015267 to your computer and use it in GitHub Desktop.
Save sgillies/2015267 to your computer and use it in GitHub Desktop.
Mapping and reducing
# Making maps with reduce()
from matplotlib import pyplot
from descartes import PolygonPatch
from fiona import collection
BLUE = '#6699cc'
def render(axes, rec):
"""Given matplotlib axes and a record, adds the record as a patch
and returns the axes so that reduce() can accumulate more
patches."""
axes.add_patch(
PolygonPatch(rec['geometry'], fc=BLUE, ec=BLUE, alpha=0.5, zorder=2))
return axes
# Set up the figure and axes.
figure = pyplot.figure(1, figsize=(6, 6), dpi=90)
axes = figure.add_subplot(111)
# Open a file and render every feature into the plot axes.
with collection("docs/data/test_uk.shp", "r") as source:
reduce(render, source, axes)
axes.autoscale(tight=True)
# We could also set limits explicitly:
# minx, miny, maxx, maxy = source.bounds
# axes.set_xlim(minx, maxx)
# axes.set_ylim(miny, maxy)
figure.savefig('with-descartes-functional.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment