Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgmeridianswitcher.cpp

Go to the documentation of this file.
00001 #include "omgmeridianswitcher.h"
00002 
00003 #include "gdal_priv.h"
00004 #include <QTextStream>
00005 #include <QFile>
00006 #include <iostream>
00007 #include <QString>
00008 #include <QVector>
00009 #include <QMap>
00010 
00011 
00012 OmgMeridianSwitcher::OmgMeridianSwitcher() : QObject()
00013 {
00014 
00015 }
00016 
00017 
00018 OmgMeridianSwitcher::~OmgMeridianSwitcher() 
00019 {
00020 }
00021 
00022 bool OmgMeridianSwitcher::doSwitch(QString theInputFileString, QString theOutputFileString)
00023 {
00024   QFile * myFilePointer=new QFile(theOutputFileString);
00025   QTextStream * myTextStream;
00026   QString mySeperatorString=QString(" ");
00027 
00028   if (!myFilePointer->open(QIODevice::WriteOnly))
00029   {
00030     emit error ("OmgMeridianSwitcher::Cannot open file : "+ theOutputFileString);
00031     return false;
00032   }
00033   else
00034   {
00035     myTextStream = new QTextStream(myFilePointer);
00036     emit message("OmgMeridianSwitcher::Opened file ... "+ theOutputFileString+" successfully.");
00037   }
00038 
00039   //printf("Started with input :  %s output: %s \n",theInputFileString,theOutputFileString);
00040   GDALAllRegister();
00041   GDALDataset  *gdalDataset = (GDALDataset *) GDALOpen( theInputFileString.toLocal8Bit(), GA_ReadOnly );
00042   if ( gdalDataset == NULL )
00043   {
00044     //valid = FALSE;
00045     emit error ("OmgMeridianSwitcher::Cannot create gdal dataset: "+ theOutputFileString);
00046     return false;
00047   }
00048   int myXDimInt = gdalDataset->GetRasterXSize();
00049   int myYDimInt = gdalDataset->GetRasterYSize();
00050   printf("Raster is %i x %i cells\n", myXDimInt, myYDimInt);
00051 
00052   //note the ll corner needs softcoding!
00053   QString myHeaderString=
00054       QString ("ncols         ") +
00055       QString::number (myXDimInt) + 
00056       QString ("\n")+
00057       QString ("nrows         ") + 
00058       QString::number (myYDimInt) + 
00059       QString ("\n")+
00060       QString ("xllcorner     -180\n")+
00061       QString ("yllcorner     -90\n")+
00062       QString ("cellsize      ") + 
00063       QString::number (360/static_cast<float>(myXDimInt)) +
00064       QString ("\n")+
00065       QString ("nodata_value  -9999.0\n");                    
00066       *myTextStream << myHeaderString;
00067 
00068   GDALRasterBand  *myGdalBand = gdalDataset->GetRasterBand( 1 );
00069   float *myGdalScanData = (float*) CPLMalloc(sizeof(uint)*myXDimInt * sizeof(uint)*myYDimInt);
00070   CPLErr myResultCPLerr = myGdalBand->RasterIO(GF_Read, 0, 0, myXDimInt, myYDimInt, myGdalScanData, myXDimInt, myYDimInt, GDT_Float32, 0, 0 );
00071   if(myResultCPLerr != CE_None)
00072   {
00073     emit error ("OmgMeridianSwitcher::Error reading from gdal dataset: "+ theOutputFileString);
00074     return false;
00075   }
00076   for (int myColumnInt = 0; myColumnInt < myYDimInt; myColumnInt++)
00077   {
00078     //grab the second half of this row and write to disk
00079     for (int myRowInt =myXDimInt/2; myRowInt < myXDimInt; myRowInt++)
00080     {
00081       float myFloat=myGdalScanData[myColumnInt*myXDimInt + myRowInt];
00082       //write the number to the file
00083       *myTextStream << myFloat << mySeperatorString;
00084     }
00085     //grab the first half of this row and write to disk
00086     for (int myRowInt =0; myRowInt < (myXDimInt/2); myRowInt++)
00087     {
00088       float myFloat=myGdalScanData[myColumnInt*myXDimInt + myRowInt];
00089       //write the number to the file
00090       *myTextStream << myFloat << mySeperatorString;
00091     }
00092     *myTextStream << "\n";
00093   }
00094   //draw with the experimental transaparency support
00095   CPLFree(myGdalScanData);
00096   GDALClose(gdalDataset);
00097   myFilePointer->close();
00098   delete myTextStream;
00099   delete myFilePointer;
00100 
00101   return true ;
00102 }

Generated on Mon Apr 28 15:08:50 2008 for openModellerDesktop by  doxygen 1.4.1-20050210