QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsgridfilewriter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgridfilewriter.cpp
3  ---------------------
4  begin : Marco 10, 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgridfilewriter.h"
19 #include "qgsinterpolator.h"
20 #include <QFile>
21 #include <QProgressDialog>
22 
23 QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows , double cellSizeX, double cellSizeY )
24  : mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows )
25  , mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
26 {
27 
28 }
29 
31 {
32 
33 }
34 
36 {
37 
38 }
39 
40 int QgsGridFileWriter::writeFile( bool showProgressDialog )
41 {
42  QFile outputFile( mOutputFilePath );
43 
44  if ( !outputFile.open( QFile::WriteOnly ) )
45  {
46  return 1;
47  }
48 
49  if ( !mInterpolator )
50  {
51  outputFile.remove();
52  return 2;
53  }
54 
55  QTextStream outStream( &outputFile );
56  outStream.setRealNumberPrecision( 8 );
57  writeHeader( outStream );
58 
59  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
60  double currentXValue;
61  double interpolatedValue;
62 
63  QProgressDialog* progressDialog = 0;
64  if ( showProgressDialog )
65  {
66  progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
67  progressDialog->setWindowModality( Qt::WindowModal );
68  }
69 
70  for ( int i = 0; i < mNumRows; ++i )
71  {
72  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
73  for ( int j = 0; j < mNumColumns; ++j )
74  {
75  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
76  {
77  outStream << interpolatedValue << " ";
78  }
79  else
80  {
81  outStream << "-9999 ";
82  }
83  currentXValue += mCellSizeX;
84  }
85  outStream << endl;
86  currentYValue -= mCellSizeY;
87 
88  if ( showProgressDialog )
89  {
90  if ( progressDialog->wasCanceled() )
91  {
92  outputFile.remove();
93  return 3;
94  }
95  progressDialog->setValue( i );
96  }
97  }
98 
99  delete progressDialog;
100  return 0;
101 }
102 
103 int QgsGridFileWriter::writeHeader( QTextStream& outStream )
104 {
105  outStream << "NCOLS " << mNumColumns << endl;
106  outStream << "NROWS " << mNumRows << endl;
107  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
108  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
109  if ( mCellSizeX == mCellSizeY ) //standard way
110  {
111  outStream << "CELLSIZE " << mCellSizeX << endl;
112  }
113  else //this is supported by GDAL but probably not by other products
114  {
115  outStream << "DX " << mCellSizeX << endl;
116  outStream << "DY " << mCellSizeY << endl;
117  }
118  outStream << "NODATA_VALUE -9999" << endl;
119 
120  return 0;
121 }