[Gdal-dev] gdal_merge.py

Frank Warmerdam fwarmerdam at gmail.com
Mon Mar 7 10:20:53 EST 2005

On Sun,  6 Mar 2005 17:10:43 -0600, Xiaodong Zhang <zhang at aero.und.edu> wrote:
> Hi,
> I'm using gdal_merge.py to merge 3 gtiff images (MODIS) with exactly the same
> spatial extents but each only cover different parts of the area. Each input
> image (byte) has areas of no data of values 0.
> gdal_merge.py -o out.tif -pct -v -co compress=packbits input1.tif input2.tif
> input3.tif
> I had expected the final image should be the "merge" of the three, taking data
> from three images. But instead, the final image has the same data cover as the
> last input image, input3.tif.
> I also tried -n nodata_value (as below), but it gave me an error message,
> ImportError: No module named Numeric


At the risk of stating the obvious, if you are building your own GDAL,
you should install the Python Numeric package (http://numpy.sf.net I think)
if you want full capabilities. 

> gdal_merge.py -o out.tif -pct -v -n 0 -co compress=packbits input1.tif
> input2.tif input3.tif
> By the way, what is the algorithm used by gdal_merge.py? Is it possible to
> specify an algorithm, say, take the maximum.

Normally gdal_merge.py just blats layer after layer overtop whatever it
has so far.  If you install Numeric I think you can accomplish what you
want using the -n 0 argument. 

The gdal_merge.py script is just a script, so you can easily modify it
yourself.  If you look at the raster_copy_with_nodata() method you will
see how it uses Numeric to chose what value to use.  If you study up
on Numeric (numpy) you should be able to modify this to do any number
of things, including taking the maximum of input pixel values. 

I would add that the FWTools builds of GDAL include Numeric. 

Finally, you can also accomplish your task using gdalwarp.  If your
images already "know" their nodata value it should be sufficient to
do something like:

gdalwarp src1.tif out.tif
gdalwarp src2.tif out.tif
gdalwarp src3.tif out.tif

If the nodata value isn't already well known in the source files, you will
need to specify it:

gdalwarp src1.tif out.tif
gdalwarp -srcnodata 0 src2.tif out.tif
gdalwarp -srcnodata 0 src3.tif out.tif

Finally, be wary of outputing to compressed TIFF files when doing
"update in place" opeations such as gdal_merge.py does.  This may
cause problems - especially for images too large to cache all in ram.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the Gdal-dev mailing list