Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 ), mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
25 {
26 
27 }
28 
30 {
31 
32 }
33 
35 {
36 
37 }
38 
39 int QgsGridFileWriter::writeFile( bool showProgressDialog )
40 {
41  QFile outputFile( mOutputFilePath );
42 
43  if ( !outputFile.open( QFile::WriteOnly ) )
44  {
45  return 1;
46  }
47 
48  if ( !mInterpolator )
49  {
50  outputFile.remove();
51  return 2;
52  }
53 
54  QTextStream outStream( &outputFile );
55  outStream.setRealNumberPrecision( 8 );
56  writeHeader( outStream );
57 
58  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
59  double currentXValue;
60  double interpolatedValue;
61 
62  QProgressDialog* progressDialog = 0;
63  if ( showProgressDialog )
64  {
65  progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
66  progressDialog->setWindowModality( Qt::WindowModal );
67  }
68 
69  for ( int i = 0; i < mNumRows; ++i )
70  {
71  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
72  for ( int j = 0; j < mNumColumns; ++j )
73  {
74  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
75  {
76  outStream << interpolatedValue << " ";
77  }
78  else
79  {
80  outStream << "-9999 ";
81  }
82  currentXValue += mCellSizeX;
83  }
84  outStream << endl;
85  currentYValue -= mCellSizeY;
86 
87  if ( showProgressDialog )
88  {
89  if ( progressDialog->wasCanceled() )
90  {
91  outputFile.remove();
92  return 3;
93  }
94  progressDialog->setValue( i );
95  }
96  }
97 
98  delete progressDialog;
99  return 0;
100 }
101 
102 int QgsGridFileWriter::writeHeader( QTextStream& outStream )
103 {
104  outStream << "NCOLS " << mNumColumns << endl;
105  outStream << "NROWS " << mNumRows << endl;
106  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
107  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
108  if ( mCellSizeX == mCellSizeY ) //standard way
109  {
110  outStream << "CELLSIZE " << mCellSizeX << endl;
111  }
112  else //this is supported by GDAL but probably not by other products
113  {
114  outStream << "DX " << mCellSizeX << endl;
115  outStream << "DY " << mCellSizeY << endl;
116  }
117  outStream << "NODATA_VALUE -9999" << endl;
118 
119  return 0;
120 }