Skip to content
Permalink
Browse files

review Random selection and Random selection within subsets

  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 708b679 commit 9f13601f801bbf944ed2f77f713d0e41c4013ff2
@@ -1,46 +1,62 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
import random

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterNumber import ParameterNumber
import random

from sextante.outputs.OutputVector import OutputVector

class RandomSelection(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"
METHOD = "METHOD"
NUMBER = "NUMBER"
PERCENTAGE = "PERCENTAGE"

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"
self.group = "Research tools"

self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(self.METHOD, "Method", self.METHODS, 0))
self.addParameter(ParameterNumber(self.NUMBER, "Number/persentage of selected features", 1, None, 10))
self.addOutput(OutputVector(self.OUTPUT, "Selection", True))

def processAlgorithm(self, progress):
filename = self.getParameterValue(RandomSelection.INPUT)
filename = self.getParameterValue(self.INPUT)
layer = QGisLayers.getObjectFromUri(filename)
method = self.getParameterValue(self.METHOD)

featureCount = layer.featureCount()

value = int(self.getParameterValue(self.NUMBER))

if method == 0:
value = int(self.getParameterValue(self.NUMBER))
if value > featureCount:
raise GeoAlgorithmExecutionException("Selected number is greater that feature count. Choose lesser value and try again.")
else:
value = self.getParameterValue(self.PERCENTAGE)
value = int(round((value / 100.0000), 4) * layer.featureCount())
selran = random.sample(xrange(0, layer.featureCount()), value)
layer.setSelectedFeatures(selran)
self.setOutputValue(self.OUTPUT, filename)
if value > 100:
raise GeoAlgorithmExecutionException("Persentage can't be greater than 100. Set corrent value and try again.")
value = int(round((value / 100.0000), 4) * featureCount)

selran = random.sample(xrange(0, featureCount), value)

def defineCharacteristics(self):
self.name = "Random selection"
self.group = "Research tools"
self.addParameter(ParameterVector(RandomSelection.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(RandomSelection.METHOD, "Method", ["Number of selected features", "Percentage of selected features"]))
self.addParameter(ParameterNumber(RandomSelection.NUMBER, "Number of selected features", 1, None, 10))
self.addParameter(ParameterNumber(RandomSelection.PERCENTAGE, "Percentage of selected features", 0, 100, 50))
self.addOutput(OutputVector(RandomSelection.OUTPUT, "Selection", True))
layer.setSelectedFeatures(selran)
self.setOutputValue(self.OUTPUT, filename)
@@ -1,78 +1,102 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
import random

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterNumber import ParameterNumber
import random
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.ftools import ftools_utils

from sextante.outputs.OutputVector import OutputVector

class RandomSelectionWithinSubsets(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"
METHOD = "METHOD"
NUMBER = "NUMBER"
PERCENTAGE = "PERCENTAGE"
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 = "Research 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/persentage 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)
field = self.getParameterValue(RandomSelectionWithinSubsets.FIELD)
filename = self.getParameterValue(RandomSelectionWithinSubsets.INPUT)
vlayer = QGisLayers.getObjectFromUri(filename)
vlayer.removeSelection(True)
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select(allAttrs)
index = vprovider.fieldNameIndex(field)
unique = ftools_utils.getUniqueValues(vprovider, int(index))
inFeat = QgsFeature()

layer.removeSelection(True)
provider = layer.dataProvider()
index = layer.fieldNameIndex(field)
layer.select([index])

unique = layer.uniqueValues(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 = []
nFeat = vprovider.featureCount() * len(unique)
nElement = 0
if not len(unique) == vlayer.featureCount():
inFeat = QgsFeature()

current = 0
total = 100.0 / float(featureCount * len(unique))

if not len(unique) == featureCount:
for i in unique:
vprovider.rewind()
FIDs= []
while vprovider.nextFeature(inFeat):
layer.select([index])
while layer.nextFeature(inFeat):
atMap = inFeat.attributeMap()
if atMap[index] == QVariant(i):
FID = inFeat.id()
FIDs.append(FID)
nElement += 1
progress.setPercentage(nElement/nFeat * 100)
if method == 0:
value = int(self.getParameterValue(self.NUMBER))
FIDs.append(inFeat.id())
current += 1
progress.setPercentage(int(current * total))

if method == 1:
selValue = int(round(value * len(FIDs), 0))
else:
value = self.getParameterValue(self.PERCENTAGE)
value = int(round((value / 100.0000) * len(FIDs), 0))
if value >= len(FIDs):
selValue = value

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

selran.extend(selFeat)
vlayer.setSelectedFeatures(selran)
layer.setSelectedFeatures(selran)
else:
vlayer.setSelectedFeatures(range(0, vlayer.featureCount()))
self.setOutputValue(RandomSelectionWithinSubsets.OUTPUT, filename)
layer.setSelectedFeatures(range(0, featureCount))


def defineCharacteristics(self):
self.name = "Random selection within subsets"
self.group = "Research tools"
self.addParameter(ParameterVector(RandomSelectionWithinSubsets.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterTableField(RandomSelectionWithinSubsets.FIELD, "ID Field", RandomSelectionWithinSubsets.INPUT))
self.addParameter(ParameterSelection(RandomSelectionWithinSubsets.METHOD, "Method", ["Number of selected features", "Percentage of selected features"]))
self.addParameter(ParameterNumber(RandomSelectionWithinSubsets.NUMBER, "Number of selected features", 1, None, 10))
self.addParameter(ParameterNumber(RandomSelectionWithinSubsets.PERCENTAGE, "Percentage of selected features", 0, 100, 50))
self.addOutput(OutputVector(RandomSelectionWithinSubsets.OUTPUT, "Selection", True))
self.setOutputValue(self.OUTPUT, filename)

0 comments on commit 9f13601

Please sign in to comment.
You can’t perform that action at this time.