QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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  QVector<vertexData>::iterator vertex_it = mCachedBaseData.begin();
51 
52  for ( ; vertex_it != mCachedBaseData.end(); ++vertex_it )
53  {
54  distance = sqrt(( vertex_it->x - x ) * ( vertex_it->x - x ) + ( vertex_it->y - y ) * ( vertex_it->y - y ) );
55  if (( distance - 0 ) < std::numeric_limits<double>::min() )
56  {
57  result = vertex_it->z;
58  return 0;
59  }
60  currentWeight = 1 / ( pow( distance, mDistanceCoefficient ) );
61  sumCounter += ( currentWeight * vertex_it->z );
62  sumDenominator += currentWeight;
63  }
64 
65  if ( sumDenominator == 0.0 )
66  {
67  return 1;
68  }
69 
70  result = sumCounter / sumDenominator;
71  return 0;
72 }