Skip to content

Commit

Permalink
[processing] Fix random selection algorithm when filter is set on layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 28, 2018
1 parent 977e14b commit 31330dd
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions python/plugins/processing/algs/qgis/RandomSelection.py
Expand Up @@ -93,11 +93,11 @@ def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
method = self.parameterAsEnum(parameters, self.METHOD, context)

featureCount = layer.featureCount()
ids = layer.allFeatureIds()
value = self.parameterAsInt(parameters, self.NUMBER, context)

if method == 0:
if value > featureCount:
if value > len(ids):
raise QgsProcessingException(
self.tr('Selected number is greater than feature count. '
'Choose a lower value and try again.'))
Expand All @@ -106,9 +106,9 @@ def processAlgorithm(self, parameters, context, feedback):
raise QgsProcessingException(
self.tr("Percentage can't be greater than 100. Set a "
"different value and try again."))
value = int(round(value / 100.0, 4) * featureCount)
value = int(round(value / 100.0, 4) * len(ids))

selran = random.sample(list(range(featureCount)), value)
selran = random.sample(ids, value)

layer.selectByIds(selran)
return {self.OUTPUT: parameters[self.INPUT]}

0 comments on commit 31330dd

Please sign in to comment.