Skip to content
Permalink
Browse files
Fixed variable name and modified progress reporting
  • Loading branch information
havatv committed Apr 7, 2020
1 parent df0f975 commit bf0aafc6342df9e543c4fd331cb2c7355a64ca5e
Showing with 25 additions and 24 deletions.
  1. +25 −24 src/analysis/processing/qgsalgorithmrandompointsonlines.cpp
@@ -74,7 +74,7 @@ void QgsRandomPointsOnLinesAlgorithm::initAlgorithm( const QVariantMap & )
minDistParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
addParameter( minDistParam.release() );

std::unique_ptr< QgsProcessingParameterNumber > maxAttemptsParam = qgis::make_unique< QgsProcessingParameterNumber >( MAX_TRIES_PER_POINT, QObject::tr( "Maximum number of search attempts (for Min. dist. > 0)" ), QgsProcessingParameterNumber::Integer, 10, true, 1, 1000 );
std::unique_ptr< QgsProcessingParameterNumber > maxAttemptsParam = qgis::make_unique< QgsProcessingParameterNumber >( MAX_TRIES_PER_POINT, QObject::tr( "Maximum number of search attempts (for Min. dist. > 0)" ), QgsProcessingParameterNumber::Integer, 10, true, 1 );
maxAttemptsParam->setFlags( maxAttemptsParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
maxAttemptsParam->setIsDynamic( true );
maxAttemptsParam->setDynamicPropertyDefinition( QgsPropertyDefinition( MAX_TRIES_PER_POINT, QObject::tr( "Maximum number of search attempts (for Min. dist. > 0)" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
@@ -179,12 +179,12 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap

QgsExpressionContext expressionContext = createExpressionContext( parameters, context, lineSource.get() );

//initialize random engine
// Initialize random engine
std::random_device rd;
std::mt19937 mt( !mUseRandomSeed ? rd() : mRandSeed );
std::uniform_real_distribution<> uniformDist( 0, 1 );

//index for finding close points (mMinDistance > 0)
// Index for finding close points (mMinDistance > 0)
QgsSpatialIndex index;

int totNPoints = 0;
@@ -196,22 +196,24 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
long numberOfFeatures = lineSource->featureCount();
long long desiredNumberOfPoints = 0;
const double featureProgressStep = 100.0 / ( numberOfFeatures > 0 ? numberOfFeatures : 1 );
double baseFeatureProgress = 0.0;
QgsFeature lFeat;
QgsFeatureIterator fitL = mIncludeLineAttr || mDynamicNumPoints || mDynamicMinDistance || mDynamicMaxAttempts ? lineSource->getFeatures()
: lineSource->getFeatures( QgsFeatureRequest().setNoAttributes() );
while ( fitL.nextFeature( lFeat ) )
{

if ( feedback->isCanceled() )
{
feedback->setProgress( 0 );
break;
}
if ( !lFeat.hasGeometry() )
{
// Increment invalid features count
emptyOrNullGeom++;
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
baseFeatureProgress += featureProgressStep;
feedback->setProgress( baseFeatureProgress );
continue;
}
QgsGeometry lGeom( lFeat.geometry() );
@@ -220,7 +222,8 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
// Increment invalid features count
emptyOrNullGeom++;
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
baseFeatureProgress += featureProgressStep;
feedback->setProgress( baseFeatureProgress );
continue;
}

@@ -237,17 +240,17 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
numberPointsForThisFeature = mNumPointsProperty.valueAsInt( expressionContext, numberPointsForThisFeature );
desiredNumberOfPoints += numberPointsForThisFeature;

int maxAttemptsForThisFeatures = mMaxAttempts;
int maxAttemptsForThisFeature = mMaxAttempts;
if ( mDynamicMaxAttempts )
maxAttemptsForThisFeatures = mMaxAttemptsProperty.valueAsInt( expressionContext, maxAttemptsForThisFeatures );
maxAttemptsForThisFeature = mMaxAttemptsProperty.valueAsInt( expressionContext, maxAttemptsForThisFeature );

double minDistanceForThisFeature = mMinDistance;
if ( mDynamicMinDistance )
minDistanceForThisFeature = mMinDistanceProperty.valueAsDouble( expressionContext, minDistanceForThisFeature );

const double baseFeatureProgress = featureCount * featureProgressStep;
const double pointProgressIncrement = featureProgressStep / numberPointsForThisFeature;
const double pointProgressIncrement = featureProgressStep / ( numberPointsForThisFeature * maxAttemptsForThisFeature );

double pointProgress = 0.0;
for ( long pointIndex = 0; pointIndex < numberPointsForThisFeature; pointIndex++ )
{
if ( feedback->isCanceled() )
@@ -256,7 +259,7 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
}
// Try to add a point (mMaxAttempts attempts)
int distCheckIterations = 0;
while ( distCheckIterations < maxAttemptsForThisFeatures )
while ( distCheckIterations < maxAttemptsForThisFeature )
{
if ( feedback->isCanceled() )
{
@@ -265,6 +268,8 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
// Generate a random point
double randPos = lineLength * uniformDist( mt );
QgsGeometry rpGeom = QgsGeometry( lGeom.interpolate( randPos ) );
distCheckIterations++;
pointProgress += pointProgressIncrement;

if ( !rpGeom.isNull() && !rpGeom.isEmpty() )
{
@@ -274,9 +279,7 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
QList<QgsFeatureId> neighbors = index.nearestNeighbor( rpGeom, 1, minDistanceForThisFeature );
if ( !neighbors.empty() )
{
// 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 ) / maxAttemptsForThisFeatures ) );
feedback->setProgress( baseFeatureProgress + pointProgress );
continue;
}
}
@@ -298,26 +301,24 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
sink->addFeature( f, QgsFeatureSink::FastInsert );
totNPoints++;
pointsAddedForThisFeature++;
pointProgress += pointProgressIncrement * ( maxAttemptsForThisFeature - distCheckIterations );
break;
}
else
{
// 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 ) / maxAttemptsForThisFeatures ) );
feedback->setProgress( baseFeatureProgress + pointProgress );
}
}

// total progress = progress over input features + progress over desired number of points for this feature
feedback->setProgress( baseFeatureProgress + pointProgressIncrement * ( pointIndex + 1 ) );
}
} // while not maxattempts
feedback->setProgress( baseFeatureProgress + pointProgress );
} // for points
baseFeatureProgress += featureProgressStep;
if ( pointsAddedForThisFeature < numberPointsForThisFeature )
{
missedLines++;
}
featureCount++;
feedback->setProgress( featureCount * featureProgressStep );
}
feedback->setProgress( baseFeatureProgress );
} // while features
missedPoints = desiredNumberOfPoints - totNPoints;
feedback->pushInfo( QObject::tr( "Total number of points generated: "
" %1\nNumber of missed points: %2\nLines with missing points: "

0 comments on commit bf0aafc

Please sign in to comment.