[gdal-dev] Zero Pixel Values. Continuation...

user gdal userofgdal at gmail.com
Mon Feb 21 23:19:57 EST 2011


Dear Sir,
Thanks a lot for reply about zero pixel values when I attempted to
write into an output image file. Though I pasted the code I wrote for
programmatically 'copying' the image, once again I am pasting the same
below.

Q. "One thing I notice about your code is that you only appear to read
and write the first scanline of the image.  Is this intentional?"

Ans. No. I want to copy the whole image. Please see the function
ReadNWrite(...). Am I reading only the first scanline? Oh my God! In
the syntax of the function RasterIO of GDALDataset, it seems there is
no parameter with scanline number. Can you please solve the problem
for me. Before call of the function ReadNWrite, I wanted to 'prepare'
the output image (see the function CreateOutputImg). The problem
should be in the functions CreateOutputImg, ReadNWrite, Read and
Write.

Another worry is projection information, which I wanted to put in
CreateOutputImg. I am sure there are some serious shortcomings in my
attempt. How to set that correctly?

Q. "For the purposes of asking a question of the list it would be helpful if
you could boil your program down to a minimum example including all the
essentials.  It will require extra work on your part, but will make it
much easier for the many members of the list who might want to skim
through the code to see if they have suggestions".

Ans. I have made some more effort to make my difficulty more specific.
Now I request all of you to kindly solve it and if possible, post in
the website.

Thanks once again.

Yours sincerely,
 Ramesh

-------------------------------------------------------------------------------------------------------------------------
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.

Ramesh
-------------------------------------------------------------------------------------------------------------------------


More information about the gdal-dev mailing list