I am having a Merging issue with the GDAL python library. I am using gdal_merge.pylink

My issue is when I merge two rasters NoData value of one raster overlaps the other data resulting in not a proper raster image.

For example, in the raster images below (left images) if I merge these 4 images labeled as 1,2,3,4 I should get the merged raster image as the right-sided image.

enter image description here

enter image description here

But, the merged image I am getting looks like this (image below):

(Note: Image labeled 1,2 and 3 are merged properly. It's only when we merged them with image labeled 4, it's not properly merging.)

enter image description here

How do I resolve this issue?

1 Answer 1


If you are not using the -n parameter, nodata values will be included in your mosaic.

GDAL will ignore nodata values if you specify this and will mosaic data without overlapping nodata areas included.

Note (update from OP): np.nan may not work as a nodata value

For example:

gdal_merge.py -n 0 -o output.tif input1.tif input2.tif

You will need to know the nodata value and make sure it is the same in each image

  • Hi @NathanThomas, I am using -n parameter. import gdal_merge as gm , gm.main(['', '-o', outPath, '-of', 'GTiff', '-init', 'np.nan', '-n', 'np.nan', '-a_nodata', 'np.nan', 'input1.tif', 'input2.tif', 'input3.tif', 'input4.tif'])
    – terraCoder
    Sep 20, 2020 at 23:14
  • Is the nodata value in each image a nan value? Sep 21, 2020 at 0:48
  • Yes, All the images have nodata as nan value.
    – terraCoder
    Sep 21, 2020 at 1:15
  • This is strange. Can you include more of your code in the question? Sep 21, 2020 at 16:52
  • 1
    Looks like I have found the problem. It was not working properly becasue of NaN values. gdal_merge.py doesn't support the NaN values. I have change nodata from NaN (np.nan) to -9999 and it workds. Thanks for the help.
    – terraCoder
    Sep 22, 2020 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.