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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
2012-10-03 18:58:56 +03:00
|
|
|
import random
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2018-05-15 15:48:28 +07:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsFeatureRequest,
|
2017-08-02 23:09:47 +10:00
|
|
|
QgsProcessingException,
|
|
|
|
QgsProcessingUtils,
|
2018-01-29 12:21:41 +10:00
|
|
|
QgsProcessingAlgorithm,
|
2017-08-02 23:09:47 +10:00
|
|
|
QgsProcessingParameterVectorLayer,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputVectorLayer)
|
|
|
|
from collections import defaultdict
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class RandomSelectionWithinSubsets(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
NUMBER = 'NUMBER'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2018-05-15 15:48:28 +07:00
|
|
|
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmSelectRandom.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("/algorithms/mAlgorithmSelectRandom.svg")
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector selection')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectorselection'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
2018-01-29 12:21:41 +10:00
|
|
|
def flags(self):
|
|
|
|
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2015-08-31 16:59:11 +02:00
|
|
|
self.methods = [self.tr('Number of selected features'),
|
|
|
|
self.tr('Percentage of selected features')]
|
|
|
|
|
2017-08-02 23:09:47 +10:00
|
|
|
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
|
|
|
self.tr('ID field'), None, self.INPUT))
|
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.METHOD,
|
|
|
|
self.tr('Method'), self.methods, False, 0))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.NUMBER,
|
|
|
|
self.tr('Number/percentage of selected features'),
|
|
|
|
QgsProcessingParameterNumber.Integer,
|
|
|
|
10, False, 0.0, 999999999999.0))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (stratified random)')))
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'randomselectionwithinsubsets'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random selection within subsets')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-08-02 23:09:47 +10:00
|
|
|
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
|
|
|
|
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
|
|
|
field = self.parameterAsString(parameters, self.FIELD, context)
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2016-09-22 12:09:13 +02:00
|
|
|
index = layer.fields().lookupField(field)
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2017-08-02 23:09:47 +10:00
|
|
|
unique = layer.uniqueValues(index)
|
2012-10-03 18:58:56 +03:00
|
|
|
featureCount = layer.featureCount()
|
|
|
|
|
2017-08-02 23:09:47 +10:00
|
|
|
value = self.parameterAsInt(parameters, self.NUMBER, context)
|
2012-10-03 18:58:56 +03:00
|
|
|
if method == 0:
|
|
|
|
if value > featureCount:
|
2017-08-02 23:09:47 +10:00
|
|
|
raise QgsProcessingException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Selected number is greater that feature count. '
|
|
|
|
'Choose lesser value and try again.'))
|
2012-10-03 18:58:56 +03:00
|
|
|
else:
|
|
|
|
if value > 100:
|
2017-08-02 23:09:47 +10:00
|
|
|
raise QgsProcessingException(
|
2015-01-18 20:47:22 +01:00
|
|
|
self.tr("Percentage can't be greater than 100. Set a "
|
2015-01-15 20:41:15 +02:00
|
|
|
"different value and try again."))
|
2012-10-03 18:58:56 +03:00
|
|
|
value = value / 100.0
|
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / (featureCount * len(unique)) if featureCount else 1
|
2012-10-03 18:58:56 +03:00
|
|
|
|
|
|
|
if not len(unique) == featureCount:
|
2017-08-02 23:09:47 +10:00
|
|
|
classes = defaultdict(list)
|
|
|
|
|
|
|
|
features = layer.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([index]))
|
|
|
|
|
|
|
|
for i, feature in enumerate(features):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
|
|
|
classes[feature.attributes()[index]].append(feature.id())
|
|
|
|
feedback.setProgress(int(i * total))
|
|
|
|
|
|
|
|
selran = []
|
2018-07-04 19:15:29 +03:00
|
|
|
for k, subset in classes.items():
|
2017-08-02 23:09:47 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
|
|
|
selValue = value if method != 1 else int(round(value * len(subset), 0))
|
2018-07-04 19:15:29 +03:00
|
|
|
if selValue > len(subset):
|
|
|
|
selValue = len(subset)
|
|
|
|
feedback.reportError(self.tr('Subset "{}" is smaller than requested number of features.'.format(k)))
|
|
|
|
selran.extend(random.sample(subset, selValue))
|
2017-08-02 23:09:47 +10:00
|
|
|
|
2016-08-06 15:44:34 +02:00
|
|
|
layer.selectByIds(selran)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2016-09-21 18:24:26 +02:00
|
|
|
layer.selectByIds(list(range(featureCount))) # FIXME: implies continuous feature ids
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-08-02 23:09:47 +10:00
|
|
|
return {self.OUTPUT: parameters[self.INPUT]}
|