QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 , mIsInitialized( false )
39 , mFeedback( feedback )
40 , mInterpolation( interpolation )
41{
42}
43
45{
46 delete mTriangulation;
47 delete mTriangleInterpolator;
48}
49
50int QgsTinInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback * )
51{
52 if ( !mIsInitialized )
53 {
54 initialize();
55 }
56
57 if ( !mTriangleInterpolator )
58 {
59 return 1;
60 }
61
62 QgsPoint r( 0, 0, 0 );
63 if ( !mTriangleInterpolator->calcPoint( x, y, r ) )
64 {
65 return 2;
66 }
67 result = r.z();
68 return 0;
69}
70
75
77{
78 mTriangulationSink = sink;
79}
80
81void QgsTinInterpolator::initialize()
82{
83 QgsDualEdgeTriangulation *dualEdgeTriangulation = new QgsDualEdgeTriangulation( 100000 );
85 {
87 dec->addTriangulation( dualEdgeTriangulation );
88 mTriangulation = dec;
89 }
90 else
91 {
92 mTriangulation = dualEdgeTriangulation;
93 }
94
95 //get number of features if we use a progress bar
96 long long nFeatures = 0;
97 long long nProcessedFeatures = 0;
98 if ( mFeedback )
99 {
100 for ( const LayerData &layer : std::as_const( mLayerData ) )
101 {
102 if ( layer.source )
103 {
104 nFeatures += layer.source->featureCount();
105 }
106 }
107 }
108
109 const QgsCoordinateReferenceSystem crs = !mLayerData.empty() ? mLayerData.at( 0 ).source->sourceCrs() : QgsCoordinateReferenceSystem();
110
111 QgsFeature f;
112 for ( const LayerData &layer : std::as_const( mLayerData ) )
113 {
114 if ( layer.source )
115 {
116 QgsAttributeList attList;
117 switch ( layer.valueSource )
118 {
120 attList.push_back( layer.interpolationAttribute );
121 break;
122
125 break;
126 }
127
128 QgsFeatureIterator fit = layer.source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attList ).setDestinationCrs( crs, layer.transformContext ) );
129
130 while ( fit.nextFeature( f ) )
131 {
132 if ( mFeedback )
133 {
134 if ( mFeedback->isCanceled() )
135 {
136 break;
137 }
138 if ( nFeatures > 0 )
139 mFeedback->setProgress( 100.0 * static_cast<double>( nProcessedFeatures ) / nFeatures );
140 }
141 insertData( f, layer.valueSource, layer.interpolationAttribute, layer.sourceType );
142 ++nProcessedFeatures;
143 }
144 }
145 }
146
148 {
149 NormVecDecorator *dec = dynamic_cast<NormVecDecorator *>( mTriangulation );
150 if ( dec )
151 {
152 auto ctInterpolator = std::make_unique<CloughTocherInterpolator>();
153 dec->estimateFirstDerivatives( mFeedback );
154 ctInterpolator->setTriangulation( dec );
155 mTriangleInterpolator = ctInterpolator.release();
156 dec->setTriangleInterpolator( mTriangleInterpolator );
157 }
158 }
159 else //linear
160 {
161 mTriangleInterpolator = new LinTriangleInterpolator( dualEdgeTriangulation );
162 }
163 mIsInitialized = true;
164
165 //debug
166 if ( mTriangulationSink )
167 {
168 dualEdgeTriangulation->saveTriangulation( mTriangulationSink, mFeedback );
169 }
170}
171
172int QgsTinInterpolator::insertData( const QgsFeature &f, QgsInterpolator::ValueSource source, int attr, QgsInterpolator::SourceType type )
173{
174 QgsGeometry g = f.geometry();
175 if ( g.isNull() || g.isEmpty() )
176 {
177 return 2;
178 }
179
180 //check attribute value
181 double attributeValue = 0;
182 bool attributeConversionOk = false;
183 switch ( source )
184 {
186 {
187 QVariant attributeVariant = f.attribute( attr );
188 if ( QgsVariantUtils::isNull( attributeVariant ) ) //attribute not found, something must be wrong (e.g. NULL value)
189 {
190 return 3;
191 }
192 attributeValue = attributeVariant.toDouble( &attributeConversionOk );
193 if ( !attributeConversionOk || std::isnan( attributeValue ) ) //don't consider vertices with attributes like 'nan' for the interpolation
194 {
195 return 4;
196 }
197 break;
198 }
199
201 if ( !g.constGet()->isMeasure() )
202 return 3;
203 else
204 break;
205
207 if ( !g.constGet()->is3D() )
208 return 3;
209 else
210 break;
211 }
212
213 switch ( type )
214 {
216 {
217 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
218 return -1;
219 break;
220 }
221
224 {
225 switch ( QgsWkbTypes::geometryType( g.wkbType() ) )
226 {
228 {
229 if ( addPointsFromGeometry( g, source, attributeValue ) != 0 )
230 return -1;
231 break;
232 }
233
236 {
237 // need to extract all rings from input geometry
238 std::vector<const QgsCurve *> curves;
240 {
241 std::vector<const QgsCurvePolygon *> polygons;
242 if ( g.isMultipart() )
243 {
244 const QgsMultiSurface *ms = qgsgeometry_cast<const QgsMultiSurface *>( g.constGet() );
245 for ( int i = 0; i < ms->numGeometries(); ++i )
246 {
247 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( ms->geometryN( i ) ) );
248 }
249 }
250 else
251 {
252 polygons.emplace_back( qgsgeometry_cast<const QgsCurvePolygon *>( g.constGet() ) );
253 }
254
255 for ( const QgsCurvePolygon *polygon : polygons )
256 {
257 if ( !polygon )
258 continue;
259
260 if ( polygon->exteriorRing() )
261 curves.emplace_back( polygon->exteriorRing() );
262
263 for ( int i = 0; i < polygon->numInteriorRings(); ++i )
264 {
265 curves.emplace_back( polygon->interiorRing( i ) );
266 }
267 }
268 }
269 else
270 {
271 if ( g.isMultipart() )
272 {
273 const QgsMultiCurve *mc = qgsgeometry_cast<const QgsMultiCurve *>( g.constGet() );
274 for ( int i = 0; i < mc->numGeometries(); ++i )
275 {
276 curves.emplace_back( mc->curveN( i ) );
277 }
278 }
279 else
280 {
281 curves.emplace_back( qgsgeometry_cast<const QgsCurve *>( g.constGet() ) );
282 }
283 }
284
285 for ( const QgsCurve *curve : curves )
286 {
287 if ( !curve )
288 continue;
289
290 QgsPointSequence linePoints;
291 curve->points( linePoints );
292 for ( QgsPoint &point : linePoints )
293 {
294 switch ( source )
295 {
297 if ( point.is3D() )
298 point.setZ( attributeValue );
299 else
300 point.addZValue( attributeValue );
301 break;
302
304 if ( point.is3D() )
305 point.setZ( point.m() );
306 else
307 point.addZValue( point.m() );
308 break;
309
311 break;
312 }
313 }
314 mTriangulation->addLine( linePoints, type );
315 }
316 break;
317 }
320 break;
321 }
322 break;
323 }
324 }
325
326 return 0;
327}
328
329
330int QgsTinInterpolator::addPointsFromGeometry( const QgsGeometry &g, QgsInterpolator::ValueSource source, double attributeValue )
331{
332 // loop through all vertices and add to triangulation
333 for ( auto point = g.vertices_begin(); point != g.vertices_end(); ++point )
334 {
335 QgsPoint p = *point;
336 double z = 0;
337 switch ( source )
338 {
340 z = attributeValue;
341 break;
342
344 z = p.z();
345 break;
346
348 z = p.m();
349 break;
350 }
351 if ( mTriangulation->addPoint( QgsPoint( p.x(), p.y(), z ) ) == -100 )
352 {
353 return -1;
354 }
355 }
356 return 0;
357}
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:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Unknown
Unknown types.
Definition qgis.h:362
@ Null
No geometry.
Definition qgis.h:363
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: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
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: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().
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:28
A source together with the information about interpolation attribute / z-coordinate interpolation and...