-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
RandomSelectionWithinSubsets.py
123 lines (99 loc) · 4.75 KB
/
RandomSelectionWithinSubsets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
"""
***************************************************************************
RandomSelectionWithinSubsets.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import random
from PyQt4.QtCore import *
from qgis.core import *
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.algs.ftools import FToolsUtils as utils
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.outputs.OutputVector import OutputVector
class RandomSelectionWithinSubsets(GeoAlgorithm):
INPUT = "INPUT"
METHOD = "METHOD"
NUMBER = "NUMBER"
FIELD = "FIELD"
OUTPUT = "OUTPUT"
METHODS = ["Number of selected features",
"Percentage of selected features"
]
#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/random_selection.png")
#===========================================================================
def defineCharacteristics(self):
self.name = "Random selection within subsets"
self.group = "Vector selection tools"
self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterTableField(self.FIELD, "ID Field", self.INPUT))
self.addParameter(ParameterSelection(self.METHOD, "Method", self.METHODS, 0))
self.addParameter(ParameterNumber(self.NUMBER, "Number/percentage of selected features", 1, None, 10))
self.addOutput(OutputVector(self.OUTPUT, "Selection", True))
def processAlgorithm(self, progress):
filename = self.getParameterValue(self.INPUT)
layer = QGisLayers.getObjectFromUri(filename)
field = self.getParameterValue(self.FIELD)
method = self.getParameterValue(self.METHOD)
layer.removeSelection()
index = layer.fieldNameIndex(field)
unique = utils.getUniqueValues(layer, index)
featureCount = layer.featureCount()
value = int(self.getParameterValue(self.NUMBER))
if method == 0:
if value > featureCount:
raise GeoAlgorithmExecutionException("Selected number is greater that feature count. Choose lesser value and try again.")
else:
if value > 100:
raise GeoAlgorithmExecutionException("Persentage can't be greater than 100. Set corrent value and try again.")
value = value / 100.0
selran = []
inFeat = QgsFeature()
current = 0
total = 100.0 / float(featureCount * len(unique))
features = QGisLayers.features(layer)
if not len(unique) == featureCount:
for i in unique:
FIDs= []
for inFeat in features:
attrs = inFeat.attributes()
if attrs[index] == QVariant(i):
FIDs.append(inFeat.id())
current += 1
progress.setPercentage(int(current * total))
if method == 1:
selValue = int(round(value * len(FIDs), 0))
else:
selValue = value
if selValue >= len(FIDs):
selFeat = FIDs
else:
selFeat = random.sample(FIDs, selValue)
selran.extend(selFeat)
layer.setSelectedFeatures(selran)
else:
layer.setSelectedFeatures(range(0, featureCount))
self.setOutputValue(self.OUTPUT, filename)