[gdal-dev] How to set BoundingBox within a shapefile using OGR Pythons bindings

Frank Warmerdam warmerdam at pobox.com
Wed Dec 16 13:43:58 EST 2009


Daniele Romagnoli wrote:
> Hi list,
> I'm creating a shapefile using the OGR python bindings. (from  the 1.6.1 
> installer)
> I have some issues in creating the boundingBox for it.
> This is a snippet of the code I'm using:
> -----------------------------------------------------
> from osgeo import ogr
> from osgeo import osr
> ....
> ....
> northings_llc = dd['northings_llc']
> northings_urc = dd['northings_urc']
> eastings_llc = dd['eastings_llc']
> eastings_urc = dd['eastings_urc']
> t_srs = osr.SpatialReference()
> t_srs.SetFromUserInput('EPSG:32632')
> drv = ogr.GetDriverByName('ESRI Shapefile')
> ds = drv.CreateDataSource(fileout)
> x0, y0, x1, y1 = str(eastings_llc[0][0]), str(northings_llc[0][0]), 
> str(eastings_urc[0][0]), str(northings_urc[0][0])
> 
> layer = ds.CreateLayer(ds.GetName(), geom_type = ogr.wkbPolygon, srs = 
> t_srs)
> geom = ogr.Geometry(type = layer.GetLayerDefn().GetGeomType())
> geom.AssignSpatialReference(t_srs)
> wkt = 'POLYGON('+x0+','+y0+','+ x0+','+ y1+','+ x1+',' +y1+','+ x1+',' 
> +y0+','+ x0+',' +y0+')'

Daniele,

You are not formatting your WKT right. There should be spaces between
x and y.  The commas are only between tuples.

eg.

POLYGON ((479819.84375 4765180.5,479690.1875 4765259.5,...

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the gdal-dev mailing list