In Python, I have a NumPy array of string values (categories of some variable) that I'd like to write to a raster file (.tif or .img) using GDAL.

The impression I get is that it's not possible to write a string value into the grid cell of a raster file, meaning I'd need to create an integer lookup table and write the integers into the grid cells instead. Then I'd need to write the character data into the column of the raster's attribute table so that you could know what string values the integers represent.

Is my reasoning correct? If so, I'd like to know the basic process for building a raster attribute table using Python, GDAL. I haven't had much success in the past, and, as I understand it, not all GDAL raster formats are able to have attribute tables at all anyway.


A Raster Attribute Table (RAT) is the best way to associate string with pixel values. The formats with the best RAT support I know of are ERDAS Imagine (HFA) and KEA (KEA; http://kealib.org/).

The steps I would use are:

  1. Write a raster with integer values (you can use GDAL to write a NumPy array).
  2. Add a RAT containing each pixel value (you can use the Raster GIS module in RSGISLib to do this; http://rsgislib.org/rsgislib_rastergis.html)
  3. Add a new column with the string associated with each pixel value (you can use RIOS to do this; https://bitbucket.org/chchrsc/rios/).

I've written a script to do steps 2 and 3, it's a bit long to post so I've uploaded to https://bitbucket.org/snippets/danclewley/geaX/add-class-name-to-rat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.