-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgridfilewriter.cpp
137 lines (120 loc) · 4.08 KB
/
qgsgridfilewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************************
qgsgridfilewriter.cpp
---------------------
begin : Marco 10, 2008
copyright : (C) 2008 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsgridfilewriter.h"
#include "qgsinterpolator.h"
#include "qgsvectorlayer.h"
#include "qgsfeedback.h"
#include <QFile>
#include <QFileInfo>
QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, double cellSizeX, double cellSizeY )
: mInterpolator( i )
, mOutputFilePath( outputPath )
, mInterpolationExtent( extent )
, mNumColumns( nCols )
, mNumRows( nRows )
, mCellSizeX( cellSizeX )
, mCellSizeY( cellSizeY )
{
}
QgsGridFileWriter::QgsGridFileWriter()
: mInterpolator( nullptr )
, mNumColumns( 0 )
, mNumRows( 0 )
, mCellSizeX( 0 )
, mCellSizeY( 0 )
{
}
int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
{
QFile outputFile( mOutputFilePath );
if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
return 1;
}
if ( !mInterpolator )
{
outputFile.remove();
return 2;
}
QTextStream outStream( &outputFile );
outStream.setRealNumberPrecision( 8 );
writeHeader( outStream );
double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
double currentXValue;
double interpolatedValue;
for ( int i = 0; i < mNumRows; ++i )
{
currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
for ( int j = 0; j < mNumColumns; ++j )
{
if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
{
outStream << interpolatedValue << ' ';
}
else
{
outStream << "-9999 ";
}
currentXValue += mCellSizeX;
}
outStream << endl;
currentYValue -= mCellSizeY;
if ( feedback )
{
if ( feedback->isCanceled() )
{
outputFile.remove();
return 3;
}
feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) );
}
}
// create prj file
QgsInterpolator::LayerData ld;
ld = mInterpolator->layerData().at( 0 );
QgsVectorLayer *vl = ld.vectorLayer;
QString crs = vl->crs().toWkt();
QFileInfo fi( mOutputFilePath );
QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
QFile prjFile( fileName );
if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
return 1;
}
QTextStream prjStream( &prjFile );
prjStream << crs;
prjStream << endl;
prjFile.close();
return 0;
}
int QgsGridFileWriter::writeHeader( QTextStream &outStream )
{
outStream << "NCOLS " << mNumColumns << endl;
outStream << "NROWS " << mNumRows << endl;
outStream << "XLLCORNER " << mInterpolationExtent.xMinimum() << endl;
outStream << "YLLCORNER " << mInterpolationExtent.yMinimum() << endl;
if ( mCellSizeX == mCellSizeY ) //standard way
{
outStream << "CELLSIZE " << mCellSizeX << endl;
}
else //this is supported by GDAL but probably not by other products
{
outStream << "DX " << mCellSizeX << endl;
outStream << "DY " << mCellSizeY << endl;
}
outStream << "NODATA_VALUE -9999" << endl;
return 0;
}