27 from osgeo
import gdal
29 import osgeo.gdalconst
as gdalc
30 from optparse
import OptionParser
36 prs = OptionParser(version=
"%prog $Revision: 4037 $",
37 usage=
"%prog -r <RASTER> -v <OVERVIEW>",
38 description=
"Dump GDAL raster overview to separate file, GeoTIF by default")
39 prs.add_option(
"-r",
"--raster", dest=
"raster", action=
"store", default=
None,
40 help=
"input raster file")
41 prs.add_option(
"-v",
"--overview", dest=
"overview", action=
"store", type=
"int", default=
None,
42 help=
"1-based index of raster overview, optional")
44 (opts, args) = prs.parse_args()
46 if opts.raster
is None:
47 prs.error(
"use option -r to specify input raster file that contains overviews")
53 src_ds = gdal.Open(opts.raster, gdalc.GA_ReadOnly);
55 sys.exit(
'ERROR: Cannot open input file: ' + opts.raster)
57 band = src_ds.GetRasterBand(1)
58 if opts.overview
is None:
60 nvend = band.GetOverviewCount()
62 nvstart = opts.overview - 1
65 for nv
in range(nvstart, nvend):
67 band = src_ds.GetRasterBand(1)
68 if nv < 0
and nv >= band.GetOverviewCount():
69 print "ERROR: Failed to find overview %d" % nv
71 ov_band = band.GetOverview(nv)
73 ovf = int(0.5 + band.XSize / float(ov_band.XSize))
75 print "--- OVERVIEW #%d level = %d ---------------------------------------" % (nv + 1, ovf)
78 dst_file = os.path.basename(opts.raster) +
"_ov_" + str(ovf) +
".tif"
80 dst_drv = gdal.GetDriverByName(dst_format)
81 dst_ds = dst_drv.Create(dst_file, ov_band.XSize, ov_band.YSize, src_ds.RasterCount, ov_band.DataType)
83 print "Source: " + opts.raster
84 print "Target: " + dst_file
85 print "Exporting %d bands of size %d x %d" % (src_ds.RasterCount, ov_band.XSize, ov_band.YSize)
91 for nb
in range(1, src_ds.RasterCount + 1):
93 band = src_ds.GetRasterBand(nb)
94 assert band
is not None
95 ov_band = band.GetOverview(nv)
96 assert ov_band
is not None
98 print " Band #%d (%s) (%d x %d)" % \
99 (nb, gdal.GetDataTypeName(ov_band.DataType), ov_band.XSize, ov_band.YSize)
101 dst_band = dst_ds.GetRasterBand(nb)
102 assert dst_band
is not None
103 data = ov_band.ReadRaster(0, 0, ov_band.XSize, ov_band.YSize)
104 assert data
is not None
105 dst_band.WriteRaster(0, 0, ov_band.XSize, ov_band.YSize, data, buf_type = ov_band.DataType)