This is my first question here. I have a big raster of the world's nighttime lights and want to clip it down to the Adm0 of a country:

Original raster and Adm0 mask vector layer

In order to do so, in QGIS 2.18 what I do is using the Clipper function and checking the option "Crop the extent of the target dataset o the extent of the cutline":

enter image description here

I expected to get a clipped raster with Nodata values for the pixels outside the boundaries of my mask vector. What I get instead is a zero value for those pixels:

Result of the clipping operation. Band 1 value refers to a pixel picked outside the boundaries of NGA_adm0

Apparently, the algorithm sets a zero value for all the pixels outside my Adm0 boundaries. Specifying "No data value" to zero is not an option, since some original pixels within the Adm0 boundaries actually have a value equal to zero, so I would miss some significant information.

I haven't had this problem with previous clipping operations on other raster layers.

I get the same problem in QGIS 3.2.

  • 2
    I think the problem is that the raster is 8 bit (0-255 values) so that limits your range and there is probably not a defined NoData value for your input raster, can you check your input data type and nodata value please. If this is the case your first image has a range of 0-20 so it should be safe to set the NoData value to 255 or edit your command string to include -dstnodata 255 which should force nodata of 255 outside your cutline. – Michael Stimson Jul 8 at 22:15
  • Thank you @MichaelStimson, that worked fine for me. As far as I understand, one needs to specify suitable value in the value range of the raster in order to properly get Nodata values in the clipping operation. I still have a question for the future: what if all the values (0-255) of my raster were significant and I didn't want to convert any of them into Nodata? Isn't there any way of directly assigning Nodata values to pixels outside the mask layer? – EnricoVac Jul 9 at 8:15
  • 1
    Ah yes, that is the dilemma, if all your values within the range are significant then you can promote the raster to unsigned 16 bit and set nodata to 65535 with translate gdal.org/programs/gdal_translate.html using -ot UInt16 -a_nodata 65535 which can be called from QGIS using Raster::Translate. If promotion to 16 bit isn't an option then you've got a real problem, in this case I make all 255 values 254 as there isn't much difference between the two intensities in a continuous raster and set nodata to 255; if you have a palette or classified raster there is no solution. – Michael Stimson Jul 9 at 22:30
  • Thank you for your answer and advice @MichaelStimson, that was helpful. – EnricoVac Jul 11 at 7:48

Your Answer

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

Browse other questions tagged or ask your own question.