QGIS API Documentation  2.99.0-Master (ba079d8)
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 <cmath>
20 #include <limits>
21 
22 QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData> &layerData ): QgsInterpolator( layerData ), mDistanceCoefficient( 2.0 )
23 {
24 
25 }
26 
27 QgsIDWInterpolator::QgsIDWInterpolator(): QgsInterpolator( QList<LayerData>() ), mDistanceCoefficient( 2.0 )
28 {
29 
30 }
31 
32 int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result )
33 {
34  if ( !mDataIsCached )
35  {
36  cacheBaseData();
37  }
38 
39  double currentWeight;
40  double distance;
41 
42  double sumCounter = 0;
43  double sumDenominator = 0;
44 
45  Q_FOREACH ( const vertexData &vertex_it, mCachedBaseData )
46  {
47  distance = sqrt( ( vertex_it.x - x ) * ( vertex_it.x - x ) + ( vertex_it.y - y ) * ( vertex_it.y - y ) );
48  if ( ( distance - 0 ) < std::numeric_limits<double>::min() )
49  {
50  result = vertex_it.z;
51  return 0;
52  }
53  currentWeight = 1 / ( pow( distance, mDistanceCoefficient ) );
54  sumCounter += ( currentWeight * vertex_it.z );
55  sumDenominator += currentWeight;
56  }
57 
58  if ( sumDenominator == 0.0 )
59  {
60  return 1;
61  }
62 
63  result = sumCounter / sumDenominator;
64  return 0;
65 }
QVector< vertexData > mCachedBaseData
Interface class for interpolations.
int interpolatePoint(double x, double y, double &result) override
Calculates interpolation value for map coordinates x, y.
bool mDataIsCached
Flag that tells if the cache already has been filled.
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:452
QgsIDWInterpolator(const QList< LayerData > &layerData)
int cacheBaseData()
Caches the vertex and value data from the provider.