2

I am trying to create a GeoTIFF raster with GDAL from data stored in numpy matrices. The data is one band, consisting of 0 and 1 values. The zero values are my nodata values. The problem is, the raster it outputs has 255 bands when viewed in ArcGIS. The one band I care about is present and visible, but I'd like to get rid of the other bands, if possible. I think it might have to do with the eType param for driver.Create() but I'm not sure.

Here is my Python GDAL code:

raster = driver.Create(rasterPath, results.shape[1], results.shape[0], 1, gdal.GDT_Byte)

raster.SetProjection(projection)
raster.SetGeoTransform(geotransform)
output_band = raster.GetRasterBand(1)
output_band.SetNoDataValue(0)

colors = gdal.ColorTable()
colors.SetColorEntry(1, (255, 0, 0))

output_band.SetRasterColorTable(colors)
output_band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)

output_band.WriteArray(results)
output_band.FlushCache()
output_band.ComputeStatistics(False)

output_band = None
raster = None

Here is a screenshot of the extra bands I'm talking about in ArcGIS:

extra bands

The extra, black bands go all the way to 255. Any ideas?

5
  • 1
    You have a raster with one band, not 255. The screenshot shows your color table entries. You set one entry to red, the rest are black because ArcGIS must assume this color table has 256 entries, not sure why.
    – user2856
    Commented Apr 5, 2021 at 23:47
  • 1
    Just change the symbology in ArcGIS from colormap to unique values.
    – user2856
    Commented Apr 5, 2021 at 23:54
  • Okay, I see. That makes sense. Is there any way to set the symbology as unique values via GDAL/Python code? I'm using this script to generate 100+ rasters, so it'd be kinda tedius to changle the symbology individually within arcgis.
    – mwsmws22
    Commented Apr 6, 2021 at 0:12
  • 1
    If you're symbolizing in ArcGIS, then the symbology needs to be done with ArcGIS. Fortunately, you can save a layerfile and apply that symbology across the board.
    – Vince
    Commented Apr 6, 2021 at 0:22
  • Got it. I'll look into that. Thanks for the help user2856 and Vince!
    – mwsmws22
    Commented Apr 6, 2021 at 0:26

2 Answers 2

2

You have a raster with one band, not 255. The screenshot shows your color table entries. You set one entry to red, the rest are black because ArcGIS must assume this color table has 256 entries, not sure why.

One way to avoid this is to use an attribute table color table:

driver = gdal.GetDriverByName( "GTiff" )
dst_ds = driver.Create( dst_filename, ncols, nrows, nbands, gdal_datatype )
  
# Create and populate the RAT
rat = gdal.RasterAttributeTable()
rat.CreateColumn('VALUE', gdal.GFT_Integer, gdal.GFU_Generic)
rat.CreateColumn('RED', gdal.GFT_Real, gdal.GFU_Generic)
rat.CreateColumn('GREEN', gdal.GFT_Real, gdal.GFU_Generic)
rat.CreateColumn('BLUE', gdal.GFT_Real, gdal.GFU_Generic)
rat.SetValueAsInt(0, 0, 1)
rat.SetValueAsDouble(0, 1, 1.0)  # args are row, col, value
rat.SetValueAsDouble(0, 2, 0.0)  # value is 0-255 rescaled 0-1 by dividing by 255.0
rat.SetValueAsDouble(0, 3, 0.0)  # to set an RGB of (255, 100, 0) for these 3 columns,
                                 # you would use values of 1.0, 0.39 and 0.0 respectively  

# Associate with the band
rb = dst_ds.GetRasterBand(1)
rb.WriteArray( raster[band, :, :] )
rb.SetDefaultRAT(rat)

enter image description here

1
  • Thank you! This fixed it.
    – mwsmws22
    Commented Apr 6, 2021 at 2:14
0

First of all, try to do FlushCache() to whole raster, not only for one band. GDAL in Python can be a little bit confusing and make some problem with cache, so once the code is done, write reset in command prompt. It works like FlushCache in code, but it's worth to try before any further steps. Try to open your new raster in python with gdal and do raster.RasterCount to see how many bands has your new file indeed.

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.