def bbox(coordinates, crs, outname=None, format='ESRI Shapefile', overwrite=True):
"""
create a bounding box vector object or shapefile from coordinates and coordinate reference system
coordinates must be provided in a dictionary containing numerical variables with names 'xmin', 'xmax', 'ymin' and 'ymax'
the coordinate reference system can be in either WKT, EPSG or PROJ4 format
"""
srs = crsConvert(crs, 'osr')
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(coordinates['xmin'], coordinates['ymin'])
ring.AddPoint(coordinates['xmin'], coordinates['ymax'])
ring.AddPoint(coordinates['xmax'], coordinates['ymax'])
ring.AddPoint(coordinates['xmax'], coordinates['ymin'])
ring.CloseRings()
geom = ogr.Geometry(ogr.wkbPolygon)
geom.AddGeometry(ring)
geom.FlattenTo2D()
bbox = Vector(driver='Memory')
bbox.addlayer('bbox', srs, ogr.wkbPolygon)
bbox.addfield('id', width=4)
bbox.addfeature(geom, 'id', 1)
geom.Destroy()
if outname is None:
return bbox
else:
bbox.write(outname, format, overwrite)
评论列表
文章目录