How do I merge black and white rasters into one file using QGIS?

Also, rasters are overlaying so I´m interested into preserving only black colour.

  • 1
    Welcome to GIS SE. At the moment you question is a little limited. A simple answer would be in QGIS you would use the 'Raster > Raster Calculator' to merge two layers together. Understand that the colour of the cells is only a representation that QGIS makes for a value. Could you update your question to include what values it is you are trying to preserve/using. EG. if black = 1 and white = 0, then you would simple add the two layers together. Check this link out learn more about the calculator: docs.qgis.org/2.2/en/docs/user_manual/working_with_raster/… – Knightshound Nov 2 '16 at 12:04
  • Thank you for you answer! Values I would like to preserve is as you described; 1= black and 0=white. I tried to add it but nothing came out as result... – QGIS user Nov 2 '16 at 13:28
  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Nov 3 '16 at 17:52

To add two rasters together, whilst preserving a value, as long as raster cell values are 1 or 0 is to multiply them in the raster calculator.

"raster1" * "raster2"

Multiplication works because: 1*1 = 1 and 1*0 = 0

Set the raster calculator to output the same crs as both your other layers, otherwise you could create a raster that ends up in a different location.

  • What if black should be preserved if either a or b has black pixels, or both? – user30184 Nov 2 '16 at 17:32
  • Not sure what you're fully asking. hypothetically, wouldn't a raster have all those examples/variations you suggested? this isn't my particular expertise but would you not try using a polygon to clip a particular area you wish to preserve. Or have I completely missed the point here. – Knightshound Nov 3 '16 at 8:10
  • 1
    If raster 1 has a line, let's say road, it has black pixels where the road is located. If raster 2 overlaps but does not contain that road I suppose that the resulting merged raster should have ones, not zeros because black=1? With your formula it would be white (0 * 1=0). But I do not know what is the aim of the exercise and I am just guessing. Perhaps a simple image merging by defining 0 as a nodata leads to good result. – user30184 Nov 3 '16 at 8:20
  • That's a good explanation. Using addition would certainly fix that problem, but would then also introduce a lot of new values, which would change the data. Just looked at the merge tool and your right, that would probably be best if zero was nodata. – Knightshound Nov 3 '16 at 8:39
  • Can you take max((raster1),(raster2)) with raster calculator? – user30184 Nov 3 '16 at 10:24

So as user30184 wrote - roads needs to be preserved (0*1=0) and this is wrong solution. I managed to solve the problem in GRASS GIS. I don't think it is possible to solve it in QGIS.

Merge tool don´t work since rasters overlap (white area than covers black).

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.