Created
April 17, 2013 00:50
-
-
Save carlohamalainen/5400913 to your computer and use it in GitHub Desktop.
Plot polygons for a bunch of shapes using basemap.
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
""" | |
Carlo Hamalainen <[email protected]> | |
Given a directory of shapefiles, e.g. | |
$ ls /path/to/shapefiles/ | |
Project_Name.dbf | |
Project_Name.prj | |
Project_Name.sbn | |
Project_Name.sbx | |
Project_Name.shp | |
Project_Name.shp.xml | |
Project_Name.shx | |
draw a polygon for each shape based on some attribute (here, whether the | |
'Status' is completed or in-progress. | |
""" | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.basemap import Basemap | |
from matplotlib.patches import Polygon | |
m = Basemap(llcrnrlon=130. ,llcrnrlat=-48, urcrnrlon=170. ,urcrnrlat=-10., resolution='h', area_thresh=10000) | |
m.drawcoastlines() | |
m.fillcontinents() | |
m.drawcountries(linewidth=2) | |
m.drawstates() | |
m.drawmapboundary(fill_color='aqua') | |
s = m.readshapefile('/path/to/shapefiles/Project_Name', 'Project_Name') | |
for xy, info in zip(m.Project_Name, m.Project_Name_info): | |
if info['Status'] == 'Completed': | |
poly = Polygon(xy, facecolor='red', alpha=0.4) | |
plt.gca().add_patch(poly) | |
elif info['Status'] == 'in progress': | |
poly = Polygon(xy, facecolor='green', alpha=0.4) | |
plt.gca().add_patch(poly) | |
else: | |
print info['Status'] | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment