GRASS GIS categorical raster layers in QGIS

With the QGIS GRASS plugin, you can easily open a GRASS raster layer in QGIS. One disadvantage, however, is that if your GRASS layer has category labels, these will not show in the QGIS legend. See for example the landuse layer in the North Carolina data set,  opened in QGIS in the screenshot below.

screenshot1

It isn’t too much problem to define the labels in QGIS if you have few categories only, but what if your map in GRASS has many categories, all neatly labelled in GRASS. Sure you don’t want to add those category labels manually again in QGIS?

I don’t, so I wrote a small GRASS GIS addon, r.category.trim, that export the attribute table and colour table in GRASS GIS as a QGIS colour map file.

This is  just a simple text file that QGIS can use to set the colours and attribute labels for a raster layer. You can actually create one easily in a spreadsheet, as described here.

With the addon it is easier though. Just install the addon through the extension Manager (Settings -> Addon extensions -> Install extensions from addons) or on the command line using g.extension. For alternative ways to install the add-on, see the GRASS wiki. Next, type r.category.trim on the command line or command console. This will open a window with a few tabs.

The r.category.trim GUI

Fill in the name of the input map (Raster tab) and the name and path of the QGIS colour file (Export tab) and select Run. Alternatively, you can run this from the command line, using:

r.category.trim input=landuse qgis=colourmap.txt

Now open the raster layer in QGIS and next open the Layer properties window. Go to the ‘Style tab’ and select as Render type the Singleband pseudocolor, if not already selected. Next load the colour map from the file you just created: Click on the little yellow folder icon above the legend and select the file.

open colourmap

Apply, click OK and you’ll have the legend with the attribute labels.

legend with labels

You can also export the category values, category labels and corresponding colours as a comma separated calues (CSV) file file, which can be viewed in a spreadsheet program such as Libre/Open Office Calc, Microsoft Excel, or Google Docs and shared together with an exported raster file for users that use other GIS programs.

Trimming redundant map categories

Besides the option to export the categories and corresponding labels and colours, the addon will trim redundant labels and colours. That probably requires a bit of explanation.

In GRASS the category labels and colours are stored internally in a ‘category table’ and ‘colour table’. Now, if you create a map by ‘cutting out’ a subset from another categorical map, like in the example below (using the landuse layer of the North Carolina data set as input), the full category and colour tables of the original map are copied to the new map, even if that new map does not include all categories.

g.region -a n=219800 s=218830 w=635850 e=636920
r.mapcalc "test = landuse@PERMANENT"

The new layer ‘test’ only has 3 of the 7 categories of the ‘landuse’ layer. Yet, the legend of test shows all 7 categories of the original map. See here for a more detailed explanation.

Map created by 'cutting out' a small part of the landuse map of the North

The r.category.trim addon trims all the redundant category and colours from the map test. In the example below I do this by creating a new layer test2, but you can also directly trim redundant categories from the input layer (just leave the output field empty).

# Trim the redundant categories
r.category.trim input=test output=test2 qgis=colourmap.txt

As you can see in the screenshot below, the legend of the layer ‘test2’ only contains the categories that are actually part of the map.

caption

And now you can load the colour map from the file just created to get the category labels.

The test2 layer, but this time with the category labels

One additional option is to recode the categories. When cutting out a map from a larger map, you may end up with ‘gaps’ between category values.

screenshot10

In some cases you may prefer the new map to have consecutive category values. The addon therefore offers the option to recode the map to consecutive values. You can do this by setting the n flag.

screenshot11

Which will get you a new raster layer with consecutive category values, but the same colours and category labels.

screenshot12

It is really a very simple addon, but at least for me it makes it just a bit easier to work with GRASS GIS categorical raster layers in QGIS and to share subsets of raster layers.

8 thoughts on “GRASS GIS categorical raster layers in QGIS

  1. Pingback: GRASS GIS categorical raster layers in QGIS | GeoNe.ws

  2. Alister

    Hi,
    If I found a problem with this, should I post a ticket on the grass trac?
    I’m getting errors like the one below:

    r.category.trim input=test@Tata qgis=C:\Processing\test cat.txt
    Traceback (most recent call last):
    File “C:\Users\alistair\AppData\Roaming\GRASS7\addons/scri
    pts/r.category.trim.py”, line 197, in
    sys.exit(main(*gs.parser()))
    File “C:\Users\alistair\AppData\Roaming\GRASS7\addons/scri
    pts/r.category.trim.py”, line 123, in main
    CCAT = map(int, CCAT)
    ValueError: invalid literal for int() with base 10:
    ‘24.0862’

    1. Hi Alister, yes, please, create a ticket on the GRASS trac. I am quite busy right now, so posting it there will make sure it does not get forgotten.

      1. Looks like you are not working with an integer raster layer. The addon should check for that and give a warning, strange. I will have a look at it later this week.

  3. Alister

    > Looks like you are not working with an integer raster layer.

    I didn’t think so, although I’m essentially a beginner with GRASS, so I don’t know how to check.

    I made it with v.to.rast using an attr column that db.describe confirms contains integers.

    And these are the categories:

Leave a comment