As a next step to my previous question "How to calculate where raster a is not equal to raster b?", I now want to sieve (using QGIS -> Raster -> Analysis -> Sieve, or gdal_sieve in the shell) a raster which consists of the values 1 and NaN. The problem is: no matter what parameters I tell gdal_sieve to use, it still returns the original raster, nothing happens.

Pixels I want so sieve out

Above you can see the raster (black pixels) that I want to sieve. Actually, this screenshot was taken immediately after the sieve algorithm ran through. You will find that nothing was sieved.

I guess this is because gdal_sieve cannot handle NaN, so the nearest possible value always is 1 again, if it skips the NaNs.

Because I want to use Zonal Statistics with this raster and some polygons afterwards, it is necessary that this raster consists just of 1 and NaN, because I am interested in the total count of pixels with value 1 in a polygon. If I would use a raster with, say 0 and 1, it would always accumulate these two in my count column.

Does anyone have an idea how to workaround this?

  • 1
    Can you post your gdal_sieve command, please. This works for me: gdal_sieve -st 100 -8 -nomask not_sieved.sdat -of "GTIFF" sieved.tif. Everything >100 connected pixel will be excluded with a treshold of 8.
    – Stefan
    Aug 9, 2017 at 12:20
  • This seems to work for me as well, thank you! I have only tried it via the QGIS GUI yet, but because the gdal_sieve command is shown below there, I didn't think that typing it in a bash script would function differently. The -nomask flag seems to make the difference, trying the same parameters via the GUI still does not work.
    – bellackn
    Aug 9, 2017 at 16:12

1 Answer 1


The code from Stefans comment works for me:

gdal_sieve.py -st 8 -8 -nomask S2_2015_AOI_Bebaut.tif -of "GTIFF" sieved.tif

The only difference I can see between the GUI version in QGIS and this bash command is the -nomask flag, which cannot be set in QGIS. This seems to do the trick. The official GDAL doc describes its function as follows:

Do not use the default validity mask for the input band (such as nodata, or alpha masks).

I cannot fully understand what this means, but as long as it works, I'm fine with it.

  • 1
    I had the same problem, but as of QGIS 3, there is a tick box in the GUI for 'Do not use the default validity mask for the input band'.
    – EcologyTom
    Sep 30, 2021 at 9:40

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.