/
qgsgridfilewriter.cpp
120 lines (102 loc) · 3.64 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
/***************************************************************************
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 <QFile>
#include <QProgressDialog>
QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator* i, QString outputPath, QgsRect extent, int nCols, int nRows ): mInterpolator( i ), mOutputFilePath( outputPath ), mInterpolationExtent( extent ), mNumColumns( nCols ), mNumRows( nRows )
{
mCellSizeX = ( mInterpolationExtent.xMax() - mInterpolationExtent.xMin() ) / mNumColumns;
mCellSizeY = ( mInterpolationExtent.yMax() - mInterpolationExtent.yMin() ) / mNumRows;
}
QgsGridFileWriter::QgsGridFileWriter(): mInterpolator( 0 )
{
}
QgsGridFileWriter::~QgsGridFileWriter()
{
}
int QgsGridFileWriter::writeFile( bool showProgressDialog )
{
QFile outputFile( mOutputFilePath );
if ( !outputFile.open( QFile::WriteOnly ) )
{
return 1;
}
if ( !mInterpolator )
{
outputFile.remove();
return 2;
}
QTextStream outStream( &outputFile );
outStream.setRealNumberPrecision(8);
writeHeader( outStream );
double currentYValue = mInterpolationExtent.yMax();
double currentXValue;
double interpolatedValue;
QProgressDialog* progressDialog = 0;
if ( showProgressDialog )
{
progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
progressDialog->setWindowModality( Qt::WindowModal );
}
for ( int i = 0; i < mNumRows; ++i )
{
currentXValue = mInterpolationExtent.xMin();
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 ( showProgressDialog )
{
if ( progressDialog->wasCanceled() )
{
outputFile.remove();
return 3;
}
progressDialog->setValue( i );
}
}
delete progressDialog;
return 0;
}
int QgsGridFileWriter::writeHeader( QTextStream& outStream )
{
outStream << "NCOLS " << mNumColumns << endl;
outStream << "NROWS " << mNumRows << endl;
outStream << "XLLCORNER " << mInterpolationExtent.xMin() << endl;
outStream << "YLLCORNER " << mInterpolationExtent.yMin() << 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;
}