5

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.

underdark
84.9k22 gold badges237 silver badges419 bronze badges
asked Aug 7, 2014 at 3:04
1

1 Answer 1

4

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

answered Apr 18, 2015 at 18:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.