QGIS API Documentation  2.99.0-Master (53aba61)
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, 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  : mNumColumns( 0 )
39  , mNumRows( 0 )
40  , mCellSizeX( 0 )
41  , mCellSizeY( 0 )
42 {
43 
44 }
45 
47 {
48  QFile outputFile( mOutputFilePath );
49 
50  if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
51  {
52  return 1;
53  }
54 
55  if ( !mInterpolator )
56  {
57  outputFile.remove();
58  return 2;
59  }
60 
61  QTextStream outStream( &outputFile );
62  outStream.setRealNumberPrecision( 8 );
63  writeHeader( outStream );
64 
65  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
66  double currentXValue;
67  double interpolatedValue;
68 
69  for ( int i = 0; i < mNumRows; ++i )
70  {
71  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
72  for ( int j = 0; j < mNumColumns; ++j )
73  {
74  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
75  {
76  outStream << interpolatedValue << ' ';
77  }
78  else
79  {
80  outStream << "-9999 ";
81  }
82  currentXValue += mCellSizeX;
83  }
84  outStream << endl;
85  currentYValue -= mCellSizeY;
86 
87  if ( feedback )
88  {
89  if ( feedback->isCanceled() )
90  {
91  outputFile.remove();
92  return 3;
93  }
94  feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) );
95  }
96  }
97 
98  // create prj file
100  ld = mInterpolator->layerData().at( 0 );
101  QgsVectorLayer *vl = ld.vectorLayer;
102  QString crs = vl->crs().toWkt();
103  QFileInfo fi( mOutputFilePath );
104  QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
105  QFile prjFile( fileName );
106  if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
107  {
108  return 1;
109  }
110  QTextStream prjStream( &prjFile );
111  prjStream << crs;
112  prjStream << endl;
113  prjFile.close();
114 
115  return 0;
116 }
117 
118 int QgsGridFileWriter::writeHeader( QTextStream &outStream )
119 {
120  outStream << "NCOLS " << mNumColumns << endl;
121  outStream << "NROWS " << mNumRows << endl;
122  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
123  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
124  if ( mCellSizeX == mCellSizeY ) //standard way
125  {
126  outStream << "CELLSIZE " << mCellSizeX << endl;
127  }
128  else //this is supported by GDAL but probably not by other products
129  {
130  outStream << "DX " << mCellSizeX << endl;
131  outStream << "DY " << mCellSizeY << endl;
132  }
133  outStream << "NODATA_VALUE -9999" << endl;
134 
135  return 0;
136 }
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Interface class for interpolations.
QgsVectorLayer * vectorLayer
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
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:43
QList< LayerData > layerData() const
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:111
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)
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
QString toWkt() const
Returns a WKT representation of this CRS.
virtual int interpolatePoint(double x, double y, double &result)=0
Calculates interpolation value for map coordinates x, y.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:101
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:106
Represents a vector layer which manages a vector based data sets.