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)
def reproject(self, projection): srs_out = crsConvert(projection, "osr") # create the CoordinateTransformation coordTrans = osr.CoordinateTransformation(self.srs, srs_out) layername = self.layername geomType = self.geomType features = self.getfeatures() self.__init__() self.addlayer(layername, srs_out, geomType) for feature in features: geom = feature.GetGeometryRef() geom.Transform(coordTrans) newfeature = feature.Clone() newfeature.SetGeometry(geom) self.layer.CreateFeature(newfeature) newfeature.Destroy() self.init_features()
def getProjection(self, type): """ type can be either "epsg", "wkt", "proj4" or "osr" """ return crsConvert(self.layer.GetSpatialRef(), type)