/
qgsinterpolationlayerbuilder.cpp
153 lines (133 loc) · 5.05 KB
/
qgsinterpolationlayerbuilder.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***************************************************************************
qgsinterpolationlayerbuilder.cpp
--------------------------------
begin : July, 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 "qgsinterpolationlayerbuilder.h"
#include "qgslogger.h"
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QDomElement>
#include <QTemporaryFile>
//for interpolation
#include "qgsgridfilewriter.h"
#include "qgsidwinterpolator.h"
#include "qgstininterpolator.h"
QgsInterpolationLayerBuilder::QgsInterpolationLayerBuilder( QgsVectorLayer *vl ): mVectorLayer( vl )
{
}
QgsInterpolationLayerBuilder::QgsInterpolationLayerBuilder(): mVectorLayer( nullptr )
{
}
QgsMapLayer *QgsInterpolationLayerBuilder::createMapLayer( const QDomElement &elem,
const QString &layerName,
QList<QTemporaryFile *> &filesToRemove,
QList<QgsMapLayer *> &layersToRemove,
bool allowCaching ) const
{
Q_UNUSED( layerName );
Q_UNUSED( allowCaching );
if ( !mVectorLayer )
{
return nullptr;
}
QDomNodeList interpolationList = elem.elementsByTagName( QStringLiteral( "Interpolation" ) );
if ( interpolationList.isEmpty() )
{
QgsDebugMsg( "No Interpolation element found" );
return nullptr;
}
QDomElement interpolationElem = interpolationList.at( 0 ).toElement();
//create QgsInterpolator object from XML
QDomNodeList tinList = interpolationElem.elementsByTagName( QStringLiteral( "TINMethod" ) );
QDomNodeList idwList = interpolationElem.elementsByTagName( QStringLiteral( "IDWMethod" ) );
QgsInterpolator *interpolator = nullptr;
QList<QgsInterpolator::LayerData> layerDataList;
QgsInterpolator::LayerData currentLayerData;
currentLayerData.vectorLayer = mVectorLayer;
QDomNodeList propertyNameList = interpolationElem.elementsByTagName( QStringLiteral( "PropertyName" ) );
if ( propertyNameList.isEmpty() )
{
currentLayerData.zCoordInterpolation = true;
}
else
{
currentLayerData.zCoordInterpolation = false;
//set attribute field interpolation or z-Coordinate interpolation
QString attributeName = propertyNameList.at( 0 ).toElement().text();
QgsVectorDataProvider *provider = mVectorLayer->dataProvider();
if ( !provider )
{
return nullptr;
}
int attributeIndex = provider->fieldNameIndex( attributeName );
if ( attributeIndex == -1 )
{
return nullptr; //attribute field not found
}
currentLayerData.interpolationAttribute = attributeIndex;
}
currentLayerData.mInputType = QgsInterpolator::POINTS;
layerDataList.push_back( currentLayerData );
if ( !idwList.isEmpty() ) //inverse distance interpolator
{
interpolator = new QgsIDWInterpolator( layerDataList );
//todo: parse <DistanceWeightingCoefficient>
}
else //tin is default
{
interpolator = new QgsTINInterpolator( layerDataList );
//todo: parse <InterpolationFunction>
}
//Resolution
int nCols, nRows;
QDomNodeList resolutionNodeList = elem.elementsByTagName( QStringLiteral( "Resolution" ) );
if ( resolutionNodeList.isEmpty() )
{
//use default values...
nCols = 100;
nRows = 100;
}
else
{
QDomElement resolutionElem = resolutionNodeList.at( 0 ).toElement();
nCols = resolutionElem.attribute( QStringLiteral( "ncols" ) ).toInt();
nRows = resolutionElem.attribute( QStringLiteral( "nrows" ) ).toInt();
if ( nCols == 0 || nRows == 0 )
{
QgsDebugMsg( "Reading of resolution failed" );
delete interpolator;
return nullptr;
}
}
QTemporaryFile *tmpFile = new QTemporaryFile();
if ( !tmpFile->open() )
{
QgsDebugMsg( "Opening temporary file failed" );
delete tmpFile;
delete interpolator;
return nullptr;
}
QgsRectangle extent = mVectorLayer->extent();
QgsGridFileWriter gridWriter( interpolator, tmpFile->fileName(), extent, nCols, nRows, extent.width() / nCols, extent.height() / nRows );
if ( gridWriter.writeFile() != 0 )
{
QgsDebugMsg( "Interpolation of raster failed" );
return nullptr;
}
filesToRemove.push_back( tmpFile ); //store raster in temporary file and remove after request
QgsRasterLayer *raster = new QgsRasterLayer( tmpFile->fileName() );
layersToRemove.push_back( raster );
return raster;
}