Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify progress reporting
  • Loading branch information
nyalldawson committed Mar 31, 2020
1 parent a2f8646 commit 13f5bdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
34 changes: 23 additions & 11 deletions src/analysis/processing/qgsalgorithmrandompointsonlines.cpp
Expand Up @@ -164,17 +164,16 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
int missedPoints = 0;
int missedLines = 0;
int emptyOrNullGeom = 0;
long tries = 0;
long saved = 0;

long featureCount = 0;
long numberOfFeatures = lineSource->featureCount();
const double featureProgressStep = 100.0 / ( numberOfFeatures > 0 ? numberOfFeatures : 1 );
QgsFeature lFeat;
QgsFeatureIterator fitL = mIncludeLineAttr ? lineSource->getFeatures()
: lineSource->getFeatures( QgsFeatureRequest().setNoAttributes() );
while ( fitL.nextFeature( lFeat ) )
{
featureCount++;

if ( feedback->isCanceled() )
{
break;
Expand All @@ -183,18 +182,27 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
{
// Increment invalid features count
emptyOrNullGeom++;
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
continue;
}
QgsGeometry lGeom( lFeat.geometry() );
if ( lGeom.isEmpty() )
{
// Increment invalid features count
emptyOrNullGeom++;
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
continue;
}

double lineLength = lGeom.length();
int pointsAddedForThisFeature = 0;
for ( long i = 0; i < mNumPoints; i++ )

const double baseFeatureProgress = featureCount * featureProgressStep;
const double pointProgressIncrement = featureProgressStep / mNumPoints;

for ( long pointIndex = 0; pointIndex < mNumPoints; pointIndex++ )
{
if ( feedback->isCanceled() )
{
Expand All @@ -208,8 +216,6 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
{
break;
}
distCheckIterations++;
tries++;
// Generate a random point
double randPos = lineLength * ( double ) rand() / RAND_MAX;
QgsGeometry rpGeom = QgsGeometry( lGeom.interpolate( randPos ) );
Expand All @@ -222,7 +228,9 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
QList<QgsFeatureId> neighbors = index.nearestNeighbor( rpGeom, 1, mMinDistance );
if ( !neighbors.empty() )
{
// Too close!
// total progress = progress over input features + progress over desired number of points for this feature + number of iterations for this point vs max iterations
distCheckIterations++;
feedback->setProgress( baseFeatureProgress + pointProgressIncrement * ( pointIndex + static_cast< double >( distCheckIterations ) / mMaxAttempts ) );
continue;
}
}
Expand All @@ -244,21 +252,25 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
sink->addFeature( f, QgsFeatureSink::FastInsert );
totNPoints++;
pointsAddedForThisFeature++;
saved += ( mMaxAttempts - distCheckIterations );

feedback->setProgress( static_cast<int>( 100 * static_cast<double>( tries + saved ) / static_cast<double>( mNumPoints * mMaxAttempts * ( numberOfFeatures - emptyOrNullGeom ) ) ) );
break;
}
else
{
feedback->setProgress( static_cast<int>( 100 * static_cast<double>( tries + saved ) / static_cast<double>( mNumPoints * mMaxAttempts * ( numberOfFeatures - emptyOrNullGeom ) ) ) );
// total progress = progress over input features + progress over desired number of points for this feature + number of iterations for this point vs max iterations
distCheckIterations++;
feedback->setProgress( baseFeatureProgress + pointProgressIncrement * ( pointIndex + static_cast< double >( distCheckIterations ) / mMaxAttempts ) );
}
}

// total progress = progress over input features + progress over desired number of points for this feature
feedback->setProgress( baseFeatureProgress + pointProgressIncrement * ( pointIndex + 1 ) );
}
if ( pointsAddedForThisFeature < mNumPoints )
{
missedLines++;
}
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
}
missedPoints = mNumPoints * featureCount - totNPoints;
feedback->pushInfo( QObject::tr( "Total number of points generated: "
Expand Down
11 changes: 5 additions & 6 deletions src/analysis/processing/qgsalgorithmrandompointsonlines.h
Expand Up @@ -55,12 +55,11 @@ class QgsRandomPointsOnLinesAlgorithm : public QgsProcessingAlgorithm

private:
int mNumPoints;
double mMinDistance;
int mMaxAttempts;
int mRandSeed;
bool mIncludeLineAttr;
QgsCoordinateReferenceSystem mCrs;

double mMinDistance = 0;
int mMaxAttempts = 10;
bool mUseRandomSeed = false;
int mRandSeed = 0;
bool mIncludeLineAttr = false;
};

///@endcond PRIVATE
Expand Down

0 comments on commit 13f5bdd

Please sign in to comment.