5

I am writing numpy arrays out as ENVI images using gdal in python. Until today, I always accessed the header the hard way by reading in all lines, changing some information and writing it out again. This is not only coding intensive, but also yields multiple error sources.

A better solution would be by using SetMetadata. I tried:

myArray = np.full(shape=(4,3), fill_value=10)

driver = gdal.GetDriverByName('ENVI')
destination = driver.Create("test.bsq", ncols, nrows, 1, gdal.GDT_Float32)
destination.GetRasterBand(1).WriteArray(myArray)
destination.SetMetadataItem('band names', 'band hello world', 'ENVI')

but in the output file, the band is still called "band 1". Is my information overwritten by the built-in function for header creation?

Or maybe I have to set the name for each band individually like

b = destination.GetRasterBand(1)
b.SetBandName("band hello world")
b.WriteArray(myArray)

But "SetBandName" does not exist and I cannot find how else it is supposed to be called.

How do I supply band names for ENVI-images then?

3

You need to use the SetDescription method of the raster band object.

rb = destination.GetRasterBand(1)
rb.SetDescription('band hello world')
rb.WriteArray(myArray)

$ cat /tmp/test.hdr 
ENVI
description = {
/tmp/test.bsq}
samples = 3
lines   = 4
bands   = 1
header offset = 0
file type = ENVI Standard
data type = 4
interleave = bsq
byte order = 0
band names = {
band hello world}
  • Incredible! I thought "SetDescription" sets the .... description. Man, you cannot make this up...seriously! Well, thank you so much! – offeltoffel Jul 18 '17 at 9:13

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.