2012-10-04 19:33:47 +02:00
|
|
|
# -*- 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$'
|
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
import random
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from qgis.core import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2013-09-12 13:19:00 +02:00
|
|
|
from processing.tools import dataobjects, vector
|
2013-09-11 19:32:38 +02:00
|
|
|
from processing.tools import vector as utils
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.parameters.ParameterSelection import ParameterSelection
|
|
|
|
from processing.parameters.ParameterVector import ParameterVector
|
|
|
|
from processing.parameters.ParameterNumber import ParameterNumber
|
|
|
|
from processing.parameters.ParameterTableField import ParameterTableField
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.outputs.OutputVector import OutputVector
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class RandomSelectionWithinSubsets(GeoAlgorithm):
|
|
|
|
|
|
|
|
INPUT = "INPUT"
|
|
|
|
METHOD = "METHOD"
|
|
|
|
NUMBER = "NUMBER"
|
|
|
|
FIELD = "FIELD"
|
2012-10-03 18:58:56 +03:00
|
|
|
OUTPUT = "OUTPUT"
|
|
|
|
|
|
|
|
METHODS = ["Number of selected features",
|
|
|
|
"Percentage of selected features"
|
|
|
|
]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-20 00:16:05 +01:00
|
|
|
#===========================================================================
|
|
|
|
# def getIcon(self):
|
|
|
|
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/random_selection.png")
|
|
|
|
#===========================================================================
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
def defineCharacteristics(self):
|
2013-07-23 18:57:06 +02:00
|
|
|
self.allowOnlyOpenedLayers = True
|
2012-10-03 18:58:56 +03:00
|
|
|
self.name = "Random selection within subsets"
|
2012-12-20 00:16:05 +01:00
|
|
|
self.group = "Vector selection tools"
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2013-08-06 15:06:35 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT, "Input layer", [ParameterVector.VECTOR_TYPE_ANY]))
|
2012-10-03 18:58:56 +03:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD, "ID Field", self.INPUT))
|
|
|
|
self.addParameter(ParameterSelection(self.METHOD, "Method", self.METHODS, 0))
|
2012-12-20 00:16:05 +01:00
|
|
|
self.addParameter(ParameterNumber(self.NUMBER, "Number/percentage of selected features", 1, None, 10))
|
2012-10-03 18:58:56 +03:00
|
|
|
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, "Selection", True))
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def processAlgorithm(self, progress):
|
2012-10-03 18:58:56 +03:00
|
|
|
filename = self.getParameterValue(self.INPUT)
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
layer = dataobjects.getObjectFromUri(filename)
|
2012-10-03 18:58:56 +03:00
|
|
|
field = self.getParameterValue(self.FIELD)
|
2012-09-15 18:25:25 +03:00
|
|
|
method = self.getParameterValue(self.METHOD)
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2013-05-11 20:40:57 +05:30
|
|
|
layer.removeSelection()
|
2012-10-03 18:58:56 +03:00
|
|
|
index = layer.fieldNameIndex(field)
|
|
|
|
|
2013-02-04 00:14:39 +01:00
|
|
|
unique = utils.getUniqueValues(layer, index)
|
2012-10-03 18:58:56 +03:00
|
|
|
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
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
selran = []
|
2012-10-03 18:58:56 +03:00
|
|
|
inFeat = QgsFeature()
|
|
|
|
|
|
|
|
current = 0
|
|
|
|
total = 100.0 / float(featureCount * len(unique))
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(layer)
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
if not len(unique) == featureCount:
|
2012-09-15 18:25:25 +03:00
|
|
|
for i in unique:
|
|
|
|
FIDs= []
|
2013-02-28 21:43:59 +04:00
|
|
|
for inFeat in features:
|
|
|
|
attrs = inFeat.attributes()
|
2013-06-03 21:25:22 +02:00
|
|
|
if attrs[index] == i:
|
2012-10-03 18:58:56 +03:00
|
|
|
FIDs.append(inFeat.id())
|
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
|
|
|
|
|
|
|
if method == 1:
|
|
|
|
selValue = int(round(value * len(FIDs), 0))
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2012-10-03 18:58:56 +03:00
|
|
|
selValue = value
|
|
|
|
|
|
|
|
if selValue >= len(FIDs):
|
2012-09-15 18:25:25 +03:00
|
|
|
selFeat = FIDs
|
|
|
|
else:
|
2012-10-03 18:58:56 +03:00
|
|
|
selFeat = random.sample(FIDs, selValue)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
selran.extend(selFeat)
|
2012-10-03 18:58:56 +03:00
|
|
|
layer.setSelectedFeatures(selran)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2012-10-03 18:58:56 +03:00
|
|
|
layer.setSelectedFeatures(range(0, featureCount))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
self.setOutputValue(self.OUTPUT, filename)
|