Skip to content

Commit

Permalink
improvements for extracts within subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Apr 20, 2017
1 parent 02dade8 commit 54e30b9
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions python/plugins/processing/algs/qgis/RandomExtractWithinSubsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import random

from qgis.core import (QgsApplication)
from collections import defaultdict
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterSelection
Expand Down Expand Up @@ -106,38 +107,23 @@ def processAlgorithm(self, feedback):
layer.fields().toList(), layer.wkbType(), layer.crs())

selran = []
current = 0
total = 100.0 / (featureCount * len(unique))
features = vector.features(layer)

if not len(unique) == featureCount:
for classValue in unique:
classFeatures = []
for i, feature in enumerate(features):
attrs = feature.attributes()
if attrs[index] == classValue:
classFeatures.append(i)
current += 1
feedback.setProgress(int(current * total))

if method == 1:
selValue = int(round(value * len(classFeatures), 0))
else:
selValue = value

if selValue >= len(classFeatures):
selFeat = classFeatures
else:
selFeat = random.sample(classFeatures, selValue)

selran.extend(selFeat)
else:
selran = list(range(featureCount))
classes = defaultdict(list)
for i, feature in enumerate(features):
attrs = feature.attributes()
classes[attrs[index]].append(feature)
feedback.setProgress(int(i * total))

for subset in classes.values():
selValue = value if method != 1 else int(round(value * len(subset), 0))
selran.extend(random.sample(subset, selValue))


features = vector.features(layer)
total = 100.0 / len(features)
for (i, feat) in enumerate(features):
if i in selran:
writer.addFeature(feat)
for (i, feat) in enumerate(selran):
writer.addFeature(feat)
feedback.setProgress(int(i * total))
del writer

0 comments on commit 54e30b9

Please sign in to comment.