Skip to content

Commit

Permalink
Pass QgsFeedback to methods in interpolation for more responsive
Browse files Browse the repository at this point in the history
cancelation and progress reports
  • Loading branch information
nyalldawson committed Nov 2, 2017
1 parent 151fb08 commit 60e98be
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/analysis/interpolation/qgsgridfilewriter.cpp
Expand Up @@ -60,7 +60,7 @@ int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
for ( int j = 0; j < mNumColumns; ++j )
{
if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
{
outStream << interpolatedValue << ' ';
}
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/interpolation/qgsidwinterpolator.cpp
Expand Up @@ -31,11 +31,11 @@ QgsIDWInterpolator::QgsIDWInterpolator()

}

int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result )
int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback *feedback )
{
if ( !mDataIsCached )
{
cacheBaseData();
cacheBaseData( feedback );
}

double currentWeight;
Expand Down
2 changes: 2 additions & 0 deletions src/analysis/interpolation/qgsidwinterpolator.h
Expand Up @@ -30,6 +30,8 @@ class ANALYSIS_EXPORT QgsIDWInterpolator: public QgsInterpolator
public:
QgsIDWInterpolator( const QList<QgsInterpolator::LayerData> &layerData );

int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback = nullptr ) override;

/**
* Calculates interpolation value for map coordinates x, y
\param x x-coordinate (in map units)
Expand Down
19 changes: 13 additions & 6 deletions src/analysis/interpolation/qgsinterpolator.cpp
Expand Up @@ -20,15 +20,15 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsgeometry.h"
#include "qgswkbptr.h"
#include "qgsfeedback.h"

QgsInterpolator::QgsInterpolator( const QList<LayerData> &layerData )
: mLayerData( layerData )
{

}

int QgsInterpolator::cacheBaseData()
QgsInterpolator::Result QgsInterpolator::cacheBaseData( QgsFeedback *feedback )
{
if ( mLayerData.empty() )
{
Expand All @@ -41,10 +41,8 @@ int QgsInterpolator::cacheBaseData()

Q_FOREACH ( const LayerData &layer, mLayerData )
{
if ( !layer.vectorLayer )
{
continue;
}
if ( feedback && feedback->isCanceled() )
return Canceled;

QgsVectorLayer *vlayer = layer.vectorLayer;
if ( !vlayer )
Expand All @@ -61,12 +59,21 @@ int QgsInterpolator::cacheBaseData()

double attributeValue = 0.0;
bool attributeConversionOk = false;
double progress = layerCount * layerStep;

QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attList ) );
double featureStep = source->featureCount() > 0 ? layerStep / source->featureCount() : layerStep;

QgsFeature feature;
while ( fit.nextFeature( feature ) )
{
if ( feedback && feedback->isCanceled() )
return Canceled;

progress += featureStep;
if ( feedback )
feedback->setProgress( progress );

if ( !layer.zCoordInterpolation )
{
QVariant attributeVariant = feature.attribute( layer.interpolationAttribute );
Expand Down
12 changes: 7 additions & 5 deletions src/analysis/interpolation/qgsinterpolator.h
Expand Up @@ -24,6 +24,7 @@

class QgsVectorLayer;
class QgsGeometry;
class QgsFeedback;

struct ANALYSIS_EXPORT vertexData
{
Expand Down Expand Up @@ -63,11 +64,12 @@ class ANALYSIS_EXPORT QgsInterpolator

/**
* Calculates interpolation value for map coordinates x, y
\param x x-coordinate (in map units)
\param y y-coordinate (in map units)
\param result out: interpolation result
\returns 0 in case of success*/
virtual int interpolatePoint( double x, double y, double &result ) = 0;
* \param x x-coordinate (in map units)
* \param y y-coordinate (in map units)
* \param result out: interpolation result
* \param feedback optional feedback object for progress and cancelation support
* \returns 0 in case of success*/
virtual int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback = nullptr ) = 0;

//! \note not available in Python bindings
QList<LayerData> layerData() const { return mLayerData; } SIP_SKIP
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/interpolation/qgstininterpolator.cpp
Expand Up @@ -43,7 +43,7 @@ QgsTINInterpolator::~QgsTINInterpolator()
delete mTriangleInterpolator;
}

int QgsTINInterpolator::interpolatePoint( double x, double y, double &result )
int QgsTINInterpolator::interpolatePoint( double x, double y, double &result, QgsFeedback * )
{
if ( !mIsInitialized )
{
Expand Down
8 changes: 1 addition & 7 deletions src/analysis/interpolation/qgstininterpolator.h
Expand Up @@ -50,13 +50,7 @@ class ANALYSIS_EXPORT QgsTINInterpolator: public QgsInterpolator
QgsTINInterpolator( const QList<QgsInterpolator::LayerData> &inputData, TINInterpolation interpolation = Linear, QgsFeedback *feedback = nullptr );
~QgsTINInterpolator();

/**
* Calculates interpolation value for map coordinates x, y
\param x x-coordinate (in map units)
\param y y-coordinate (in map units)
\param result out: interpolation result
\returns 0 in case of success*/
int interpolatePoint( double x, double y, double &result ) override;
int interpolatePoint( double x, double y, double &result, QgsFeedback *feedback ) override;

/**
* Returns the fields output by features when saving the triangulation.
Expand Down

0 comments on commit 60e98be

Please sign in to comment.