Skip to content

Commit f200d9c

Browse files
committedNov 2, 2017
Cleanup interpolation code
1 parent cb292c1 commit f200d9c

File tree

4 files changed

+104
-54
lines changed

4 files changed

+104
-54
lines changed
 

‎python/analysis/interpolation/qgsidwinterpolator.sip

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,46 @@
1111

1212
class QgsIDWInterpolator: QgsInterpolator
1313
{
14+
%Docstring
15+
Inverse distance weight interpolator.
16+
%End
1417

1518
%TypeHeaderCode
1619
#include "qgsidwinterpolator.h"
1720
%End
1821
public:
22+
1923
QgsIDWInterpolator( const QList<QgsInterpolator::LayerData> &layerData );
24+
%Docstring
25+
Constructor for QgsIDWInterpolator, with the specified ``layerData`` sources.
26+
%End
27+
28+
virtual int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback = 0 );
2029

21-
virtual int interpolatePoint( double x, double y, double &result );
2230

31+
void setDistanceCoefficient( double coefficient );
2332
%Docstring
24-
Calculates interpolation value for map coordinates x, y
25-
\param x x-coordinate (in map units)
26-
\param y y-coordinate (in map units)
27-
\param result out: interpolation result
28-
:return: 0 in case of success*
29-
:rtype: int
33+
Sets the distance ``coefficient``, the parameter that sets how the values are
34+
weighted with distance. Smaller values mean sharper peaks at the data points.
35+
36+
Point values are weighted by 1 / ( distance ^ coefficient ).
37+
38+
.. seealso:: distanceCoefficient()
39+
.. versionadded:: 3.0
3040
%End
3141

32-
void setDistanceCoefficient( double p );
42+
double distanceCoefficient() const;
43+
%Docstring
44+
Returns the distance coefficient, the parameter that sets how the values are
45+
weighted with distance. Smaller values mean sharper peaks at the data points.
46+
The default is a coefficient of 2.
47+
48+
Point values are weighted by 1 / ( distance ^ coefficient ).
49+
50+
.. seealso:: setDistanceCoefficient()
51+
.. versionadded:: 3.0
52+
:rtype: float
53+
%End
3354

3455
};
3556

‎python/analysis/interpolation/qgsinterpolator.sip

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,110 @@
1010

1111

1212

13-
struct vertexData
13+
struct QgsInterpolatorVertexData
1414
{
15+
16+
QgsInterpolatorVertexData( double x, double y, double z );
17+
%Docstring
18+
Constructor for QgsInterpolatorVertexData with the specified
19+
``x``, ``y``, and ``z`` coordinate.
20+
%End
21+
22+
QgsInterpolatorVertexData();
23+
%Docstring
24+
Constructor for QgsInterpolatorVertexData
25+
%End
26+
1527
double x;
28+
%Docstring
29+
X-coordinate
30+
%End
1631
double y;
32+
%Docstring
33+
Y-coordinate
34+
%End
1735
double z;
36+
%Docstring
37+
Z-coordinate
38+
%End
1839
};
1940

2041
class QgsInterpolator
2142
{
2243
%Docstring
2344
Interface class for interpolations. Interpolators take
24-
the vertices of a vector layer as base data. The z-Value
25-
can be an attribute or the z-coordinates in case of 25D types*
45+
the vertices of a vector layer as base data. The z-Value
46+
can be an attribute or the z-coordinates in case of 3D types.
2647
%End
2748

2849
%TypeHeaderCode
2950
#include "qgsinterpolator.h"
3051
%End
3152
public:
32-
enum InputType
53+
54+
enum SourceType
3355
{
34-
POINTS,
35-
STRUCTURE_LINES,
36-
BREAK_LINES
56+
SourcePoints,
57+
SourceStructureLines,
58+
SourceBreakLines,
59+
};
60+
61+
enum Result
62+
{
63+
Success,
64+
Canceled,
65+
InvalidSource,
66+
FeatureGeometryError,
3767
};
3868

3969
struct LayerData
4070
{
41-
QgsVectorLayer *vectorLayer;
42-
bool zCoordInterpolation;
71+
QgsFeatureSource *source;
72+
%Docstring
73+
Feature source
74+
%End
75+
bool useZValue;
76+
%Docstring
77+
True if feature geometry z values should be used for interpolation
78+
%End
4379
int interpolationAttribute;
44-
QgsInterpolator::InputType mInputType;
80+
%Docstring
81+
Index of feature attribute to use for interpolation
82+
%End
83+
QgsInterpolator::SourceType sourceType;
84+
%Docstring
85+
Source type
86+
%End
4587
};
4688

4789
QgsInterpolator( const QList<QgsInterpolator::LayerData> &layerData );
4890

4991
virtual ~QgsInterpolator();
5092

51-
virtual int interpolatePoint( double x, double y, double &result ) = 0;
93+
virtual int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback = 0 ) = 0;
5294
%Docstring
5395
Calculates interpolation value for map coordinates x, y
54-
\param x x-coordinate (in map units)
55-
\param y y-coordinate (in map units)
56-
\param result out: interpolation result
57-
:return: 0 in case of success*
96+
\param x x-coordinate (in map units)
97+
\param y y-coordinate (in map units)
98+
\param result out: interpolation result
99+
\param feedback optional feedback object for progress and cancelation support
100+
:return: 0 in case of success*
58101
:rtype: int
59102
%End
60103

61104

62105
protected:
63106

64-
int cacheBaseData();
107+
Result cacheBaseData( QgsFeedback *feedback = 0 );
65108
%Docstring
66109
Caches the vertex and value data from the provider. All the vertex data
67-
will be held in virtual memory
68-
:return: 0 in case of success*
69-
:rtype: int
110+
will be held in virtual memory.
111+
112+
An optional ``feedback`` argument may be specified to allow cancelation and
113+
progress reports from the cache operation.
114+
115+
:return: Success in case of success
116+
:rtype: Result
70117
%End
71118

72119

‎python/analysis/interpolation/qgstininterpolator.sip

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,8 @@ class QgsTINInterpolator: QgsInterpolator
3434
%End
3535
~QgsTINInterpolator();
3636

37-
virtual int interpolatePoint( double x, double y, double &result );
37+
virtual int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback );
3838

39-
%Docstring
40-
Calculates interpolation value for map coordinates x, y
41-
\param x x-coordinate (in map units)
42-
\param y y-coordinate (in map units)
43-
\param result out: interpolation result
44-
:return: 0 in case of success*
45-
:rtype: int
46-
%End
4739

4840
static QgsFields triangulationFields();
4941
%Docstring

‎src/analysis/interpolation/qgsidwinterpolator.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@
1616
***************************************************************************/
1717

1818
#include "qgsidwinterpolator.h"
19+
#include "qgis.h"
1920
#include <cmath>
2021
#include <limits>
2122

2223
QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData> &layerData )
2324
: QgsInterpolator( layerData )
24-
{
25-
26-
}
27-
28-
QgsIDWInterpolator::QgsIDWInterpolator()
29-
: QgsInterpolator( QList<LayerData>() )
30-
{
31-
32-
}
25+
{}
3326

3427
int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback *feedback )
3528
{
@@ -38,22 +31,19 @@ int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result, Qg
3831
cacheBaseData( feedback );
3932
}
4033

41-
double currentWeight;
42-
double distance;
43-
4434
double sumCounter = 0;
4535
double sumDenominator = 0;
4636

47-
Q_FOREACH ( const vertexData &vertex_it, mCachedBaseData )
37+
for ( const QgsInterpolatorVertexData &vertex : qgis::as_const( mCachedBaseData ) )
4838
{
49-
distance = std::sqrt( ( vertex_it.x - x ) * ( vertex_it.x - x ) + ( vertex_it.y - y ) * ( vertex_it.y - y ) );
50-
if ( ( distance - 0 ) < std::numeric_limits<double>::min() )
39+
double distance = std::sqrt( ( vertex.x - x ) * ( vertex.x - x ) + ( vertex.y - y ) * ( vertex.y - y ) );
40+
if ( qgsDoubleNear( distance, 0.0 ) )
5141
{
52-
result = vertex_it.z;
42+
result = vertex.z;
5343
return 0;
5444
}
55-
currentWeight = 1 / ( std::pow( distance, mDistanceCoefficient ) );
56-
sumCounter += ( currentWeight * vertex_it.z );
45+
double currentWeight = 1 / ( std::pow( distance, mDistanceCoefficient ) );
46+
sumCounter += ( currentWeight * vertex.z );
5747
sumDenominator += currentWeight;
5848
}
5949

0 commit comments

Comments
 (0)
Please sign in to comment.