I have a raster (GeoTIFF .tif file) that is stored in three bands (Red, Green, Blue), with each band's data type as a byte. I have a separate color table in the form of a .csv file, that stores a height value (in meters), a Red value, Green value, and Blue value on each line. I need to able to utilize this color table to interpret the RGB values in the raster as raw height values. I am using C# bindings of GDAL, OGR and OSR. How do I get the file loaded so that it can be represented as a GDAL color table?

I know OGR has a CSV driver which you can use to Open the file, which I have done. This creates a DataSource object. But I have no idea how to convert this DataSource to a GDAL ColorTable (if it is even possible). Or is another method needed, such as reading the file line by line manually?

  • 1
    As per the Tour there should be only one question asked per question.
    – PolyGeo
    Commented Sep 29, 2019 at 8:00

1 Answer 1


Through trial and error I have figured out a working solution to this problem.

1) Load the CSV driver manually:

var csvDriver = OSGeo.OGR.Ogr.GetDriverByName("CSV");

2) Open the CSV file using the driver:

using (var dataSource = csvDriver.Open(fullColorTableFilePath, 0))

3) Get the layer from the DataSource. As far as I can tell this is all of the lines of the file:

var layer = dataSource.GetLayerByIndex(0);

4) Get the first "feature" (line of data) from the file:

var feature = layer.GetNextFeature();

5) Verify the data I need is in the file. I am going to enforce column names with my users in order to ensure consistency and reduce the complexity of the task:

int redIndex = feature.GetFieldIndex("Red");
int greenIndex = feature.GetFieldIndex("Green");
int blueIndex = feature.GetFieldIndex("Blue");
int elevIndex = feature.GetFieldIndex("Start_elev");

6) Get the field values for the line:

var elev = feature.GetFieldAsDouble(elevIndex);
var red = feature.GetFieldAsInteger(redIndex);
var green = feature.GetFieldAsInteger(greenIndex);
var blue = feature.GetFieldAsInteger(blueIndex);

7) Continue reading each line using layer.GetNextFeature.

Your Answer

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

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