[Gdal-dev] RasterIO()

Frank Warmerdam fwarmerdam at gmail.com
Fri Feb 25 00:07:43 EST 2005


On Sun, 20 Feb 2005 03:49:28 +0000 (UTC), gal05 <senkum_d at yahoo.com> wrote:
> Hai Frank,
>   I am attaching a program which is meant to convert a raw binary image file
> into a GTiff file. If i try to open the output file using ENVI, the image is
> not properly displayed. I dont know what could be wrong. And i am trying to
> set projection "WGS84", i tried using SetWellKnowGS() but it doesn't support.
> So i am passing the data to the setprojection(). Is that right? And whats the
> problem with the RasterIO() that used in the program to write the file.
> Could u help me to modify the program to make it work and display the Gtiff
> image properly.
> Thanks in advance...
> 
> -Gal05

Gal05, 

You aren't too specific about what is wrong with the resulting file
so I will comment on a few items. 
 
>       //SetProjection data
> 
> char *projstring ="PROJCS[\"IMAGINE GeoTIFF Support Copyright 1991 - 2001 by
> ERDAS, Inc. All Rights
> Reserved @(#)$RCSfile: egtf.c $ $Revision: 1.6.1.4 $ $Date: 2001/04/23
> 14:58:04Z $ Projection Name = Lambert
> Azimuthal Equal-ar\",GEOGCS[\"WGS84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84
> \",6378137,298.2572235629972,AUTHORITY[\"EPSG\",\"$

The above is *not* a valid coordinate system definition in WKT. 
>From your geotransform I can tell that you aren't working in a
geographic coordinate system, so I will need details before I 
can propose a reasonable projection string. 
 
> /***************** adding the geotransform values and the projection details
> on the output file *********************/
> 
>         poDstDS->SetGeoTransform( adfGeoTransform );
>         GDALRasterBand *poBand;
>         poDstDS->SetProjection( projstring );
>         printf( "\n\nProjection is %s\n",projstring );
>  CPLFree( projstring );
> 
>         poBand = poDstDS->GetRasterBand(1);
>         cout<<"\nsuccessful completion of passing geotransform and projection
> info\n";//
> 
> /*********************  Reading binary image file row data and writing to the
> ouput file ***************************/
> 
>         FILE *inFile;
>         inFile = fopen(srcFile, "rb");                  // open the input file
> in Read Binary mode
>         float * rowData = (float *)calloc(Xvalue, 1);   // to assign the
> memory for each row data

I'm pretty sure you want to allocate the rowData with 
calloc(Xvalue,4) since floats are 4 bytes each, otherwise 
your buffer will be too small and all sorts of problems might
result. 

>         cout <<"\nreading file..\n"<<sizeof(float)<<"\n";
>             for(int j=0;j<Yvalue;j++)
>                  {
>                   fread(rowData,sizeof(float),Xvalue, inFile);  // reading the
> row data
>                   poBand->RasterIO( GF_Write, 0, j, Xvalue, 1,  rowData,
> Xvalue, 1, GDT_Byte, 0, 0 ); //write data
>                  }

If the buffer is floating point, but you want things truncated to byte
on write, then you should pass GDT_Float32 in the RasterIO() call.
GDAL will notice that the output file is GDT_Byte and cast the floats
to byte values before writing.  note that all values less than 0 will
be set t

Sorry for the delay in responding.  Please subscribe to the list before
replying so your message(s) don't need to be manually approved.

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the Gdal-dev mailing list