澳大利亚选举委员会提供免费的 ESRI 和 MapInfo 格式的澳大利亚选举边界 GIS 图层供下载。我想使用免费工具将此数据转换为缩略图多边形图像。
问问题
1792 次
3 回答
4
我假设您想要为每个选民提供单独的图像?如果是这样,我会使用 python 采取以下方法:
使用 GDAL/OGR 读取几何图形:
安装GDAL/OGR 工具及其python 绑定。下载选举边界的 ESRI shapefile。确保您可以使用 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()
使用 matplotlib 通过匀称地输出几何图形,笛卡尔
安装matplotlib、shapely和decartes。修改上面的脚本,通过 shapely 和 decartes 将每个多边形加载到 matplob 中:
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()
显然,您需要对其进行一些修复以使其绘制成您想要的样子,但一般方法应该是合理的。
于 2010-07-13T02:38:02.697 回答
2
下载和使用 QGIS - www.qgis.org 这个方便的开源工具运行良好,并且原生打开许多典型格式(即形状文件,最初由 ESRI 开发) 它还内置了 OGR 工具。
此外,它玩起来很有趣,而且易于使用。
于 2010-07-21T00:34:21.160 回答