Index: gdal_merge.py =================================================================== --- gdal_merge.py (revision 21833) +++ gdal_merge.py (working copy) @@ -48,13 +48,13 @@ # ============================================================================= def raster_copy( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, - nodata=None ): + nodata=None, a_nodata=None ): if nodata is not None: return raster_copy_with_nodata( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, - nodata ) + nodata, a_nodata ) if verbose != 0: print('Copy %d,%d,%d,%d to %d,%d,%d,%d.' \ @@ -66,6 +66,10 @@ data = s_band.ReadRaster( s_xoff, s_yoff, s_xsize, s_ysize, t_xsize, t_ysize, t_band.DataType ) + + if a_nodata is not None: + t_band.SetNoDataValue(a_nodata) + t_band.WriteRaster( t_xoff, t_yoff, t_xsize, t_ysize, data, t_xsize, t_ysize, t_band.DataType ) @@ -75,7 +79,7 @@ # ============================================================================= def raster_copy_with_nodata( s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, - nodata ): + nodata, a_nodata ): try: import numpy as Numeric except ImportError: @@ -95,6 +99,9 @@ nodata_test = Numeric.equal(data_src,nodata) to_write = Numeric.choose( nodata_test, (data_src, data_dst) ) + + if a_nodata is not None: + t_band.SetNoDataValue(a_nodata) t_band.WriteArray( to_write, t_xoff, t_yoff ) @@ -164,7 +171,7 @@ print('UL:(%f,%f) LR:(%f,%f)' \ % (self.ulx,self.uly,self.lrx,self.lry)) - def copy_into( self, t_fh, s_band = 1, t_band = 1, nodata_arg=None ): + def copy_into( self, t_fh, s_band = 1, t_band = 1, nodata_arg=None, a_nodata_arg=None ): """ Copy this files image into target file. @@ -234,14 +241,15 @@ return \ raster_copy( s_fh, sw_xoff, sw_yoff, sw_xsize, sw_ysize, s_band, t_fh, tw_xoff, tw_yoff, tw_xsize, tw_ysize, t_band, - nodata_arg ) + nodata_arg, a_nodata_arg ) # ============================================================================= def Usage(): print('Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]*') print(' [-ps pixelsize_x pixelsize_y] [-tap] [-separate] [-q] [-v] [-pct]') - print(' [-ul_lr ulx uly lrx lry] [-n nodata_value] [-init "value [value...]"]') + print(' [-ul_lr ulx uly lrx lry] [-init "value [value...]"]') + print(' [-n nodata_value] [-a_nodata output_nodata_value]') print(' [-ot datatype] [-createonly] input_files') print(' [--help-general]') print('') @@ -265,6 +273,7 @@ separate = 0 copy_pct = 0 nodata = None + a_nodata = None create_options = [] pre_init = [] band_type = None @@ -322,6 +331,10 @@ i = i + 1 nodata = float(argv[i]) + elif arg == '-a_nodata': + i = i + 1 + a_nodata = float(argv[i]) + elif arg == '-f': # for backward compatibility. i = i + 1 @@ -473,9 +486,9 @@ if separate == 0 : for band in range(1, bands+1): - fi.copy_into( t_fh, band, band, nodata ) + fi.copy_into( t_fh, band, band, nodata, a_nodata ) else: - fi.copy_into( t_fh, 1, t_band, nodata ) + fi.copy_into( t_fh, 1, t_band, nodata, a_nodata ) t_band = t_band+1 fi_processed = fi_processed+1