Skip to content

Commit 81aa2f9

Browse files
committedFeb 14, 2018
[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
1 parent e0cf578 commit 81aa2f9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎python/plugins/processing/algs/qgis/RandomPointsPolygons.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ def processAlgorithm(self, parameters, context, feedback):
131131
da.setEllipsoid(context.project().ellipsoid())
132132

133133
total = 100.0 / source.featureCount() if source.featureCount() else 0
134+
current_progress = 0
134135
for current, f in enumerate(source.getFeatures()):
135136
if feedback.isCanceled():
136137
break
137138

139+
current_progress = total * current
140+
feedback.setProgress(current_progress)
141+
138142
expressionContext.setFeature(f)
139143
value = expression.evaluate(expressionContext)
140144
if expression.hasEvalError():
@@ -150,7 +154,7 @@ def processAlgorithm(self, parameters, context, feedback):
150154
pointCount = int(round(value * da.measureArea(fGeom)))
151155

152156
if pointCount == 0:
153-
feedback.pushInfo("Skip feature {} as number of points for it is 0.")
157+
feedback.pushInfo("Skip feature {} as number of points for it is 0.".format(f.id()))
154158
continue
155159

156160
index = QgsSpatialIndex()
@@ -159,7 +163,7 @@ def processAlgorithm(self, parameters, context, feedback):
159163
nPoints = 0
160164
nIterations = 0
161165
maxIterations = pointCount * 200
162-
total = 100.0 / pointCount if pointCount else 1
166+
feature_total = total / pointCount if pointCount else 1
163167

164168
random.seed()
165169

@@ -183,13 +187,13 @@ def processAlgorithm(self, parameters, context, feedback):
183187
index.insertFeature(f)
184188
points[nPoints] = p
185189
nPoints += 1
186-
feedback.setProgress(int(nPoints * total))
190+
feedback.setProgress(current_progress + int(nPoints * feature_total))
187191
nIterations += 1
188192

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

193-
feedback.setProgress(0)
197+
feedback.setProgress(100)
194198

195199
return {self.OUTPUT: dest_id}

0 commit comments

Comments
 (0)
Please sign in to comment.