Creating a QGIS color map from text file

I recently found the interesting USGS map of global ecosystems. The data can be viewed online but can also  be downloaded as geotif files, which is great as about every GIS program on the planet can read that format. Unfortunately the color legend is included as a *.lyr file, which can only be used in ArcGIS.

But the good people of USGS also provide a dbf file with, amongst others, the mapping unit, names of the ecosystem units and a color code. In ArcGIS this table can be linked to the raster layers, a features which is not (yet) implemented in QGIS. However, it is easy enough to create a color map from this file, which you can use in QGIS. It is just a text file. The first two lines read:

# QGIS Generated Color Map Export File
INTERPOLATION:EXACT

This is followed by lines with the mapping unit (numerical id), the RGBA code (4 nummers for red, green, blue and alpha), and the label, all separated by commas. This can be simply created in LibreOffice or any capable spreadsheet (but note that MS Excel does not always handle dbf files well).

Step 1
Open the dbf file in Libreoffice. In the screenshot I have only kept the relevant columns.

Attribute table with mapping units id (value), mapping unit name (Macrogroup), and the code for red, green, blue and opacity. The last column (color rule) combines the other columns.
Attribute table with mapping units id (value), mapping unit name (Macrogroup), and the code for red, green, blue and opacity. The last column (colour rule) combines the other columns (see the red encircled code).

Step 2
Join the content of the four columns into one string with the mapping unit, RGBA numbers and the label. This can be done using the CONCANATE function or with &. I used the latter in the screenshot above. See this Stack Exchange Q&A for examples of both approaches. The code I used was:

=A3&","&C3*255&","&D3*255&","&E3*255&","&F3*255&","&B3

The colour values are between 0 and 1, but for the colour map they should be rescaled to a value between 0 and 255. In the code the values in the RGB and Opacity columns are therefore multiplied by 255. In the screenshot I round the number, but that is just to make the column ‘Color rule’ easier to read.

Step 3
Create a text file with as two lines mentioned above, copy the column ‘Color rule’ from the spreadsheet to the text file, to get something like below.

# QGIS Generated Color Map Export File
INTERPOLATION:EXACT
0,255,255,255,255,None
1,0,117,10,255,Guineo-Congolian Evergreen Rainforest
3,17,123,12,255,Guineo-Congolian Semi-Deciduous Rainforest
4,7,110,10,255,Guineo-Congolian Littoral Rainforest

Step 4
Open the raster layer in QGIS, open the layer properties window by double clicking on the layer in the Layers panel. Go to the Style tab and use the option ‘load colour map from file’ to load the file you just created (button in red encircled in screenshot below).

Load colour map from text file
Load colour map from text file

And you should be good to go 🙂

11 thoughts on “Creating a QGIS color map from text file

Leave a comment