This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Raster attributes
Tom Evans edited this page Mar 13, 2019
·
4 revisions
A raster attribute is a method of encoding non-numeric (e.g., text) into a raster format. For example, land use classes.
See GDALRasterAttributeTable Class Reference for reference.
Here is an example of applying 11 classes (+ 1 NoData class) in a 4x3 grid, as a geotiff.
import numpy as np
from osgeo import gdal
gdal.UseExceptions()
nrow = 4
ncol = 3
ar = np.arange(nrow * ncol).reshape((nrow, ncol)) * 20
drv = gdal.GetDriverByName('GTiff')
ds = drv.Create('lab_rat.tif', ncol, nrow, 1, gdal.GDT_Byte)
# ds.SetGeoTransform(...)
# ds.SetProjection(...)
band = ds.GetRasterBand(1)
band.WriteArray(ar)
band.SetMetadataItem('RepresentationType', 'THEMATIC')
band.SetNoDataValue(0)
rat = gdal.RasterAttributeTable()
rat.CreateColumn('Value', gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn('Name', gdal.GFT_String, gdal.GFU_Name)
vals = np.unique(ar[ar > 0]).tolist()
for i in range(len(vals)):
rat.SetValueAsInt(i, 0, vals[i])
rat.SetValueAsString(i, 1, 'Row {} is value {}'.format(i, vals[i]))
band.SetDefaultRAT(rat)
# Save and close file
band = rat = ds = None
Here is how the raster is presented in ArcMap 10.5.1:
And how it is represented by gdalinfo
:
$ gdalinfo lab_rat.tif
Driver: GTiff/GeoTIFF
Files: lab_rat.tif
lab_rat.tif.aux.xml
Size is 3, 4
Coordinate System is `'
Image Structure Metadata:
INTERLEAVE=BAND
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 4.0)
Upper Right ( 3.0, 0.0)
Lower Right ( 3.0, 4.0)
Center ( 1.5, 2.0)
Band 1 Block=3x4 Type=Byte, ColorInterp=Gray
NoData Value=0
Metadata:
RepresentationType=THEMATIC
<GDALRasterAttributeTable>
<FieldDefn index="0">
<Name>Value</Name>
<Type>0</Type>
<Usage>5</Usage>
</FieldDefn>
<FieldDefn index="1">
<Name>Name</Name>
<Type>2</Type>
<Usage>2</Usage>
</FieldDefn>
<Row index="0">
<F>20</F>
<F>Row 0 is value 20</F>
</Row>
<Row index="1">
<F>40</F>
<F>Row 1 is value 40</F>
</Row>
<Row index="2">
<F>60</F>
<F>Row 2 is value 60</F>
</Row>
<Row index="3">
<F>80</F>
<F>Row 3 is value 80</F>
</Row>
<Row index="4">
<F>100</F>
<F>Row 4 is value 100</F>
</Row>
<Row index="5">
<F>120</F>
<F>Row 5 is value 120</F>
</Row>
<Row index="6">
<F>140</F>
<F>Row 6 is value 140</F>
</Row>
<Row index="7">
<F>160</F>
<F>Row 7 is value 160</F>
</Row>
<Row index="8">
<F>180</F>
<F>Row 8 is value 180</F>
</Row>
<Row index="9">
<F>200</F>
<F>Row 9 is value 200</F>
</Row>
<Row index="10">
<F>220</F>
<F>Row 10 is value 220</F>
</Row>
</GDALRasterAttributeTable>
The .TIFF file and its RAT are accessible in Delta Shell, although it's necessary to dig into the underlying GDAL libraries a little bit. Here's an IronPython example that accesses the RAT of the raster created by the above example script, using the Delta Shell framework and GDAL library from Sobek.
from Libraries.StandardFunctions import *
from Libraries.MapFunctions import *
# Creating a map and loading a layer into it forces some necessary libraries to load.
# Any shape file will do.
shapeFileLayer = CreateShapeFileLayer("C:\\temp\\Gemeenten.shp")
map = Map()
map.Layers.Add(shapeFileLayer)
map.Name = "New Map"
OpenView(map)
import sys
# Adding GDAL as a Common Language Runtime (clr) library
import clr
clr.AddReferenceToFileAndPath(r"C:\Program Files (x86)\Deltares\SOBEK (3.5.7.35997)\bin\gdal_csharp.dll")
import OSGeo.GDAL as gdal
print gdal.Gdal.VersionInfo('')
print sys.version
ds = gdal.Gdal.Open("C:\\temp\\lab_rat.tif", gdal.Access.GA_ReadOnly)
band = ds.GetRasterBand(1)
rat = band.GetDefaultRAT()
colNames = rat.GetNameOfCol(0)
for i in range(1, rat.GetColumnCount()):
colNames += ", " + rat.GetNameOfCol(i)
print "RAT Columns: " + colNames
Result:
GDAL 1.11.1, released 2014/09/24
2.7.5 (IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.42000 (64-bit))
RAT Columns: Value, Label