1

I am trying to merge two rasters (geotiffs) in QGIS.

One raster is the original & the other is clipped to a specific region of the original raster and 0 applied to null area to make transparent. See pictures below.

Original:

original raster

clipped:

clipped raster

Both together, clipped on top:

both rasters

I want to merge the clipped raster on top of the original raster to make one file where the original raster is visible in the null (transparent) area of the clipped raster.

Depending on the order I list the files in the gdal command generated by merge function in QGIS I get a merge result where you cannot see the clipped raster or I get a merge result where the original raster is not visible in the null area of the clipped raster.

result 1 result 2

What do I need to do to have the clipped raster merge on top of the original raster and be able to see the original raster in the null area of the clipped raster?

Some of the questions I have looked through that seemed related:

Which value does raster merge chose in overlapping area?

How to merge two or more rasters which overlap?

Merging raster in QGIS?


Per request of 1st responder here is the gdal merge command generated in qgis:

gdal_merge.py -n 0 -a_nodata 0 -of GTiff -o /home/qgis/Desktop/merge1.tif /home/qgis/Desktop/41_36_1.tif /home/qgis/Desktop/41_36_2.tif

I have tried with the files in both orders.


Here's a link to download the files I am working on.

https://drive.google.com/open?id=17wyRKbkDE1twWBSMDsY2upY7cuoHmOq1

  • You can use gdalwarp gdal.org/gdalwarp.html. Usage: gdalwarp clipped.tif original.tif. But there might be an option to improve the automatically generated gdal command to handle nodata correctly if you add the command into your question. – user30184 May 2 at 18:06
  • @user30184 , I did this in terminal: sudo gdalwarp clipped.tif original.tif merge.tif and again with the clipped.tif as the second file listed both produced a new file in which the clipped raster is not visible. Do I need to add something? I also added the generated gdal command in the merge tool from qgis to my question. – vtagis May 2 at 19:35
  • You can gdalwarp and update the target file directly just as I wrote the command and as documented Mosaicing into an existing output file is supported if the output file already exists. – user30184 May 2 at 20:34
  • I think that your command shoud work. Check with gdalinfo that both 41_36_1.tif and 41_36_2.tif really have 0 as nodata. – user30184 May 2 at 20:40
  • I promise you it does not. I am aware your command works as stated, but I wanted to keep the originals unchanged to try different combinations and options. I also checked with gdalinfo and can confirm no data is indeed "0". – vtagis May 2 at 20:52
0

As you see this is trickier than it seems. When you used the option in QGIS to set value 0 to transparent, you were "fooling" yourself into thinking that the 0 values were indeed "null". But 0 is not null. So when you merge the rasters you do not get the second raster to fill in the 0 regions in the first.

The correct way to do this is to use gdalwarp to set 0 values to null. Then those areas will be transparent without having to resort to the display tools in QGIS. Since it seems that you have a three band RGB raster, you have to set the nodata for all bands. So the commands should be:

gdalwarp -overwrite -srcnodata "0,0,0" clipped.tif clipped_nodata.tif

then

gdal_merge -o merged.tif original.tif clipped_nodata.tif

HTH

  • Between your response and the response from user 30184, i think I have merged the files correctly, but the color changes I made to the clipped raster are lost during merge. Thus to my eyes, I thought the merge was unsuccessful. In fact I have another problem, my color changes for clipped raster is being lost in merge. This is the only thing I can think of. Thanks for your help. – vtagis May 4 at 1:30

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.