QGIS API Documentation  2.15.0-Master (5f66276)
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 
28 {
29 
30 }
31 
33 {
34 
35 }
36 
37 int QgsIDWInterpolator::interpolatePoint( double x, double y, double& result )
38 {
39  if ( !mDataIsCached )
40  {
41  cacheBaseData();
42  }
43 
44  double currentWeight;
45  double distance;
46 
47  double sumCounter = 0;
48  double sumDenominator = 0;
49 
50  Q_FOREACH ( const vertexData& vertex_it, mCachedBaseData )
51  {
52  distance = sqrt(( vertex_it.x - x ) * ( vertex_it.x - x ) + ( vertex_it.y - y ) * ( vertex_it.y - y ) );
53  if (( distance - 0 ) < std::numeric_limits<double>::min() )
54  {
55  result = vertex_it.z;
56  return 0;
57  }
58  currentWeight = 1 / ( pow( distance, mDistanceCoefficient ) );
59  sumCounter += ( currentWeight * vertex_it.z );
60  sumDenominator += currentWeight;
61  }
62 
63  if ( sumDenominator == 0.0 )
64  {
65  return 1;
66  }
67 
68  result = sumCounter / sumDenominator;
69  return 0;
70 }
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.
QgsIDWInterpolator(const QList< LayerData > &layerData)
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
int cacheBaseData()
Caches the vertex and value data from the provider.