[gdal-dev] SOS! Zero pixel values in the output! Please help!

user gdal userofgdal at gmail.com
Fri Feb 4 05:28:41 EST 2011


Dear all,
I am new to GDAL and I was at fault in being too hasty in asking for
an advanced topic in my previous post.

I am facing a fundamental difficulty. The problem is to retrieve
projection and pixel data etc. from a source ERDAS .img file and copy
'as it is' to another file. For this, I wrote a small program in C++
whose relevant sections I am pasting here for kind perusal:

template<class T> void CIOImg_Templ<T>::AllocateMemory( )
{
 unsigned int b;

 buf=new T * [Band];
 if(!buf) HandleException("Memory Allocation Problem");

 for(b=0; b<Band; b++) // Bands in the input file
 {
 *(buf+b)=new T [Col];
 if(!(buf+b)) HandleException("Memory Allocation Problem");
 }
}   // Now buf is allocated with memory as buf[Band][Col]

template<class A> void CIOImg_Templ<A>::CreateOutputImg(GDALDataset *
const* InDS) // output dataset (DS) is defined as a class member
{
 char **options=NULL; /* saw an example of this kind in GDAL site */
 double trans[6];         // the transformation coefficients

 GDALDriver *driver=(*InDS)->GetDriver( );

 GDALAllRegister( );
 if(!driver) HandleException("Sorry, couldn't load the driver for your format");

 outds=(GDALDataset *) GDALOpen(outfilename, GA_Update);
 outds=driver->Create(outfilename, Col, Row, Band, datatype, options);
 // is it correct

 (*InDS)->GetGeoTransform(trans);
 outds->SetGeoTransform(trans);

 outds->SetProjection((*InDS)->GetProjectionRef( ));

 /* Seems there is much more to all this, but how to do? */
}

template<class T> void CIOImg_Templ<T>::Read(const unsigned int b,
GDALDataset* const *inds)
{
 ((*inds)->GetRasterBand(b+1))->RasterIO(GF_Read, 0, 0, Col, 1,
buf[b], Col, 1, datatype, 0, 0);   // able to read the pixel values
correctly into buffer (buf)
}

template<class T> void CIOImg_Templ<T>::Write(const unsigned int b,
GDALDataset** opds)
{
 ((*opds)->GetRasterBand(b+1))->RasterIO(GF_Write, 0, 0, Col, 1,
buf[b], Col, 1, datatype, 0, 0);

 */ (*opds)->RasterIO(GF_Write, 0, 0, Col, 1, buf[b], Col, 1,
datatype, 1, NULL, 0, 0, 0); */ /* Neither of these is working */
}

template<class T> void CIOImg_Templ<T>::ReadNWrite(GDALDataset * const *inds)
{
 unsigned int b, i=0;

 CreateOutputImg(inds);

 while(i<Row) // for each row
 {
 b=0;
 while(b<Band)
 {
 Read(b, inds); Write(b, &outds); // read row by row and write into a new file
 b++;
 }

 i++;
 }
}

The class's destructor closes the output dataset ("outds") using GDALClose.

I checked the input pixel values just after the call to the function
'Read'. They were read correctly (I verified with ERDAS software).
However, I am getting an image with exactly the same rows, columns and
bands as the input image but the output image contains only NULL (0)
values everywhere! Also, even if the input image is projected, I do
not see the projection info. in the output img. I understand that many
more parameters have to be retrieved and set properly for the output
img, I don't know how to.

Finally, I also chcked GDAL tutorials. It uses OGRSpatialReference,
apparently a class or a struct. Moreover, the example in the site ()
is limited only for UTM, whereas a general program must 'adapt' to the
required projection, if I'm correct. If I use OGR..., the program is
not compiling. Can you please provide me a solution.

In a nutshell (forgive me for the long message):

I didn't expect that I would stuck at this level itself, especially
the pixel values. I request your help in this regard and projection
info.

With many thanks,

Yours sincerely,
 Ramesh.


More information about the gdal-dev mailing list