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