QGIS API Documentation 3.43.0-Master (e737cc10456)
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#include "qgsfeatureiterator.h"
22#include "NormVecDecorator.h"
24#include "qgspoint.h"
25#include "qgsfeature.h"
26#include "qgsgeometry.h"
27#include "qgsvectorlayer.h"
28#include "qgsvariantutils.h"
29#include "qgsfeedback.h"
30#include "qgscurve.h"
31#include "qgsmulticurve.h"
32#include "qgscurvepolygon.h"
33#include "qgsmultisurface.h"
34
35QgsTinInterpolator::QgsTinInterpolator( const QList<LayerData> &inputData, QgsTinInterpolator::TinInterpolation interpolation, QgsFeedback *feedback )
36 : QgsInterpolator( inputData )
37 , mIsInitialized( false )
38 , mFeedback( feedback )
39 , mInterpolation( interpolation )
40{
41}
42
44{
45 delete mTriangulation;
46 delete mTriangleInterpolator;
47}
48
49int QgsTinInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback * )
50{
51 if ( !mIsInitialized )
52 {
53 initialize();
54 }
55
56 if ( !mTriangleInterpolator )
57 {
58 return 1;
59 }
60
61 QgsPoint r( 0, 0, 0 );
62 if ( !mTriangleInterpolator->calcPoint( x, y, r ) )
63 {
64 return 2;
65 }
66 result = r.z();
67 return 0;
68}
69
74
76{
77 mTriangulationSink = sink;
78}
79
80void QgsTinInterpolator::initialize()
81{
82 QgsDualEdgeTriangulation *dualEdgeTriangulation = new QgsDualEdgeTriangulation( 100000 );
84 {
86 dec->addTriangulation( dualEdgeTriangulation );
87 mTriangulation = dec;
88 }
89 else
90 {
91 mTriangulation = dualEdgeTriangulation;
92 }
93
94 //get number of features if we use a progress bar
95 long long nFeatures = 0;
96 long long nProcessedFeatures = 0;
97 if ( mFeedback )
98 {
99 for ( const LayerData &layer : std::as_const( mLayerData ) )
100 {
101 if ( layer.source )
102 {
103 nFeatures += layer.source->featureCount();
104 }
105 }
106 }
107
108 const QgsCoordinateReferenceSystem crs = !mLayerData.empty() ? mLayerData.at( 0 ).source->sourceCrs() : QgsCoordinateReferenceSystem();
109
110 QgsFeature f;
111 for ( const LayerData &layer : std::as_const( mLayerData ) )
112 {
113 if ( layer.source )
114 {
115 QgsAttributeList attList;
116 switch ( layer.valueSource )
117 {
119 attList.push_back( layer.interpolationAttribute );
120 break;
121
124 break;
125 }
126
127 QgsFeatureIterator fit = layer.source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attList ).setDestinationCrs( crs, layer.transformContext ) );
128
129 while ( fit.nextFeature( f ) )
130 {
131 if ( mFeedback )
132 {
133 if ( mFeedback->isCanceled() )
134 {
135 break;
136 }
137 if ( nFeatures > 0 )
138 mFeedback->setProgress( 100.0 * static_cast<double>( nProcessedFeatures ) / nFeatures );
139 }
140 insertData( f, layer.valueSource, layer.interpolationAttribute, layer.sourceType );
141 ++nProcessedFeatures;
142 }
143 }
144 }
145
147 {
148 NormVecDecorator *dec = dynamic_cast<NormVecDecorator *>( mTriangulation );
149 if ( dec )
150 {
151 auto ctInterpolator = std::make_unique<CloughTocherInterpolator>();
152 dec->estimateFirstDerivatives( mFeedback );
153 ctInterpolator->setTriangulation( dec );
154 mTriangleInterpolator = ctInterpolator.release();
155 dec->setTriangleInterpolator( mTriangleInterpolator );
156 }
157 }
158 else //linear
159 {
160 mTriangleInterpolator = new LinTriangleInterpolator( dualEdgeTriangulation );
161 }
162 mIsInitialized = true;
163
164 //debug
165 if ( mTriangulationSink )
166 {
167 dualEdgeTriangulation->saveTriangulation( mTriangulationSink, mFeedback );
168 }
169}
170
171int QgsTinInterpolator::insertData( const QgsFeature &f, QgsInterpolator::ValueSource source, int attr, QgsInterpolator::SourceType type )
172{
173 QgsGeometry g = f.geometry();
174 if ( g.isNull() || g.isEmpty() )
175 {
176 return 2;
177 }
178
179 //check attribute value
180 double attributeValue = 0;
181 bool attributeConversionOk = false;
182 switch ( source )
183 {
185 {
186 QVariant attributeVariant = f.attribute( attr );
187 if ( QgsVariantUtils::isNull( attributeVariant ) ) //attribute not found, something must be wrong (e.g. NULL value)
188 {
189 return 3;
190 }
191 attributeValue = attributeVariant.toDouble( &attributeConversionOk );
192 if ( !attributeConversionOk || std::isnan( attributeValue ) ) //don't consider vertices with attributes like 'nan' for the interpolation
193 {
194 return 4;
195 }
196 break;
197 }
198
200 if ( !g.constGet()->isMeasure() )
201 return 3;
202 else
203 break;
204
206 if ( !g.constGet()->is3D() )
207 return 3;
208 else
209 break;
210 }
211
212 switch ( type )
213 {
215 {
216 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
217 return -1;
218 break;
219 }
220
223 {
224 switch ( QgsWkbTypes::geometryType( g.wkbType() ) )
225 {
227 {
228 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
229 return -1;
230 break;
231 }
232
235 {
236 // need to extract all rings from input geometry
237 std::vector<const QgsCurve *> curves;
239 {
240 std::vector<const QgsCurvePolygon *> polygons;
241 if ( g.isMultipart() )
242 {
243 const QgsMultiSurface *ms = qgsgeometry_cast<const QgsMultiSurface *>( g.constGet() );
244 for ( int i = 0; i < ms->numGeometries(); ++i )
245 {
246 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( ms->geometryN( i ) ) );
247 }
248 }
249 else
250 {
251 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( g.constGet() ) );
252 }
253
254 for ( const QgsCurvePolygon *polygon : polygons )
255 {
256 if ( !polygon )
257 continue;
258
259 if ( polygon->exteriorRing() )
260 curves.emplace_back( polygon->exteriorRing() );
261
262 for ( int i = 0; i < polygon->numInteriorRings(); ++i )
263 {
264 curves.emplace_back( polygon->interiorRing( i ) );
265 }
266 }
267 }
268 else
269 {
270 if ( g.isMultipart() )
271 {
272 const QgsMultiCurve *mc = qgsgeometry_cast<const QgsMultiCurve *>( g.constGet() );
273 for ( int i = 0; i < mc->numGeometries(); ++i )
274 {
275 curves.emplace_back( mc->curveN( i ) );
276 }
277 }
278 else
279 {
280 curves.emplace_back( qgsgeometry_cast<const QgsCurve *>( g.constGet() ) );
281 }
282 }
283
284 for ( const QgsCurve *curve : curves )
285 {
286 if ( !curve )
287 continue;
288
289 QgsPointSequence linePoints;
290 curve->points( linePoints );
291 for ( QgsPoint &point : linePoints )
292 {
293 switch ( source )
294 {
296 if ( point.is3D() )
297 point.setZ( attributeValue );
298 else
299 point.addZValue( attributeValue );
300 break;
301
303 if ( point.is3D() )
304 point.setZ( point.m() );
305 else
306 point.addZValue( point.m() );
307 break;
308
310 break;
311 }
312 }
313 mTriangulation->addLine( linePoints, type );
314 }
315 break;
316 }
319 break;
320 }
321 break;
322 }
323 }
324
325 return 0;
326}
327
328
329int QgsTinInterpolator::addPointsFromGeometry( const QgsGeometry &g, QgsInterpolator::ValueSource source, double attributeValue )
330{
331 // loop through all vertices and add to triangulation
332 for ( auto point = g.vertices_begin(); point != g.vertices_end(); ++point )
333 {
334 QgsPoint p = *point;
335 double z = 0;
336 switch ( source )
337 {
339 z = attributeValue;
340 break;
341
343 z = p.z();
344 break;
345
347 z = p.m();
348 break;
349 }
350 if ( mTriangulation->addPoint( QgsPoint( p.x(), p.y(), z ) ) == -100 )
351 {
352 return -1;
353 }
354 }
355 return 0;
356}
Interpolates linearly on a triangulation.
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 ...
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
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.
Represents a coordinate reference system (CRS).
Curve polygon geometry type.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
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.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
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:58
QgsGeometry geometry
Definition qgsfeature.h:69
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
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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.
Interface class for interpolations.
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...
Multi curve geometry collection.
QgsCurve * curveN(int index)
Returns the curve with the specified index.
Multi surface geometry collection.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
double m
Definition qgspoint.h:55
double y
Definition qgspoint.h:53
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().
virtual void addLine(const QgsPointSequence &points, QgsInterpolator::SourceType lineType)=0
Adds a line (e.g.
virtual int addPoint(const QgsPoint &point)=0
Adds a point to the triangulation.
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.
virtual bool calcPoint(double x, double y, QgsPoint &result)=0
Performs a linear interpolation in a triangle and assigns the x-,y- and z-coordinates to point.
QVector< QgsPoint > QgsPointSequence
QList< int > QgsAttributeList
Definition qgsfield.h:27
const QgsCoordinateReferenceSystem & crs