I have a raster file which has a range (min: 80 max: 120). I would like to reclass this raster by using quantiles. Is that possible?
If it is just for visualisation, then you can adjust how the raster is displayed in QGIS, by choosing single band pseudo colour with discrete colour interpretation - you can then adjust the boundaries between each colour yourself.
If you actually need to produce an output raster classified by percentile, then it may or may not be possible to do so with the raster calculator in QGIS. But I would strongly advise that you have a look at the python tools that are available, as they really are ideal for this. Numpy has a percentile function which does exactly this.
I believe that QGIS comes with numpy as standard, so you can do this in the QGIS python console (open with ctl + alt + p).
An example script would be
import numpy as np from osgeo import gdal, gdal_array # open the dataset and retrieve raster data as an array dataset = gdal.Open("/path/to/image.tif") array = dataset.ReadAsArray() # create an array of zeros the same shape as the input array output = np.zeros_like(array).astype(np.uint8) # use the numpy percentile function to calculate percentile thresholds percentile_80 = np.percentile(array, 80) percentile_60 = np.percentile(array, 60) percentile_40 = np.percentile(array, 40) percentile_20 = np.percentile(array, 20) percentile_0 = np.percentile(array, 0)
The numpy.where function to change the output array; the syntax is np.where((condition), x, y)), where x is the value to set if the condition evaluates to true at that index, and y is the value to set if the condition evaluates to false.
output = np.where((array > percentile_0), 1, output) output = np.where((array > percentile_20), 2, output) output = np.where((array > percentile_40), 3, output) output = np.where((array > percentile_60), 4, output) output = np.where((array > percentile_80), 5, output)
The gdal_array.SaveArray is a very handy function which allows you to specify a prototype, in this case the input dataset, from which the projection info is copied.
outname = "/path/to/output_name.tif") gdal_array.SaveArray(output, outname, "gtiff", prototype=dataset)