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.
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.
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.
Apply, click OK and you’ll have the legend with the attribute 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.
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.
And now you can load the colour map from the file just created to get 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.
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.
Which will get you a new raster layer with consecutive category values, but the same colours and category labels.
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.