1. Sean Gillies
  2. capgrid-export

Source

capgrid-export / capgrid-shapes.py

from pleiades.capgrids import data, Map
from osgeo import ogr
from shapefile import PolyShp

shp = PolyShp('capgrids.shp', 'w')
shp.add_fields({
    'map': {'type': 'string'}, 
    'grid': {'type': 'string'}, 
    'scale': {'type': 'string'}}
    )

for item in data.values():
    for rec in item:
        try:
            m = Map(rec)
        except:
            import pdb; pdb.set_trace()
            raise
        for g in m.values():
            r = ogr.Geometry(ogr.wkbLinearRing)
            for x, y in g.coordinates[0]:
                r.AddPoint(x, y)
            r.CloseRings()
            p = ogr.Geometry(ogr.wkbPolygon)
            p.AddGeometry(r)
            shp.add_geometry(p, 
                {'map': str(g.mapid), 
                 'grid': str(g.gridsquare), 
                 'scale': m.scale}
                )