In QGIS I have a raster map showing land, snow and cloud zones, having resp. pixel values 1, 2, and 3. I extracted the mean altitude values (in m) of each zone on the basis of an underlying DEM and Zonal Statistics of the LecoS (Landscape Ecology Statistics) plugin. The results show in a table like:

enter image description here

On the basis of the mean values of zone 1 and 2, using r.rclass from the Processing Toolbox, I reclassified my zones using this reclass rules text file:
2588 thru 4497 = 1
4498 thru 5122 = 12
5123 thru 7533 = 2
(2588 is the minimum altitude of the DEM, 7533 the maximum altitude).
The result is a new raster map with zones land (1), snow (2) and mixed land-snow pixels (12). This works fine in separate steps and manually preparing the reclass rules text file.

However I have thousands of files to process! How would it be possible to use the output of the table mean values to automatically populate a reclass rules text file, that can then be directly used in r.reclass?
r.reclass doesn't have an option to import values from an outside table file. I guess I need to write a script for this. I have no experience with scripts so far.

  • 1
    In recent QGIS versions there's a "reclassify by layer" algorithm - does this assist? – ndawson Feb 13 at 7:17
  • Looks promising but then I first have to fill manually a table with min, max and value values. That is the same as manually making a reclass values text file. – henkth Feb 13 at 8:03
  • I think you're looking at the wrong algorithm - there 2, one which requires a manual table, the other which can take the class values from an existing layer (e.g. the layer generated by LecoS). Potentially you could make a simple graphical model which runs the Lecos tool and then does the reclassify using its output, and run that using batch processing over all your layers. – ndawson Feb 13 at 9:30
  • I looked at the correct algorithm, but it needs the input of the table of a vector layer with geometry information. LecoS just produces a delimited text table with no geometry. That's why it didn't work. Thanks for your help anyway! – henkth Feb 13 at 11:03

First solution is to use r.reclassas a batch process by right-clicking on it in process list

enter image description here

It opens a new window where you can add every raster one by one but it's still a tedious task for thousands of rasters.

enter image description here

For a massive treatment, the best way is to use a python script with rasterio lib. Something like this:

import os, sys, rasterio, glob #import lib
import numpy as np #import lib

rasters_folder = sys.argv[1] #path of raster folder

rasters_list = glob.glob('{0}/*.tif'.format(rasters_folder)) #create a list of rasters

for raster_path in rasters_list: #for each raster in the list do...
    with rasterio.Env(): #work with rasterio environment
        src = rasterio.open(raster_path) #open the raster
        profil = src.profile #raster profile like height and width

        img = src.read(1) #first band of raster

        #3 reclassment conditions
        img[np.where((2588 < img) & (img <= 4497)) ] = 1
        img[np.where((4497 < img) & (img <= 5122)) ] = 12
        img[np.where((5122 < img) & (img <= 7533)) ] = 2

        #save result in a new raster                
        with rasterio.open('{}_reclass.tif'.format(os.path.splitext(raster_path)[0]), 'w', **profil) as dst:
            dst.write(img, 1)
  • Thanks for your answer! I am aware of the batch processing. The problem is that all my raster files are different and will produce different mean values, so my reclass rules values are different for each file. I don't want to prepare the reclass rule files one by one manually in this case. – henkth Feb 13 at 11:09
  • 1
    @HenkThoma So the best way is to use a python script. You can do everything you want, it's more flexible than any other solution. I know to learn python it's a big time investment, but I did 2 years ago and I don't regret it. – Tim C. Feb 13 at 13:17

Just a follow-up on my above question. I have been able to find a solution by learning more about python scripting. I did the following and it worked (for 1 file):

enter image description here

Maybe not the best code but this is my humble beginner attempt with a (for me) serious script. In the graphic modeler of QGIS I managed to implement this script and use it for multiple files in batch processing. I would like to thank everyone that tried to help with this post!


I would do this by writing simple field formulae in Excel. You can then copy and paste for your new tables and do it in such a way that the table or a number of columns within it can be exported as text using space delimiters. Alternatively, wrap that into a Python function for greater automation.

  • Thanks for the suggestion! I am not sure how to proceed in Excel, worse even how to do this in Python. If you could be so kind, could you show me how you would do it in Excel using the above mentioned mean values to get as an output the mentioned reclass rules txt. Greatly appreciated! – henkth Feb 13 at 11:35

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.