00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 ), mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
00025 {
00026
00027 }
00028
00029 QgsGridFileWriter::QgsGridFileWriter(): mInterpolator( 0 )
00030 {
00031
00032 }
00033
00034 QgsGridFileWriter::~QgsGridFileWriter()
00035 {
00036
00037 }
00038
00039 int QgsGridFileWriter::writeFile( bool showProgressDialog )
00040 {
00041 QFile outputFile( mOutputFilePath );
00042
00043 if ( !outputFile.open( QFile::WriteOnly ) )
00044 {
00045 return 1;
00046 }
00047
00048 if ( !mInterpolator )
00049 {
00050 outputFile.remove();
00051 return 2;
00052 }
00053
00054 QTextStream outStream( &outputFile );
00055 outStream.setRealNumberPrecision( 8 );
00056 writeHeader( outStream );
00057
00058 double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0;
00059 double currentXValue;
00060 double interpolatedValue;
00061
00062 QProgressDialog* progressDialog = 0;
00063 if ( showProgressDialog )
00064 {
00065 progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
00066 progressDialog->setWindowModality( Qt::WindowModal );
00067 }
00068
00069 for ( int i = 0; i < mNumRows; ++i )
00070 {
00071 currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0;
00072 for ( int j = 0; j < mNumColumns; ++j )
00073 {
00074 if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
00075 {
00076 outStream << interpolatedValue << " ";
00077 }
00078 else
00079 {
00080 outStream << "-9999 ";
00081 }
00082 currentXValue += mCellSizeX;
00083 }
00084 outStream << endl;
00085 currentYValue -= mCellSizeY;
00086
00087 if ( showProgressDialog )
00088 {
00089 if ( progressDialog->wasCanceled() )
00090 {
00091 outputFile.remove();
00092 return 3;
00093 }
00094 progressDialog->setValue( i );
00095 }
00096 }
00097
00098 delete progressDialog;
00099 return 0;
00100 }
00101
00102 int QgsGridFileWriter::writeHeader( QTextStream& outStream )
00103 {
00104 outStream << "NCOLS " << mNumColumns << endl;
00105 outStream << "NROWS " << mNumRows << endl;
00106 outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
00107 outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
00108 if ( mCellSizeX == mCellSizeY )
00109 {
00110 outStream << "CELLSIZE " << mCellSizeX << endl;
00111 }
00112 else
00113 {
00114 outStream << "DX " << mCellSizeX << endl;
00115 outStream << "DY " << mCellSizeY << endl;
00116 }
00117 outStream << "NODATA_VALUE -9999" << endl;
00118
00119 return 0;
00120 }