QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
52  outStream.setCodec( "UTF-8" );
53 #endif
54  outStream.setRealNumberPrecision( 8 );
55  writeHeader( outStream );
56 
57  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
58  double currentXValue;
59  double interpolatedValue;
60 
61  for ( int i = 0; i < mNumRows; ++i )
62  {
63  currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
64  for ( int j = 0; j < mNumColumns; ++j )
65  {
66  if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
67  {
68  outStream << interpolatedValue << ' ';
69  }
70  else
71  {
72  outStream << "-9999 ";
73  }
74  currentXValue += mCellSizeX;
75  }
76 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
77  outStream << endl;
78 #else
79  outStream << Qt::endl;
80 #endif
81  currentYValue -= mCellSizeY;
82 
83  if ( feedback )
84  {
85  if ( feedback->isCanceled() )
86  {
87  outputFile.remove();
88  return 3;
89  }
90  feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) );
91  }
92  }
93 
94  // create prj file
96  ld = mInterpolator->layerData().at( 0 );
97  QgsFeatureSource *source = ld.source;
98  const QString crs = source->sourceCrs().toWkt();
99  const QFileInfo fi( mOutputFilePath );
100  const QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
101  QFile prjFile( fileName );
102  if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
103  {
104  return 1;
105  }
106  QTextStream prjStream( &prjFile );
107  prjStream << crs;
108 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
109  prjStream << endl;
110 #else
111  prjStream << Qt::endl;
112 #endif
113  prjFile.close();
114 
115  return 0;
116 }
117 
118 int QgsGridFileWriter::writeHeader( QTextStream &outStream )
119 {
120 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
121  outStream << "NCOLS " << mNumColumns << endl;
122  outStream << "NROWS " << mNumRows << endl;
123  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
124  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
125 #else
126  outStream << "NCOLS " << mNumColumns << Qt::endl;
127  outStream << "NROWS " << mNumRows << Qt::endl;
128  outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << Qt::endl;
129  outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << Qt::endl;
130 #endif
131  if ( mCellSizeX == mCellSizeY ) //standard way
132  {
133 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
134  outStream << "CELLSIZE " << mCellSizeX << endl;
135 #else
136  outStream << "CELLSIZE " << mCellSizeX << Qt::endl;
137 #endif
138  }
139  else //this is supported by GDAL but probably not by other products
140  {
141 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
142  outStream << "DX " << mCellSizeX << endl;
143  outStream << "DY " << mCellSizeY << endl;
144 #else
145  outStream << "DX " << mCellSizeX << Qt::endl;
146  outStream << "DY " << mCellSizeY << Qt::endl;
147 #endif
148  }
149 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
150  outStream << "NODATA_VALUE -9999" << endl;
151 #else
152  outStream << "NODATA_VALUE -9999" << Qt::endl;
153 #endif
154  return 0;
155 }
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
An interface for objects which provide features via a getFeatures method.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
QgsGridFileWriter(QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows)
Constructor for QgsGridFileWriter, for the specified interpolator.
int writeFile(QgsFeedback *feedback=nullptr)
Writes the grid file.
Interface class for interpolations.
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
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
const QgsCoordinateReferenceSystem & crs
A source together with the information about interpolation attribute / z-coordinate interpolation and...
QgsFeatureSource * source
Feature source.