--- gdal_merge_trunk.py +++ gdal_merge.py @@ -33,7 +33,10 @@ import sys import glob +__version__ = '$id$'[5:-1] verbose = 0 +progress_prefix = None +progress_len = 0 # ============================================================================= @@ -233,21 +236,37 @@ print 'Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]*' print ' [-ps pixelsize_x pixelsize_y] [-separate] [-v] [-pct]' print ' [-ul_lr ulx uly lrx lry] [-n nodata_value] [-init value]' - print ' [-ot datatype] [-createonly] input_files' + print ' [-ot datatype] [-createonly] [-progress] input_files' print ' [--help-general]' print # ============================================================================= +def progress (msg='', total=''): + """With arguments it displays progress, without it erases last progress string""" + global progress_prefix, progress_len + if progress_prefix != None: + sys.stderr.write (' '*progress_len + '\r') + msg = str (msg) + total = str (total) + if msg: + if total: msg = '%s%s of %s' % (progress_prefix, msg, total) + else: msg = '%s%s' % (progress_prefix, msg) + progress_len = len (msg) + sys.stderr.write (msg + '\r') + sys.stderr.flush () + +# ============================================================================= # # Program mainline. # -if __name__ == '__main__': - +def main( argv=None ): names = [] format = 'GTiff' out_file = 'out.tif' + global verbose, progress_prefix + verbose = False # Really needed here too! ulx = None psize_x = None separate = 0 @@ -259,7 +278,9 @@ createonly = 0 gdal.AllRegister() - argv = gdal.GeneralCmdLineProcessor( sys.argv ) + if argv is None: + argv = sys.argv + argv = gdal.GeneralCmdLineProcessor( argv ) if argv is None: sys.exit( 0 ) @@ -327,6 +348,10 @@ lry = float(argv[i+4]) i = i + 4 + elif arg == '-progress': + if progress_prefix == None: + progress_prefix = 'gdal_merge: ' + elif arg[:1] == '-': print 'Unrecognised command option: ', arg Usage() @@ -356,6 +381,7 @@ sys.exit( 1 ) # Collect information on all the source files. + progress ('preparing...') file_infos = names_to_fileinfos( names ) if ulx is None: @@ -421,7 +447,9 @@ # Copy data from source files into output file. t_band = 1 - for fi in file_infos: + for i, fi in enumerate( file_infos ): + progress( i+1, len( file_infos )) + if createonly != 0: continue @@ -437,4 +465,11 @@ t_band = t_band+1 # Force file to be closed. + progress ('saving...') t_fh = None + progress () + progress_prefix = None # Really needed here! + + +if __name__ == '__main__': + sys.exit(main())