-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsinterpolationlayerbuilder.cpp
158 lines (136 loc) · 4.98 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
154
155
156
157
158
/***************************************************************************
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( 0 )
{
}
QgsInterpolationLayerBuilder::~QgsInterpolationLayerBuilder()
{
}
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 0;
}
QDomNodeList interpolationList = elem.elementsByTagName( "Interpolation" );
if ( interpolationList.size() < 1 )
{
QgsDebugMsg( "No Interpolation element found" );
return 0;
}
QDomElement interpolationElem = interpolationList.at( 0 ).toElement();
//create QgsInterpolator object from XML
QDomNodeList tinList = interpolationElem.elementsByTagName( "TINMethod" );
QDomNodeList idwList = interpolationElem.elementsByTagName( "IDWMethod" );
QgsInterpolator* theInterpolator = 0;
QList<QgsInterpolator::LayerData> layerDataList;
QgsInterpolator::LayerData currentLayerData;
currentLayerData.vectorLayer = mVectorLayer;
QDomNodeList propertyNameList = interpolationElem.elementsByTagName( "PropertyName" );
if ( propertyNameList.size() < 1 )
{
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 0;
}
int attributeIndex = provider->fieldNameIndex( attributeName );
if ( attributeIndex == -1 )
{
return 0; //attribute field not found
}
currentLayerData.interpolationAttribute = attributeIndex;
}
currentLayerData.mInputType = QgsInterpolator::POINTS;
layerDataList.push_back( currentLayerData );
if ( idwList.size() > 0 ) //inverse distance interpolator
{
theInterpolator = new QgsIDWInterpolator( layerDataList );
//todo: parse <DistanceWeightingCoefficient>
}
else //tin is default
{
theInterpolator = new QgsTINInterpolator( layerDataList );
//todo: parse <InterpolationFunction>
}
//Resolution
int nCols, nRows;
QDomNodeList resolutionNodeList = elem.elementsByTagName( "Resolution" );
if ( resolutionNodeList.size() < 1 )
{
//use default values...
nCols = 100;
nRows = 100;
}
else
{
QDomElement resolutionElem = resolutionNodeList.at( 0 ).toElement();
nCols = resolutionElem.attribute( "ncols" ).toInt();
nRows = resolutionElem.attribute( "nrows" ).toInt();
if ( nCols == 0 || nRows == 0 )
{
QgsDebugMsg( "Reading of resolution failed" );
delete theInterpolator;
return 0;
}
}
QTemporaryFile* tmpFile = new QTemporaryFile();
if ( !tmpFile->open() )
{
QgsDebugMsg( "Opening temporary file failed" );
delete tmpFile;
delete theInterpolator;
return 0;
}
QgsRectangle extent = mVectorLayer->extent();
QgsGridFileWriter gridWriter( theInterpolator, tmpFile->fileName(), extent, nCols, nRows, extent.width() / nCols, extent.height() / nRows );
if ( gridWriter.writeFile( false ) != 0 )
{
QgsDebugMsg( "Interpolation of raster failed" );
return 0;
}
filesToRemove.push_back( tmpFile ); //store raster in temporary file and remove after request
QgsRasterLayer* theRaster = new QgsRasterLayer( tmpFile->fileName() );
layersToRemove.push_back( theRaster );
return theRaster;
}