QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 : qgis::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 }
QgsInterpolatorVertexData
Interpolation data for an individual source vertex.
Definition: qgsinterpolator.h:37
QgsInterpolator::mDataIsCached
bool mDataIsCached
Flag that tells if the cache already has been filled.
Definition: qgsinterpolator.h:149
qgis.h
QgsInterpolator
Interface class for interpolations.
Definition: qgsinterpolator.h:67
qgsidwinterpolator.h
QgsIDWInterpolator::QgsIDWInterpolator
QgsIDWInterpolator(const QList< QgsInterpolator::LayerData > &layerData)
Constructor for QgsIDWInterpolator, with the specified layerData sources.
Definition: qgsidwinterpolator.cpp:23
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsInterpolator::cacheBaseData
Result cacheBaseData(QgsFeedback *feedback=nullptr)
Caches the vertex and value data from the provider.
Definition: qgsinterpolator.cpp:31
QgsInterpolator::mCachedBaseData
QVector< QgsInterpolatorVertexData > mCachedBaseData
Cached vertex data for input sources.
Definition: qgsinterpolator.h:146
QgsIDWInterpolator::interpolatePoint
int interpolatePoint(double x, double y, double &result, QgsFeedback *feedback=nullptr) override
Calculates interpolation value for map coordinates x, y.
Definition: qgsidwinterpolator.cpp:27