QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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"
20 #include <cmath>
21 #include <limits>
22 
23 QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData> &layerData )
24  : QgsInterpolator( layerData )
25 {}
26 
27 int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback *feedback )
28 {
29  if ( !mDataIsCached )
30  {
31  cacheBaseData( feedback );
32  }
33 
34  double sumCounter = 0;
35  double sumDenominator = 0;
36 
37  for ( const QgsInterpolatorVertexData &vertex : std::as_const( mCachedBaseData ) )
38  {
39  double distance = std::sqrt( ( vertex.x - x ) * ( vertex.x - x ) + ( vertex.y - y ) * ( vertex.y - y ) );
40  if ( qgsDoubleNear( distance, 0.0 ) )
41  {
42  result = vertex.z;
43  return 0;
44  }
45  double currentWeight = 1 / ( std::pow( distance, mDistanceCoefficient ) );
46  sumCounter += ( currentWeight * vertex.z );
47  sumDenominator += currentWeight;
48  }
49 
50  if ( sumDenominator == 0.0 )
51  {
52  return 1;
53  }
54 
55  result = sumCounter / sumDenominator;
56  return 0;
57 }
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
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:1578
Interpolation data for an individual source vertex.