2010-07-12 61 views

回答

4

我假设你想为每个选民单独的图像?如果是的话,我会使用python采取如下方法:

阅读使用GDAL/OGR几何:

安装GDAL/OGR tools及其python bindings。下载选举界限的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几何,笛卡尔

安装matplotlibshapelydescartes。修改上面的脚本通过匀称和笛卡尔加载每个多边形到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() 

显然,你需要解决这个问题了一点得到它吸引你怎么想,但一般的做法应该是声。

2

下载和使用QGIS - www.qgis.org 这个方便的开源工具运行良好,并在本地打开了许多典型格式(即最初由ESRI开发的形状文件)。它也有一个内置的OGR工具。

加上它只是玩的乐趣,并易于使用。

相关问题