我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用osgeo.gdal.UseExceptions()。
def rasterize_polygons(polygon_shp, template_raster, out_raster, field): """ generate a categorical raster based on polygons :rtype: None :param polygon_shp: input polygon shapefile :param template_raster: raster template for cellsize and extent :param out_raster: output raster file """ # Source: https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html gdal.UseExceptions() # Get template raster specs src_ds = gdal.Open(template_raster) if src_ds is None: print 'Unable to open %s' % template_raster sys.exit(1) try: srcband = src_ds.GetRasterBand(1) except RuntimeError, e: print 'No band %i found' % 0 print e sys.exit(1) # Open the data source and read in the extent source_ds = ogr.Open(polygon_shp) source_layer = source_ds.GetLayer() target_ds = gdal.GetDriverByName('GTiff').Create(out_raster, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Float32) target_ds.SetGeoTransform(src_ds.GetGeoTransform()) target_ds.SetProjection(src_ds.GetProjection()) band = target_ds.GetRasterBand(1) band.SetNoDataValue(srcband.GetNoDataValue()) # Rasterize gdal.RasterizeLayer(target_ds, [1], source_layer, options=["ATTRIBUTE={}".format(field)])
def __init__(self): gdal.UseExceptions() pass