[gdal-dev] problems reading a .img file containing only elevation data

Tom Flynn flynnt at acm.org
Fri Sep 26 20:15:04 EDT 2008

Hi there,

First a disclaimer:  I come from a 3d graphics background and am
somewhat of a GIS noob.  Forgive me if I'm doing something blatently

I have a 6000x5000 Erdas .img file containing elevation data as
floating point numbers (GDT_Float32).  I can bring up and view this
file in GlobalMapper on Windows and in QGIS on Linux.  However, when
trying to read this file via GDAL in my application, I get all 0s.
I'm using the GDALDataset::RasterIO() method.  I try to read a line at
a time into a buffer that I then process on my own.  All the data i
seem to be getting into my buffer seems to be zero though.  Here are
my arguments to RasterIO():

                         GF_Read,        // eRWFlag
                         0, yoffset,     // nXOff, nYOff
                         nXSize, 1,      // nXSize, nYSize
                         pData,              // pData
                         nXSize, 1,      // nBufXSize, nBufYSize
                         type,           // eBufType
                         band,         // nBandCount
                         NULL,           // panBandCount
                         0,              // nPixelSpace
                         0,              // nLineSpace
                         0 );            // nBandSpace

nXSize is the size in pixels of the elevation data (6000).
pData is the pointer to my buffer
type is GDT_Float32 in this case
band is 1, there is only one band in this file.
yoffset is the number of lines down in the file where i want to start reading.

I'm expecting this call to put nXSize*4 bytes of data into my buffer
at pData.  That data will contain floating point numbers.
Do my arguments match what I intend?

I compiled gdal debug and did some single-stepping in the debugger.  I
followed it as far as line 240 in rasterio.cpp in gdal-1.5.2/gcore/.
That line is where the memcpy() occurs.  If i take a look at the
buffer it is copying from, it contains all 0s.

If I do a gdal_translate to another format (say 16bit png or geotiff),
i see all black.

I'm not really familiar with this Erdas .img file format.  Some
cursory googling didn't turn up much info that was useful.  All I know
is that the data is supposedly in 64x64 blocks.  My file size is
essentially 6000x5000x4 (for floats) + 1MB + 189 bytes.  I'm guessing
the data is uncompressed since the file is quite a bit larger than
just raw floating point numbers would be.  I'm guessing 189 bytes is
header info (haven't verified yet).  Have no idea what an entire
friggin' MB is being used for....and assuming the rest is elevation
data.  I did some guessing of where the data I want to access should
be and tried writing a simple program to open the file, seek to the
data i want, read and save it out.  While I get elevation numbers in
the range I expect, it's not clear to me how the blocks are arranged.

This is all very frustrating.

Any clues as to what I'm potentially doing wrong in gdal or a pointer
to some docs on how to parse out and read a .img would be greatly

Thanks in advance for any help,

More information about the gdal-dev mailing list