Skip to content

Commit 5ae2430

Browse files
alexbruynyalldawson
authored andcommitted
[processing] fix Random extract/select within subset when subset is
smaller than number of requested features (fix #19322) (cherry-picked from 29207a1)
1 parent ff6fd80 commit 5ae2430

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

python/plugins/processing/algs/qgis/RandomExtractWithinSubsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def processAlgorithm(self, parameters, context, feedback):
127127

128128
for subset in classes.values():
129129
selValue = value if method != 1 else int(round(value * len(subset), 0))
130-
selran.extend(random.sample(subset, selValue))
130+
selran.extend(random.sample(subset, min(selValue, len(subset))))
131131

132132
total = 100.0 / featureCount if featureCount else 1
133133
for (i, feat) in enumerate(selran):

python/plugins/processing/algs/qgis/RandomSelectionWithinSubsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def processAlgorithm(self, parameters, context, feedback):
138138
break
139139

140140
selValue = value if method != 1 else int(round(value * len(subset), 0))
141-
selran.extend(random.sample(subset, selValue))
141+
selran.extend(random.sample(subset, min(selValue, len(subset))))
142142

143143
layer.selectByIds(selran)
144144
else:

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,6 +3923,21 @@ tests:
39233923
name: points_weighted.gml
39243924
compare: false
39253925

3926+
- algorithm: qgis:randomextractwithinsubsets
3927+
name: Random extract within subset (subset smaller than number)
3928+
params:
3929+
FIELD: id2
3930+
INPUT:
3931+
name: points.gml
3932+
type: vector
3933+
METHOD: 0
3934+
NUMBER: 3
3935+
results:
3936+
OUTPUT:
3937+
type: vector
3938+
name: points.gml
3939+
compare: false
3940+
39263941
- algorithm: qgis:heatmapkerneldensityestimation
39273942
name: Heatmap (Kernel density estimation)
39283943
params:
@@ -5355,6 +5370,7 @@ tests:
53555370
compare:
53565371
fields:
53575372
fid: skip
5373+
53585374
- algorithm: native:difference
53595375
name: Test Difference B - A (basic)
53605376
params:

0 commit comments

Comments
 (0)