Skip to content

Commit

Permalink
[processing] Misc fixes and improvements to Random Points in Polygons
Browse files Browse the repository at this point in the history
- Fix missing format call for string when calculated number
of points is 0
- Fix incorrect progress reports which were spamming the dialog
and causing UI hangs, and slow algorithm execution
  • Loading branch information
nyalldawson committed Feb 14, 2018
1 parent e0cf578 commit 81aa2f9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions python/plugins/processing/algs/qgis/RandomPointsPolygons.py
Expand Up @@ -131,10 +131,14 @@ def processAlgorithm(self, parameters, context, feedback):
da.setEllipsoid(context.project().ellipsoid())

total = 100.0 / source.featureCount() if source.featureCount() else 0
current_progress = 0
for current, f in enumerate(source.getFeatures()):
if feedback.isCanceled():
break

current_progress = total * current
feedback.setProgress(current_progress)

expressionContext.setFeature(f)
value = expression.evaluate(expressionContext)
if expression.hasEvalError():
Expand All @@ -150,7 +154,7 @@ def processAlgorithm(self, parameters, context, feedback):
pointCount = int(round(value * da.measureArea(fGeom)))

if pointCount == 0:
feedback.pushInfo("Skip feature {} as number of points for it is 0.")
feedback.pushInfo("Skip feature {} as number of points for it is 0.".format(f.id()))
continue

index = QgsSpatialIndex()
Expand All @@ -159,7 +163,7 @@ def processAlgorithm(self, parameters, context, feedback):
nPoints = 0
nIterations = 0
maxIterations = pointCount * 200
total = 100.0 / pointCount if pointCount else 1
feature_total = total / pointCount if pointCount else 1

random.seed()

Expand All @@ -183,13 +187,13 @@ def processAlgorithm(self, parameters, context, feedback):
index.insertFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
feedback.setProgress(current_progress + int(nPoints * feature_total))
nIterations += 1

if nPoints < pointCount:
feedback.pushInfo(self.tr('Could not generate requested number of random '
'points. Maximum number of attempts exceeded.'))

feedback.setProgress(0)
feedback.setProgress(100)

return {self.OUTPUT: dest_id}

0 comments on commit 81aa2f9

Please sign in to comment.