QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgstininterpolator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstininterpolator.cpp
3 ----------------------
4 begin : March 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 "qgstininterpolator.h"
19
22#include "NormVecDecorator.h"
23#include "qgscurve.h"
24#include "qgscurvepolygon.h"
26#include "qgsfeature.h"
27#include "qgsfeatureiterator.h"
28#include "qgsfeedback.h"
29#include "qgsgeometry.h"
30#include "qgsmulticurve.h"
31#include "qgsmultisurface.h"
32#include "qgspoint.h"
33#include "qgsvariantutils.h"
34#include "qgsvectorlayer.h"
35
36QgsTinInterpolator::QgsTinInterpolator( const QList<LayerData> &inputData, QgsTinInterpolator::TinInterpolation interpolation, QgsFeedback *feedback )
37 : QgsInterpolator( inputData )
38 , mFeedback( feedback )
39 , mInterpolation( interpolation )
40{}
41
43{
44 delete mTriangulation;
45 delete mTriangleInterpolator;
46}
47
48int QgsTinInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback * )
49{
50 if ( !mIsInitialized )
51 {
52 initialize();
53 }
54
55 if ( !mTriangleInterpolator )
56 {
57 return 1;
58 }
59
60 QgsPoint r( 0, 0, 0 );
61 if ( !mTriangleInterpolator->calcPoint( x, y, r ) )
62 {
63 return 2;
64 }
65 result = r.z();
66 return 0;
67}
68
73
75{
76 mTriangulationSink = sink;
77}
78
79void QgsTinInterpolator::initialize()
80{
81 QgsDualEdgeTriangulation *dualEdgeTriangulation = new QgsDualEdgeTriangulation( 100000 );
83 {
85 dec->addTriangulation( dualEdgeTriangulation );
86 mTriangulation = dec;
87 }
88 else
89 {
90 mTriangulation = dualEdgeTriangulation;
91 }
92
93 //get number of features if we use a progress bar
94 long long nFeatures = 0;
95 long long nProcessedFeatures = 0;
96 if ( mFeedback )
97 {
98 for ( const LayerData &layer : std::as_const( mLayerData ) )
99 {
100 if ( layer.source )
101 {
102 nFeatures += layer.source->featureCount();
103 }
104 }
105 }
106
107 const QgsCoordinateReferenceSystem crs = !mLayerData.empty() ? mLayerData.at( 0 ).source->sourceCrs() : QgsCoordinateReferenceSystem();
108
109 QgsFeature f;
110 for ( const LayerData &layer : std::as_const( mLayerData ) )
111 {
112 if ( layer.source )
113 {
114 QgsAttributeList attList;
115 switch ( layer.valueSource )
116 {
118 attList.push_back( layer.interpolationAttribute );
119 break;
120
123 break;
124 }
125
126 QgsFeatureIterator fit = layer.source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attList ).setDestinationCrs( crs, layer.transformContext ) );
127
128 while ( fit.nextFeature( f ) )
129 {
130 if ( mFeedback )
131 {
132 if ( mFeedback->isCanceled() )
133 {
134 break;
135 }
136 if ( nFeatures > 0 )
137 mFeedback->setProgress( 100.0 * static_cast<double>( nProcessedFeatures ) / nFeatures );
138 }
139 insertData( f, layer.valueSource, layer.interpolationAttribute, layer.sourceType );
140 ++nProcessedFeatures;
141 }
142 }
143 }
144
146 {
147 NormVecDecorator *dec = dynamic_cast<NormVecDecorator *>( mTriangulation );
148 if ( dec )
149 {
150 auto ctInterpolator = std::make_unique<CloughTocherInterpolator>();
151 dec->estimateFirstDerivatives( mFeedback );
152 ctInterpolator->setTriangulation( dec );
153 mTriangleInterpolator = ctInterpolator.release();
154 dec->setTriangleInterpolator( mTriangleInterpolator );
155 }
156 }
157 else //linear
158 {
159 mTriangleInterpolator = new LinTriangleInterpolator( dualEdgeTriangulation );
160 }
161 mIsInitialized = true;
162
163 //debug
164 if ( mTriangulationSink )
165 {
166 dualEdgeTriangulation->saveTriangulation( mTriangulationSink, mFeedback );
167 }
168}
169
170int QgsTinInterpolator::insertData( const QgsFeature &f, QgsInterpolator::ValueSource source, int attr, QgsInterpolator::SourceType type )
171{
172 QgsGeometry g = f.geometry();
173 if ( g.isNull() || g.isEmpty() )
174 {
175 return 2;
176 }
177
178 //check attribute value
179 double attributeValue = 0;
180 bool attributeConversionOk = false;
181 switch ( source )
182 {
184 {
185 QVariant attributeVariant = f.attribute( attr );
186 if ( QgsVariantUtils::isNull( attributeVariant ) ) //attribute not found, something must be wrong (e.g. NULL value)
187 {
188 return 3;
189 }
190 attributeValue = attributeVariant.toDouble( &attributeConversionOk );
191 if ( !attributeConversionOk || std::isnan( attributeValue ) ) //don't consider vertices with attributes like 'nan' for the interpolation
192 {
193 return 4;
194 }
195 break;
196 }
197
199 if ( !g.constGet()->isMeasure() )
200 return 3;
201 else
202 break;
203
205 if ( !g.constGet()->is3D() )
206 return 3;
207 else
208 break;
209 }
210
211 switch ( type )
212 {
214 {
215 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
216 return -1;
217 break;
218 }
219
222 {
223 switch ( QgsWkbTypes::geometryType( g.wkbType() ) )
224 {
226 {
227 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
228 return -1;
229 break;
230 }
231
234 {
235 // need to extract all rings from input geometry
236 std::vector<const QgsCurve *> curves;
238 {
239 std::vector<const QgsCurvePolygon *> polygons;
240 if ( g.isMultipart() )
241 {
242 const QgsMultiSurface *ms = qgsgeometry_cast<const QgsMultiSurface *>( g.constGet() );
243 for ( int i = 0; i < ms->numGeometries(); ++i )
244 {
245 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( ms->geometryN( i ) ) );
246 }
247 }
248 else
249 {
250 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( g.constGet() ) );
251 }
252
253 for ( const QgsCurvePolygon *polygon : polygons )
254 {
255 if ( !polygon )
256 continue;
257
258 if ( polygon->exteriorRing() )
259 curves.emplace_back( polygon->exteriorRing() );
260
261 for ( int i = 0; i < polygon->numInteriorRings(); ++i )
262 {
263 curves.emplace_back( polygon->interiorRing( i ) );
264 }
265 }
266 }
267 else
268 {
269 if ( g.isMultipart() )
270 {
271 const QgsMultiCurve *mc = qgsgeometry_cast<const QgsMultiCurve *>( g.constGet() );
272 for ( int i = 0; i < mc->numGeometries(); ++i )
273 {
274 curves.emplace_back( mc->curveN( i ) );
275 }
276 }
277 else
278 {
279 curves.emplace_back( qgsgeometry_cast<const QgsCurve *>( g.constGet() ) );
280 }
281 }
282
283 for ( const QgsCurve *curve : curves )
284 {
285 if ( !curve )
286 continue;
287
288 QgsPointSequence linePoints;
289 curve->points( linePoints );
290 for ( QgsPoint &point : linePoints )
291 {
292 switch ( source )
293 {
295 if ( point.is3D() )
296 point.setZ( attributeValue );
297 else
298 point.addZValue( attributeValue );
299 break;
300
302 if ( point.is3D() )
303 point.setZ( point.m() );
304 else
305 point.addZValue( point.m() );
306 break;
307
309 break;
310 }
311 }
312 mTriangulation->addLine( linePoints, type );
313 }
314 break;
315 }
318 break;
319 }
320 break;
321 }
322 }
323
324 return 0;
325}
326
327
328int QgsTinInterpolator::addPointsFromGeometry( const QgsGeometry &g, QgsInterpolator::ValueSource source, double attributeValue )
329{
330 // loop through all vertices and add to triangulation
331 for ( auto point = g.vertices_begin(); point != g.vertices_end(); ++point )
332 {
333 QgsPoint p = *point;
334 double z = 0;
335 switch ( source )
336 {
338 z = attributeValue;
339 break;
340
342 z = p.z();
343 break;
344
346 z = p.m();
347 break;
348 }
349 if ( mTriangulation->addPoint( QgsPoint( p.x(), p.y(), z ) ) == -100 )
350 {
351 return -1;
352 }
353 }
354 return 0;
355}
Decorator class which adds the functionality of estimating normals at the data points.
void setTriangleInterpolator(TriangleInterpolator *inter) override
Sets an interpolator.
bool estimateFirstDerivatives(QgsFeedback *feedback=nullptr)
This method adds the functionality of estimating normals at the data points. Return true in the case ...
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
@ Null
No geometry.
Definition qgis.h:384
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
DualEdgeTriangulation is an implementation of a triangulation class based on the dual edge data struc...
bool saveTriangulation(QgsFeatureSink *sink, QgsFeedback *feedback=nullptr) const override
Saves the triangulation features to a feature sink.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
An interface for objects which accept features via addFeature(s) methods.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Container of fields for a vector layer.
Definition qgsfields.h:46
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
QgsInterpolator(const QList< QgsInterpolator::LayerData > &layerData)
ValueSource
Source for interpolated values from features.
@ Z
Use feature's geometry Z values for interpolation.
@ M
Use feature's geometry M values for interpolation.
@ Attribute
Take value from feature's attribute.
SourceType
Describes the type of input data.
@ StructureLines
Structure lines.
QList< LayerData > mLayerData
Information about the input vector layers and the attributes (or z-values) that are used for interpol...
QgsCurve * curveN(int index)
Returns the curve with the specified index.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double z
Definition qgspoint.h:58
double x
Definition qgspoint.h:56
double m
Definition qgspoint.h:59
double y
Definition qgspoint.h:57
QgsTinInterpolator(const QList< QgsInterpolator::LayerData > &inputData, QgsTinInterpolator::TinInterpolation interpolation=QgsTinInterpolator::TinInterpolation::Linear, QgsFeedback *feedback=nullptr)
Constructor for QgsTinInterpolator.
int interpolatePoint(double x, double y, double &result, QgsFeedback *feedback) override
Calculates interpolation value for map coordinates x, y.
static QgsFields triangulationFields()
Returns the fields output by features when saving the triangulation.
void setTriangulationSink(QgsFeatureSink *sink)
Sets the optional sink for saving the triangulation features.
TinInterpolation
Indicates the type of interpolation to be performed.
@ CloughTocher
Clough-Tocher interpolation.
static QgsFields triangulationFields()
Returns the fields output by features when calling saveTriangulation().
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
virtual void addTriangulation(QgsTriangulation *t)
Adds an association to a triangulation.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsPoint > QgsPointSequence
QList< int > QgsAttributeList
Definition qgsfield.h:30
A source together with the information about interpolation attribute / z-coordinate interpolation and...