Dear all,<br>
I am new to GDAL and I was at fault in being too hasty in asking for<br>
an advanced topic in my previous post.<br>
<br>
I am facing a fundamental difficulty. The problem is to retrieve<br>
projection and pixel data etc. from a source ERDAS .img file and copy<br>
'as it is' to another file. For this, I wrote a small program in C++<br>
whose relevant sections I am pasting here for kind perusal:<br>
<br>
template<class T> void CIOImg_Templ<T>::<div id=":4j">AllocateMemory( )<br>
{<br>
unsigned int b;<br>
<br>
buf=new T * [Band];<br>
if(!buf) HandleException("Memory Allocation Problem");<br>
<br>
for(b=0; b<Band; b++) // Bands in the input file<br>
{<br>
*(buf+b)=new T [Col];<br>
if(!(buf+b)) HandleException("Memory Allocation Problem");<br>
}<br>
} // Now buf is allocated with memory as buf[Band][Col]<br>
<br>
template<class A> void CIOImg_Templ<A>::CreateOutputImg(GDALDataset *<br>
const* InDS) // output dataset (DS) is defined as a class member<br>
{<br>
char **options=NULL; /* saw an example of this kind in GDAL site */<br>
double trans[6]; // the transformation coefficients<br>
<br>
GDALDriver *driver=(*InDS)->GetDriver( );<br>
<br>
GDALAllRegister( );<br>
if(!driver) HandleException("Sorry, couldn't load the driver for your format");<br>
<br>
outds=(GDALDataset *) GDALOpen(outfilename, GA_Update);<br>
outds=driver->Create(outfilename, Col, Row, Band, datatype, options);<br>
// is it correct<br>
<br>
(*InDS)->GetGeoTransform(trans);<br>
outds->SetGeoTransform(trans);<br>
<br>
outds->SetProjection((*InDS)->GetProjectionRef( ));<br>
<br>
/* Seems there is much more to all this, but how to do? */<br>
}<br>
<br>
template<class T> void CIOImg_Templ<T>::Read(const unsigned int b,<br>
GDALDataset* const *inds)<br>
{<br>
((*inds)->GetRasterBand(b+1))->RasterIO(GF_Read, 0, 0, Col, 1,<br>
buf[b], Col, 1, datatype, 0, 0); // able to read the pixel values<br>
correctly into buffer (buf)<br>
}<br>
<br>
template<class T> void CIOImg_Templ<T>::Write(const unsigned int b,<br>
GDALDataset** opds)<br>
{<br>
((*opds)->GetRasterBand(b+1))->RasterIO(GF_Write, 0, 0, Col, 1,<br>
buf[b], Col, 1, datatype, 0, 0);<br>
<br>
*/ (*opds)->RasterIO(GF_Write, 0, 0, Col, 1, buf[b], Col, 1,<br>
datatype, 1, NULL, 0, 0, 0); */ /* Neither of these is working */<br>
}<br>
<br>
template<class T> void CIOImg_Templ<T>::ReadNWrite(GDALDataset * const *inds)<br>
{<br>
unsigned int b, i=0;<br>
<br>
CreateOutputImg(inds);<br>
<br>
while(i<Row) // for each row<br>
{<br>
b=0;<br>
while(b<Band)<br>
{<br>
Read(b, inds); Write(b, &outds); // read row by row and write into a new file<br>
b++;<br>
}<br>
<br>
i++;<br>
}<br>
}<br>
<br>
The class's destructor closes the output dataset ("outds") using GDALClose.<br>
<br>
I checked the input pixel values just after the call to the function<br>
'Read'. They were read correctly (I verified with ERDAS software).<br>
However, I am getting an image with exactly the same rows, columns and<br>
bands as the input image but the output image contains only NULL (0)<br>
values everywhere! Also, even if the input image is projected, I do<br>
not see the projection info. in the output img. I understand that many<br>
more parameters have to be retrieved and set properly for the output<br>
img, I don't know how to.<br>
<br>
Finally, I also chcked GDAL tutorials. It uses OGRSpatialReference,<br>
apparently a class or a struct. Moreover, the example in the site ()<br>
is limited only for UTM, whereas a general program must 'adapt' to the<br>
required projection, if I'm correct. If I use OGR..., the program is<br>
not compiling. Can you please provide me a solution.<br>
<br>
In a nutshell (forgive me for the long message):<br>
<br>
I didn't expect that I would stuck at this level itself, especially<br>
the pixel values. I request your help in this regard and projection<br>
info.<br>
<br>
With many thanks,<br>
<br>
Yours sincerely,<br>
Ramesh.</div>