Color tables in GRASS GIS – using the command line

In GRASS to define the colors of a raster you normally go to the menu Raster → Manage colors → Color tables. Alternatively type r.colors on the command line. Both will open a screen where you can select the raster layer and one of the many pre-defined color tables. There are a number of additional options, most are self-explanatory, otherwise check out the help function.

Instead of a pre-defined color table, you can also use a rules file, which is a simple text file with the rules defining the colors for the different map categories or ranges of map values (i.e., it defines an user-defined color table). There are two methods to specify the colors: by category value and by ‘percentage’ values. Colors can be defined using the RGB notation or the name of the colors. Check out the help file, this gives a clear explanation and examples of the different options.

Rules files are simple text files and can be created using any text editor or directly on the command line. It can also be created as part of a GRASS bash script, using the cat function, like the following example: cat > rules.txt << EOF

1 10:58:0
2 21:101:21
3 82:140:82
4 144:238:144
r.colors map=mapname rules=rules.txt
rm rules.txt

The EOF is a so-called “here tag”. Basically it tells the shell that you are going to enter a multi-line string after the first EOF until the second EOF. This text will be used as input for cat, which writes the text to a new text file rules.txt. Next, this text file is used in the GRASS function r.colors to define the colors of map categories 1-4. The last line is just to remove the text file as you won’t need it anymore.

There are a few rules for the use of these “here tags”: Tags will not be considered as a “here tag” if there are other words in that line. Likewise, the tag should not have leading or trailing spaces in that line. In both cases they will be considered as part of the input string. See this forum post for a more detailed explanation.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s