Skip to content

Commit 54e30b9

Browse files
committedApr 20, 2017
improvements for extracts within subsets
1 parent 02dade8 commit 54e30b9

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed
 

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

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import random
3030

3131
from qgis.core import (QgsApplication)
32+
from collections import defaultdict
3233
from processing.core.GeoAlgorithm import GeoAlgorithm
3334
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3435
from processing.core.parameters import ParameterSelection
@@ -106,38 +107,23 @@ def processAlgorithm(self, feedback):
106107
layer.fields().toList(), layer.wkbType(), layer.crs())
107108

108109
selran = []
109-
current = 0
110110
total = 100.0 / (featureCount * len(unique))
111111
features = vector.features(layer)
112112

113-
if not len(unique) == featureCount:
114-
for classValue in unique:
115-
classFeatures = []
116-
for i, feature in enumerate(features):
117-
attrs = feature.attributes()
118-
if attrs[index] == classValue:
119-
classFeatures.append(i)
120-
current += 1
121-
feedback.setProgress(int(current * total))
122-
123-
if method == 1:
124-
selValue = int(round(value * len(classFeatures), 0))
125-
else:
126-
selValue = value
127-
128-
if selValue >= len(classFeatures):
129-
selFeat = classFeatures
130-
else:
131-
selFeat = random.sample(classFeatures, selValue)
132-
133-
selran.extend(selFeat)
134-
else:
135-
selran = list(range(featureCount))
113+
classes = defaultdict(list)
114+
for i, feature in enumerate(features):
115+
attrs = feature.attributes()
116+
classes[attrs[index]].append(feature)
117+
feedback.setProgress(int(i * total))
118+
119+
for subset in classes.values():
120+
selValue = value if method != 1 else int(round(value * len(subset), 0))
121+
selran.extend(random.sample(subset, selValue))
122+
136123

137124
features = vector.features(layer)
138125
total = 100.0 / len(features)
139-
for (i, feat) in enumerate(features):
140-
if i in selran:
141-
writer.addFeature(feat)
126+
for (i, feat) in enumerate(selran):
127+
writer.addFeature(feat)
142128
feedback.setProgress(int(i * total))
143129
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.