Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to use GDAL to read and write images in ENVI format (for example a .bsq file with an associated .hdr file). The GDAL Formats Info page states "Starting with GDAL 1.10, all ENVI header fields will be stored in the ENVI metadata domain.". If that is the case, how should I access them?

I am using the Python bindings with GDAL 1.9.2 (the latest at this time of writing), and have tried getting all of the metadata:

image.GetMetadata()

which gives me a list of the band 'names' as displayed in ENVI, but that's about it. I've also tried specifically asking for the ENVI metadata domain, as instructed on the GDAL information page linked above:

image.GetMetadata('ENVI')

but that doesn't return anything.

Is there a way to get other metadata from the ENVI file, or will I have to parse the header file myself?

Furthermore, even writing the band names (the ones that seem to be returned when I run GetMetadata()) using SetMetadata() doesn't seem to put them in the .hdr file.

Can anyone shed any light on this?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

"Starting with GDAL 1.10..."

"I am using the Python bindings with GDAL 1.9.2..."

GDAL 1.10 hasn't been released yet. Beta 1 was released a short while ago or if you're using Windows, you can grab a build of the current trunk (1.10dev) from GISInternals.

If you're stuck with 1.9.2 for a while, here's some code to parse envi headers (envi.py)

Some more code that uses regexes to parse a header:

import re

def get_envi_header_dict(hdr):
    #Get all "key = {val}" type matches
    regex=re.compile(r'^(.+?)\s*=\s*({\s*.*?\n*.*?})$',re.M|re.I)
    matches=regex.findall(hdr)

    #Remove them from the header
    subhdr=regex.sub('',hdr)

    #Get all "key = val" type matches
    regex=re.compile(r'^(.+?)\s*=\s*(.*?)$',re.M|re.I)
    matches.extend(regex.findall(subhdr))

    return dict(matches)

if __name__=='__main__':
    hdr='''ENVI
description = {
  RPC Orthorectification Result [Mon Aug 13 13:38:09 2012] [Mon Aug 13
  13:38:09 2012]}
samples = 27856
lines   = 30016
bands   = 1
header offset = 0
file type = ENVI Standard
data type = 12
interleave = bsq
sensor type = WorldView
byte order = 0
map info = {UTM, 1.000, 1.000, 723000.000, 8129434.000, 5.0000000000e-001, 5.0000000000e-001, 55, South, WGS-84, units=Meters}
coordinate system string = {PROJCS["UTM_Zone_55S",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",147.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]}
wavelength units = Micrometers
band names = {
 Orthorectified (Band 1)}
wavelength = {
 0.625000}
'''
    print get_envi_header_dict(hdr)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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