Skip to content
Permalink
Browse files

[processing] Misc fixes and improvements to Random Points in Polygons

- 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 81aa2f956e1db3ea8ceca0ef2bc480783cedf3db
Showing with 8 additions and 4 deletions.
  1. +8 −4 python/plugins/processing/algs/qgis/RandomPointsPolygons.py
@@ -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():
@@ -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()
@@ -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()

@@ -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.
You can’t perform that action at this time.