QGIS API Documentation  2.5.0-Master
 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 "qgsvectorlayer.h"
21 #include <QFile>
22 #include <QFileInfo>
23 #include <QProgressDialog>
24 
25 QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRectangle extent, int nCols, int nRows , double cellSizeX, double cellSizeY )
26  : mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows )
27  , mCellSizeX( cellSizeX ), mCellSizeY( cellSizeY )
28 {
29 
30 }
31 
33 {
34 
35 }
36 
38 {
39 
40 }
41 
42 int QgsGridFileWriter::writeFile( bool showProgressDialog )
43 {
44  QFile outputFile( mOutputFilePath );
45 
46  if ( !outputFile.open( QFile::WriteOnly ) )
47  {
48  return 1;
49  }
50 
51  if ( !mInterpolator )
52  {
53  outputFile.remove();
54  return 2;
55  }
56 
57  QTextStream outStream( &outputFile );
58  outStream.setRealNumberPrecision( 8 );
59  writeHeader( outStream );
60 
61  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
62  double currentXValue;
63  double interpolatedValue;
64 
65  QProgressDialog* progressDialog = 0;
66  if ( showProgressDialog )
67  {
68  progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
69  progressDialog->setWindowModality( Qt::WindowModal );
70  }
71 
72  for ( int i = 0; i < mNumRows; ++i )
73  {
74  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
75  for ( int j = 0; j < mNumColumns; ++j )
76  {
77  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
78  {
79  outStream << interpolatedValue << " ";
80  }
81  else
82  {
83  outStream << "-9999 ";
84  }
85  currentXValue += mCellSizeX;
86  }
87  outStream << endl;
88  currentYValue -= mCellSizeY;
89 
90  if ( showProgressDialog )
91  {
92  if ( progressDialog->wasCanceled() )
93  {
94  outputFile.remove();
95  return 3;
96  }
97  progressDialog->setValue( i );
98  }
99  }
100 
101  // create prj file
103  ld = mInterpolator->layerData().first();
104  QgsVectorLayer* vl = ld.vectorLayer;
105  QString crs = vl->crs().toWkt();
106  QFileInfo fi( mOutputFilePath );
107  QString fileName = fi.absolutePath() + "/" + fi.completeBaseName() + ".prj";
108  QFile prjFile( fileName );
109  if ( !prjFile.open( QFile::WriteOnly ) )
110  {
111  return 1;
112  }
113  QTextStream prjStream( &prjFile );
114  prjStream << crs;
115  prjStream << endl;
116  prjFile.close();
117 
118  delete progressDialog;
119  return 0;
120 }
121 
122 int QgsGridFileWriter::writeHeader( QTextStream& outStream )
123 {
124  outStream << "NCOLS " << mNumColumns << endl;
125  outStream << "NROWS " << mNumRows << endl;
126  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
127  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
128  if ( mCellSizeX == mCellSizeY ) //standard way
129  {
130  outStream << "CELLSIZE " << mCellSizeX << endl;
131  }
132  else //this is supported by GDAL but probably not by other products
133  {
134  outStream << "DX " << mCellSizeX << endl;
135  outStream << "DY " << mCellSizeY << endl;
136  }
137  outStream << "NODATA_VALUE -9999" << endl;
138 
139  return 0;
140 }