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