[Gdal-dev] RasterIO()
gal05
senkum_d at yahoo.com
Sat Feb 19 22:49:28 EST 2005
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
#include "gdal_priv.h"
#include "cpl_string.h"
#include <ogr_spatialref.h>
#include <iostream.h>
#include <cstring>
/* The program is to add a GeoTIFF header to a binary image and output a
GeoTIFF file
Inputs : filename x y
where filename : the name of the binary image to be added with GeoTIFF
header
x : numbers of columns for the input image (in this case 21000)
y : numbers of rows for the input image (in this case 12500)
*/
/**Reading the input file name, column and row values ****/
int main(int argc, char **argv)
{
char *src = argv[1];
int Xvalue=atoi(argv[2]); //read the X and Y value from command line
int Yvalue=atoi(argv[3]);
/* construct the input file name and store the source name in the array */
// normally input file name has an extension of .img
char srcFile[100]; //array for source file name
char dstFile[105]; //array for destination file name
int i=0;
while(src[i]!='\0')
{
srcFile[i]=src[i]; //reading the input name
dstFile[i]=src[i];
i++;
}
srcFile[i]='\0';
dstFile[i]='\0';
// to add the .tif extension to the output file name
char ext[3]={'f','i','t'};
int l=0;
for(int k=i-1; k>=i-3; k--)
{
dstFile[k]=ext[l];
l++;
}
cout <<"\nDestination file name :"<<dstFile<<"\n";
/**************** construct an output GeoTiff image file with
geotransformation and projection ***********/
//adfGeoTransform[5] array value
double adfGeoTransform[6]={-
2624999.963000,250.000000,0.000000,1006701.788000,0.000000,-250.000000};
// definitions for using GDAL library
char **papszOptions = NULL;
GDALDataset *poDstDS;
const char *pszFormat = "GTiff";
GDALDriver *poDriver;
char **papszMetadata;
GDALAllRegister(); //Register All Drivers
//Set the Driver using DriverManager
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
//Create a new dataset with this driver
poDstDS = poDriver->Create(dstFile, Xvalue, Yvalue, 1, GDT_Byte,
papszOptions );
//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\",\"$
/***************** 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
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
}
cout<<"\ndestination file written!\n";
free(rowData); // free memory
fclose(inFile); // close input file
delete poDstDS;
return 0;
}
More information about the Gdal-dev
mailing list