QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsidwinterpolator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsidwinterpolator.cpp
3 ----------------------
4 begin : Marco 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 "qgsidwinterpolator.h"
19#include "qgis.h"
21#include <cmath>
22#include <limits>
23
24QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData> &layerData )
25 : QgsInterpolator( layerData )
26{}
27
28int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback *feedback )
29{
30 if ( !mDataIsCached )
31 {
32 cacheBaseData( feedback );
33 }
34
35 double sumCounter = 0;
36 double sumDenominator = 0;
37
38 for ( const QgsInterpolatorVertexData &vertex : std::as_const( mCachedBaseData ) )
39 {
40 double distance = QgsGeometryUtilsBase::distance2D( vertex.x, vertex.y, x, y );
41 if ( qgsDoubleNear( distance, 0.0 ) )
42 {
43 result = vertex.z;
44 return 0;
45 }
46 double currentWeight = 1 / ( std::pow( distance, mDistanceCoefficient ) );
47 sumCounter += ( currentWeight * vertex.z );
48 sumDenominator += currentWeight;
49 }
50
51 if ( sumDenominator == 0.0 )
52 {
53 return 1;
54 }
55
56 result = sumCounter / sumDenominator;
57 return 0;
58}
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
int interpolatePoint(double x, double y, double &result, QgsFeedback *feedback=nullptr) override
Calculates interpolation value for map coordinates x, y.
QgsIDWInterpolator(const QList< QgsInterpolator::LayerData > &layerData)
Constructor for QgsIDWInterpolator, with the specified layerData sources.
Interface class for interpolations.
QVector< QgsInterpolatorVertexData > mCachedBaseData
Cached vertex data for input sources.
bool mDataIsCached
Flag that tells if the cache already has been filled.
Result cacheBaseData(QgsFeedback *feedback=nullptr)
Caches the vertex and value data from the provider.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6066
Interpolation data for an individual source vertex.