Saturday, 1 December 2018

qgis - How to sieve a raster with 1 and NaN values?


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?



Answer




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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...