def get_features_as_geojson(layer, bbox=None, union=False):
""" Get features in this layer and return as GeoJSON """
if bbox is not None:
layer.SetSpatialFilterRect(bbox[0], bbox[3], bbox[2], bbox[1])
poly = ogr.Geometry(ogr.wkbPolygon)
if union:
for feature in layer:
geom = feature.GetGeometryRef()
# required for ogr2
if hasattr(geom, 'GetLinearGeometry'):
geom = geom.GetLinearGeometry()
poly = poly.Union(geom)
if bbox is not None:
wkt = "POLYGON ((%s %s, %s %s, %s %s, %s %s, %s %s))" % \
(bbox[0], bbox[1], bbox[2], bbox[1], bbox[2], bbox[3], bbox[0], bbox[3], bbox[0], bbox[1])
bbox_wkt = ogr.CreateGeometryFromWkt(wkt)
poly = poly.Intersection(bbox_wkt)
return json.loads(poly.ExportToJson())
评论列表
文章目录