import sys
import ogr
from shapely.wkb import loads
from descartes import PolygonPatch
from matplotlib import pyplot
ds = ogr.Open( "/path/to/boundary/file.shp" )
if ds is None:
print "Open failed.\n"
sys.exit( 1 )
lyr = ds.GetLayer(0)
lyr.ResetReading()
feat = lyr.GetNextFeature()
while feat is not None:
geom = feat.GetGeometryRef()
if geom is None or geom.GetGeometryType() != ogr.wkbPolygon:
print "no poly geometry\n"
else:
# create matplotlib figure:
fig = pyplot.figure(1, figsize = [10,10], dpi = 300) #create 10x10 figure
ax = fig.addsubplot(111) #Add the map frame (single plot)
# add polygon:
patch = PolygonPatch(loads(feature.GetGeometryRef().ExportToWkb()), plus colour and line considerations)
ax.addpatch(patch) # simply add the patch to the subplot
# set plot vars
ax.set_xlim(get xmin and xmax values from data)
ax.set_ylim(get ymin and ymax values from data)
ax.set_aspect(1)
# save as image
pyplot.savefig('somefile.png', some arguments you like)¶
feat = lyr.GetNextFeature()
ds.Destroy()
显然,您需要对此进行一些修复,以使其按照您想要的方式绘制,但一般方法应该是合理的。
I'm assuming you want a separate image for each electorate? If so, I would take the following approach using python:
Read the geometry using GDAL/OGR:
Install the GDAL/OGR tools and their python bindings. Download the ESRI shapefile for the electoral boundaries. Ensure you can read the polygon geometry using OGR:
import sys
import ogr
ds = ogr.Open( "/path/to/boundary/file.shp" )
if ds is None:
print "Open failed.\n"
sys.exit( 1 )
lyr = ds.GetLayer(0)
lyr.ResetReading()
feat = lyr.GetNextFeature()
while feat is not None:
geom = feat.GetGeometryRef()
if geom is None or geom.GetGeometryType() != ogr.wkbPolygon:
print "no poly geometry\n"
feat = lyr.GetNextFeature()
ds.Destroy()
Output the geometry using matplotlib via shapely, descartes
Install matplotlib, shapely and descartes. Modify the above script to load each polygon into matplob via shapely and descartes:
import sys
import ogr
from shapely.wkb import loads
from descartes import PolygonPatch
from matplotlib import pyplot
ds = ogr.Open( "/path/to/boundary/file.shp" )
if ds is None:
print "Open failed.\n"
sys.exit( 1 )
lyr = ds.GetLayer(0)
lyr.ResetReading()
feat = lyr.GetNextFeature()
while feat is not None:
geom = feat.GetGeometryRef()
if geom is None or geom.GetGeometryType() != ogr.wkbPolygon:
print "no poly geometry\n"
else:
# create matplotlib figure:
fig = pyplot.figure(1, figsize = [10,10], dpi = 300) #create 10x10 figure
ax = fig.addsubplot(111) #Add the map frame (single plot)
# add polygon:
patch = PolygonPatch(loads(feature.GetGeometryRef().ExportToWkb()), plus colour and line considerations)
ax.addpatch(patch) # simply add the patch to the subplot
# set plot vars
ax.set_xlim(get xmin and xmax values from data)
ax.set_ylim(get ymin and ymax values from data)
ax.set_aspect(1)
# save as image
pyplot.savefig('somefile.png', some arguments you like)¶
feat = lyr.GetNextFeature()
ds.Destroy()
Obviously you need to fix this up a bit to get it to draw how you want, but the general approach should be sound.
Download and use QGIS - www.qgis.org
This handy open source tool works well, and opens many typical formats natively (i.e. shape files, originally developed by ESRI) It also has a built-in OGR tool.
发布评论
评论(3)
我假设您想要为每个选民提供单独的图像?如果是这样,我将使用 python 采取以下方法:
使用 GDAL/OGR 读取几何图形:
安装 GDAL/OGR 工具 及其 python 绑定。下载选举边界的 ESRI shapefile。确保您可以使用 OGR 读取多边形几何图形:
通过 shapely、descartes 使用 matplotlib 输出几何图形
安装 matplotlib, < a href="http://trac.gispython.org/lab/wiki/Shapely" rel="nofollow noreferrer">shapely 和 笛卡尔。修改上面的脚本,通过 shapely 和 descartes 将每个多边形加载到 matplob 中:
显然,您需要对此进行一些修复,以使其按照您想要的方式绘制,但一般方法应该是合理的。
I'm assuming you want a separate image for each electorate? If so, I would take the following approach using python:
Read the geometry using GDAL/OGR:
Install the GDAL/OGR tools and their python bindings. Download the ESRI shapefile for the electoral boundaries. Ensure you can read the polygon geometry using OGR:
Output the geometry using matplotlib via shapely, descartes
Install matplotlib, shapely and descartes. Modify the above script to load each polygon into matplob via shapely and descartes:
Obviously you need to fix this up a bit to get it to draw how you want, but the general approach should be sound.
下载并使用 QGIS - www.qgis.org
这个方便的开源工具运行良好,可以本地打开许多典型格式(即形状文件,最初由 ESRI 开发)。它还有一个内置的 OGR 工具。
另外,它玩起来很有趣,而且易于使用。
Download and use QGIS - www.qgis.org
This handy open source tool works well, and opens many typical formats natively (i.e. shape files, originally developed by ESRI) It also has a built-in OGR tool.
Plus it's just fun to play with, and easy to use.
查看 FWTools。
如果您需要有关转换的帮助,还有一个有用的邮件列表。
Check out FWTools.
There is also a helpful mailing list if you need help on the conversions.