Quantum GIS API Documentation  1.8
src/analysis/interpolation/qgsgridfilewriter.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgsgridfilewriter.cpp
00003                               ---------------------
00004   begin                : Marco 10, 2008
00005   copyright            : (C) 2008 by Marco Hugentobler
00006   email                : marco dot hugentobler at karto dot baug dot ethz dot ch
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsgridfilewriter.h"
00019 #include "qgsinterpolator.h"
00020 #include <QFile>
00021 #include <QProgressDialog>
00022 
00023 QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows , double cellSizeX, double cellSizeY )
00024     : mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows )
00025     , mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
00026 {
00027 
00028 }
00029 
00030 QgsGridFileWriter::QgsGridFileWriter(): mInterpolator( 0 )
00031 {
00032 
00033 }
00034 
00035 QgsGridFileWriter::~QgsGridFileWriter()
00036 {
00037 
00038 }
00039 
00040 int QgsGridFileWriter::writeFile( bool showProgressDialog )
00041 {
00042   QFile outputFile( mOutputFilePath );
00043 
00044   if ( !outputFile.open( QFile::WriteOnly ) )
00045   {
00046     return 1;
00047   }
00048 
00049   if ( !mInterpolator )
00050   {
00051     outputFile.remove();
00052     return 2;
00053   }
00054 
00055   QTextStream outStream( &outputFile );
00056   outStream.setRealNumberPrecision( 8 );
00057   writeHeader( outStream );
00058 
00059   double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
00060   double currentXValue;
00061   double interpolatedValue;
00062 
00063   QProgressDialog* progressDialog = 0;
00064   if ( showProgressDialog )
00065   {
00066     progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
00067     progressDialog->setWindowModality( Qt::WindowModal );
00068   }
00069 
00070   for ( int i = 0; i < mNumRows; ++i )
00071   {
00072     currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
00073     for ( int j = 0; j < mNumColumns; ++j )
00074     {
00075       if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
00076       {
00077         outStream << interpolatedValue << " ";
00078       }
00079       else
00080       {
00081         outStream << "-9999 ";
00082       }
00083       currentXValue += mCellSizeX;
00084     }
00085     outStream << endl;
00086     currentYValue -= mCellSizeY;
00087 
00088     if ( showProgressDialog )
00089     {
00090       if ( progressDialog->wasCanceled() )
00091       {
00092         outputFile.remove();
00093         return 3;
00094       }
00095       progressDialog->setValue( i );
00096     }
00097   }
00098 
00099   delete progressDialog;
00100   return 0;
00101 }
00102 
00103 int QgsGridFileWriter::writeHeader( QTextStream& outStream )
00104 {
00105   outStream << "NCOLS " << mNumColumns << endl;
00106   outStream << "NROWS " << mNumRows << endl;
00107   outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
00108   outStream << "YLLCORNER " <<  mInterpolationExtent.yMinimum() << endl;
00109   if ( mCellSizeX == mCellSizeY ) //standard way
00110   {
00111     outStream << "CELLSIZE " << mCellSizeX << endl;
00112   }
00113   else //this is supported by GDAL but probably not by other products
00114   {
00115     outStream << "DX " << mCellSizeX << endl;
00116     outStream << "DY " << mCellSizeY << endl;
00117   }
00118   outStream << "NODATA_VALUE -9999" << endl;
00119 
00120   return 0;
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines