QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Interface class for interpolations.
QgsVectorLayer * vectorLayer
void setWindowModality(Qt::WindowModality windowModality)
bool remove()
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:192
void setRealNumberPrecision(int precision)
QString tr(const char *sourceText, const char *disambiguation, int n)
int writeFile(bool showProgressDialog=false)
Writes the grid file.
void setValue(int progress)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:197
const QList< LayerData > & layerData() const
QgsGridFileWriter(QgsInterpolator *i, QString outputPath, QgsRectangle extent, int nCols, int nRows, double cellSizeX, double cellSizeY)
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual void close()
A layer together with the information about interpolation attribute / z-coordinate interpolation and ...
virtual int interpolatePoint(double x, double y, double &result)=0
Calculates interpolation value for map coordinates x, y.
QString completeBaseName() const
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
QString absolutePath() const
Represents a vector layer which manages a vector based data sets.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:187