Recode your raster file in GRASS GIS using a csv file

Author: Paulo van Breugel
Updated on: 2017-04-29

The two easiest ways to reclassify a raster layer in GRASS GIS are using the r.reclass or r.recode functions. Although both are easy enough to use, sometimes it would be nice if you could just provide the input layer and a simple table with re-class values to create new raster maps. A fairly trivial task, so ideal to make an attempt at creating a python script if you are a novice.

I create a small add-on (r.recode.attr) that let you reclass/recode a raster layer based on values in a csv (comma separated text) file. The csv file should include one column with the raster values and one or more columns with the reclassification values. For each of these columns a new map will be created. See the manual page for more information.

The add-on is available through the Extension Manager (Settings -> Addon extensions -> Install extensions from addons). For alternative ways to install the add-on, see the GRASS wiki.

Below an example how to use the add-on, using the North Carolina data set. We’ll use the land use map landuse96_28m and reclassify it to create two new maps representing the biodiversity and recreational values of the different land use types.

Land use map landuse96_28m from the North Carolina dataset

The reclassification values are provided in the csv file landuse96_reclass.csv. The first column [RASTERID] represents the land-use mapping units. The 2nd and 3rd column represent the corresponding biodiversity [BIODIVERSITY] and recreational [RECREATIONAL] values. Note, these values are completely fictional, they just serve to illustrate the use of the add-on!

After you have installed the addon, type r.recode.attr in the command console or the terminal and you should get a screen like below.

The r.recode.attr addon

In the example above I have filled in the input map (landuse96_28m), the name of the output layer and the path to the reclass file. I have provided only one output name, so each layer will get the name output_name-columns, i.e., landuse96_BIODIVERSITY and landuse96_RECREATIONAL.

Reclassification of the landuse96_28m layer in two new layers

If you want different/specific names, you need to provide as many names as there are reclassification columns in your table. Thus (using the command line this time):

r.recode.attr input=landuse96 output=BioValue,RecrValue rules=laneuse96_reclass.csv

This will give you the two new layers BioValue and RecrValue.


Software: @grassgis
Tools: @r_recode_attr
Subject: @reclassification

If you have questions

If you have questions or comments about the text, let me know. You can use this contact form. Please make sure to include the page title ("Recode your raster file in GRASS GIS using a csv file") or page name ("recode-raster-layer").