Clipping raster layer with *raster* mask layer in QGIS

The simple way to do this in QGIS is to use the Raster Calculator (Raster->Raster Calculator). You have a couple of options. The easiest to explain/understand is to make a unitary raster from your mask (all data set to either 1 or NoData) and then multiply your clip layer by the unitary mask layer.

To ensure the extents match the mask layer, in the raster calculator window select the mask layer in the 'Raster bands' list on the left and then click the 'Current layer extent' button on the right.

You can create a unitary mask on the fly by using a conditional statement (see the link) something like this:

(maskLayer@1 >= 0) * clipLayer@1

This statement basically says: treat everything in my mask layer that is not NoData as being equal to 1 (NoData stays as NoData). Just be sure to remember to set the extent (see above)!


Raster Calculator and paletted data:

Any output from the raster calculator will be just values and not carry over any information contained within a colour pallet. You have a couple of options to 'get your palette back':

  1. Go to the style tab of the original layer's properties and (making sure it is paletted), click on Save Style button at the bottom. You can then apply this same style to your new layer and if you have only clipped it as per the above instructions, it will appear the same as before. This is easy but your raster is not paletted in a persistent way outside of the QGIS project.
  2. To make the presentation persistent, you can right-click the layer and 'Save As'. Check the 'Rendered Image' radio button at the top of the save-dialog box. This will make a new raster with the exact colour scheme as your palatted layer BUT it will no longer be a single band raster but a four band RGBA raster.
  3. If you MUST have an actual stand-alone single-band raster with a persistent palette (and not RGBA), then I don't know of a way of doing this in QGIS but you could take the output of step 2 above and open it in GIMP (or Photoshop) and change the mode from RGB to Indexed Colour. But you will need to create a world file as saving the image in GIMP/Photoshop will destroy the georeferencing.