27 from __future__
import print_function
28 from osgeo
import gdal
30 import osgeo.gdalconst
as gdalc
31 from optparse
import OptionParser
37 prs = OptionParser(version=
"%prog $Revision: 4037 $",
38 usage=
"%prog -r <RASTER> -v <OVERVIEW>",
39 description=
"Dump GDAL raster overview to separate file, GeoTIF by default")
40 prs.add_option(
"-r",
"--raster", dest=
"raster", action=
"store", default=
None,
41 help=
"input raster file")
42 prs.add_option(
"-v",
"--overview", dest=
"overview", action=
"store", type=
"int", default=
None,
43 help=
"1-based index of raster overview, optional")
45 (opts, args) = prs.parse_args()
47 if opts.raster
is None:
48 prs.error(
"use option -r to specify input raster file that contains overviews")
54 src_ds = gdal.Open(opts.raster, gdalc.GA_ReadOnly);
56 sys.exit(
'ERROR: Cannot open input file: ' + opts.raster)
58 band = src_ds.GetRasterBand(1)
59 if opts.overview
is None:
61 nvend = band.GetOverviewCount()
63 nvstart = opts.overview - 1
66 for nv
in range(nvstart, nvend):
68 band = src_ds.GetRasterBand(1)
69 if nv < 0
and nv >= band.GetOverviewCount():
70 print(
"ERROR: Failed to find overview %d" % nv)
72 ov_band = band.GetOverview(nv)
74 ovf = int(0.5 + band.XSize / float(ov_band.XSize))
76 print(
"--- OVERVIEW #%d level = %d ---------------------------------------" % (nv + 1, ovf))
79 dst_file = os.path.basename(opts.raster) +
"_ov_" +
str(ovf) +
".tif"
81 dst_drv = gdal.GetDriverByName(dst_format)
82 dst_ds = dst_drv.Create(dst_file, ov_band.XSize, ov_band.YSize, src_ds.RasterCount, ov_band.DataType)
84 print(
"Source: " + opts.raster)
85 print(
"Target: " + dst_file)
86 print(
"Exporting %d bands of size %d x %d" % (src_ds.RasterCount, ov_band.XSize, ov_band.YSize))
92 for nb
in range(1, src_ds.RasterCount + 1):
94 band = src_ds.GetRasterBand(nb)
95 assert band
is not None
96 ov_band = band.GetOverview(nv)
97 assert ov_band
is not None
99 print(
" Band #%d (%s) (%d x %d)" % \
100 (nb, gdal.GetDataTypeName(ov_band.DataType), ov_band.XSize, ov_band.YSize))
102 dst_band = dst_ds.GetRasterBand(nb)
103 assert dst_band
is not None
104 data = ov_band.ReadRaster(0, 0, ov_band.XSize, ov_band.YSize)
105 assert data
is not None
106 dst_band.WriteRaster(0, 0, ov_band.XSize, ov_band.YSize, data, buf_type = ov_band.DataType)