QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgsfeedback.h"
22 #include <QFile>
23 #include <QFileInfo>
24 
25 QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows )
26  : mInterpolator( i )
27  , mOutputFilePath( outputPath )
28  , mInterpolationExtent( extent )
29  , mNumColumns( nCols )
30  , mNumRows( nRows )
31  , mCellSizeX( extent.width() / nCols )
32  , mCellSizeY( extent.height() / nRows )
33 {}
34 
36 {
37  QFile outputFile( mOutputFilePath );
38 
39  if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
40  {
41  return 1;
42  }
43 
44  if ( !mInterpolator )
45  {
46  outputFile.remove();
47  return 2;
48  }
49 
50  QTextStream outStream( &outputFile );
51  outStream.setRealNumberPrecision( 8 );
52  writeHeader( outStream );
53 
54  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
55  double currentXValue;
56  double interpolatedValue;
57 
58  for ( int i = 0; i < mNumRows; ++i )
59  {
60  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
61  for ( int j = 0; j < mNumColumns; ++j )
62  {
63  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
64  {
65  outStream << interpolatedValue << ' ';
66  }
67  else
68  {
69  outStream << "-9999 ";
70  }
71  currentXValue += mCellSizeX;
72  }
73  outStream << endl;
74  currentYValue -= mCellSizeY;
75 
76  if ( feedback )
77  {
78  if ( feedback->isCanceled() )
79  {
80  outputFile.remove();
81  return 3;
82  }
83  feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) );
84  }
85  }
86 
87  // create prj file
89  ld = mInterpolator->layerData().at( 0 );
90  QgsFeatureSource *source = ld.source;
91  QString crs = source->sourceCrs().toWkt();
92  QFileInfo fi( mOutputFilePath );
93  QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
94  QFile prjFile( fileName );
95  if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
96  {
97  return 1;
98  }
99  QTextStream prjStream( &prjFile );
100  prjStream << crs;
101  prjStream << endl;
102  prjFile.close();
103 
104  return 0;
105 }
106 
107 int QgsGridFileWriter::writeHeader( QTextStream &outStream )
108 {
109  outStream << "NCOLS " << mNumColumns << endl;
110  outStream << "NROWS " << mNumRows << endl;
111  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
112  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
113  if ( mCellSizeX == mCellSizeY ) //standard way
114  {
115  outStream << "CELLSIZE " << mCellSizeX << endl;
116  }
117  else //this is supported by GDAL but probably not by other products
118  {
119  outStream << "DX " << mCellSizeX << endl;
120  outStream << "DY " << mCellSizeY << endl;
121  }
122  outStream << "NODATA_VALUE -9999" << endl;
123 
124  return 0;
125 }
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Interface class for interpolations.
int writeFile(QgsFeedback *feedback=nullptr)
Writes the grid file.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
QgsFeatureSource * source
Feature source.
const QgsCoordinateReferenceSystem & crs
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
virtual int interpolatePoint(double x, double y, double &result, QgsFeedback *feedback=nullptr)=0
Calculates interpolation value for map coordinates x, y.
QList< LayerData > layerData() const
QgsGridFileWriter(QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows)
Constructor for QgsGridFileWriter, for the specified interpolator.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
A source together with the information about interpolation attribute / z-coordinate interpolation and...
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
An interface for objects which provide features via a getFeatures method.
QString toWkt() const
Returns a WKT representation of this CRS.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172