Hi,

gdal_merge.py doesn't handle tiff files with alpha masks so I've added this functionality.

My patch changes the behaviour of gdal_merge as follows:
- If a band is recognized as Alpha Band it is automatically treated as having a NODATA value of 0. I'm not sure if this is the way to go. But it makes some sense, as the resulting alpha channel is a correct alpha merge, independent of the nodata value used for the other bands. - If a band has mask flag GMF_ALPHA set, the mask band controls the parts that get written to the destination

Would be great if someone could review and possibly checkin the patch.

Regards
    Stefan



--- /home/stefan/src/gdal-1.7/swig/python/scripts/gdal_merge.py	2010-06-29 12:50:04.000000000 +0200
+++ gdal_merge_with_alpha.py	2010-06-29 16:46:51.000000000 +0200
@@ -27,9 +27,11 @@
 
 try:
     from osgeo import gdal
+    from osgeo import gdalconst
     gdal.TermProgress = gdal.TermProgress_nocb
 except ImportError:
     import gdal
+    import gdalconst
 
 import sys
 import glob
@@ -44,6 +46,15 @@
                  t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n,
                  nodata=None ):
 
+    s_band = s_fh.GetRasterBand( s_band_n )
+    if s_band.GetMaskFlags() & gdalconst.GMF_ALPHA:
+        return raster_copy_with_mask(
+            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 )
+
+    elif s_band.GetColorInterpretation() == gdalconst.GCI_AlphaBand:
+        nodata = 0
+
     if nodata is not None:
         return raster_copy_with_nodata(
             s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n,
@@ -55,7 +66,7 @@
               % (s_xoff, s_yoff, s_xsize, s_ysize,
              t_xoff, t_yoff, t_xsize, t_ysize ))
 
-    s_band = s_fh.GetRasterBand( s_band_n )
+    
     t_band = t_fh.GetRasterBand( t_band_n )
 
     data = s_band.ReadRaster( s_xoff, s_yoff, s_xsize, s_ysize,
@@ -93,6 +104,36 @@
     t_band.WriteArray( to_write, t_xoff, t_yoff )
 
     return 0
+
+# =============================================================================
+def raster_copy_with_mask( 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 ):
+    try:
+        import numpy as Numeric
+    except ImportError:
+        import Numeric
+    
+    if verbose != 0:
+        print('Copy %d,%d,%d,%d to %d,%d,%d,%d.' \
+              % (s_xoff, s_yoff, s_xsize, s_ysize,
+             t_xoff, t_yoff, t_xsize, t_ysize ))
+
+    s_band = s_fh.GetRasterBand( s_band_n )
+    m_band = s_band.GetMaskBand()
+    t_band = t_fh.GetRasterBand( t_band_n )
+
+    data_src = s_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize,
+                                   t_xsize, t_ysize )
+    data_mask = m_band.ReadAsArray( s_xoff, s_yoff, s_xsize, s_ysize,
+                                   t_xsize, t_ysize )
+    data_dst = t_band.ReadAsArray( t_xoff, t_yoff, t_xsize, t_ysize )
+
+    mask_test = Numeric.equal(data_mask, 0)
+    to_write = Numeric.choose( mask_test, (data_src, data_dst) )
+                               
+    t_band.WriteArray( to_write, t_xoff, t_yoff )
+
+    return 0
     
 # =============================================================================
 def names_to_fileinfos( names ):
_______________________________________________
gdal-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to