I have a "likelihood" raster map which ranges from low to high (e.g. 0 to 100), and I would like to select a specific number of raster cells based on these values. So, if I want to select 533 cells, I want to select the 533 cells with the highest values of the likelihood map.

Trying to find the correct number of cells (by looking at the cell statistics or histograms) was not successful, and also I work with large rasters (large countries with a 200m resolution), so trying my luck with different "larger than" expressions in raster calculator is not feasible as well.

1 Answer 1


You can use Python with numpy if you want to get the values, like:

import numpy as np
from osgeo import gdal

ds = gdal.Open(r"../some_raster.asc")

# rastervalues in array from left to right - top down
myarray = np.array(ds.GetRasterBand(1).ReadAsArray())

# flatten array to a rank one array (m,)
myarray_flattened = myarray.flatten()

# get the n number of larges values in raster
print(myarray_flattened[np.argsort(myarray_flattened, axis=0)[-n:]])

Enter the code in QGIS Python console or QGIS Python editor of menu Plugins > Python Console. Remember to change the raster filename in gdal.Open().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.