Created
March 11, 2012 06:17
-
-
Save sgillies/2015267 to your computer and use it in GitHub Desktop.
Mapping and reducing
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
# 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