2013-10-07 18:41:07 +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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import range
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
__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
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
2017-04-25 17:53:32 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-06-22 18:23:38 +10:00
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputVectorLayer)
|
2017-04-20 08:47:19 +02:00
|
|
|
from collections import defaultdict
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class RandomExtractWithinSubsets(QgisAlgorithm):
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
NUMBER = 'NUMBER'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector selection tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.methods = [self.tr('Number of selected features'),
|
|
|
|
self.tr('Percentage of selected features')]
|
|
|
|
|
2017-06-22 18:23:38 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-06-22 18:23:38 +10:00
|
|
|
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.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Extracted (random stratified)')))
|
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Extracted (random stratified)')))
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = None
|
|
|
|
self.method = None
|
|
|
|
self.field = None
|
|
|
|
self.value = None
|
|
|
|
self.sink = None
|
|
|
|
self.dest_id = None
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'randomextractwithinsubsets'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random extract within subsets')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
self.method = self.parameterAsEnum(parameters, self.METHOD, context)
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.field = self.parameterAsString(parameters, self.FIELD, context)
|
|
|
|
self.value = self.parameterAsInt(parameters, self.NUMBER, context)
|
|
|
|
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
self.source.fields(), self.source.wkbType(), self.source.sourceCrs())
|
|
|
|
return True
|
2013-10-24 15:26:39 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
index = self.source.fields().lookupField(self.field)
|
2013-10-24 15:26:39 +02:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
features = self.source.getFeatures()
|
|
|
|
featureCount = self.source.featureCount()
|
|
|
|
unique = self.source.uniqueValues(index)
|
|
|
|
if self.method == 0:
|
|
|
|
if self.value > featureCount:
|
2013-10-07 18:41:07 +02:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Selected number is greater that feature count. '
|
|
|
|
'Choose lesser value and try again.'))
|
2013-10-07 18:41:07 +02:00
|
|
|
else:
|
2017-06-28 19:55:08 +10:00
|
|
|
if self.value > 100:
|
2013-10-07 18:41:07 +02:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr("Percentage can't be greater than 100. Set "
|
|
|
|
"correct value and try again."))
|
2017-06-28 19:55:08 +10:00
|
|
|
self.value = self.value / 100.0
|
2013-10-24 15:26:39 +02:00
|
|
|
|
|
|
|
selran = []
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / (featureCount * len(unique)) if featureCount else 1
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-04-20 08:47:19 +02:00
|
|
|
classes = defaultdict(list)
|
|
|
|
for i, feature in enumerate(features):
|
2017-06-22 18:23:38 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2017-04-20 08:47:19 +02:00
|
|
|
attrs = feature.attributes()
|
|
|
|
classes[attrs[index]].append(feature)
|
|
|
|
feedback.setProgress(int(i * total))
|
|
|
|
|
|
|
|
for subset in classes.values():
|
2017-06-28 19:55:08 +10:00
|
|
|
selValue = self.value if self.method != 1 else int(round(self.value * len(subset), 0))
|
2017-04-20 08:47:19 +02:00
|
|
|
selran.extend(random.sample(subset, selValue))
|
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / featureCount if featureCount else 1
|
2017-04-20 08:47:19 +02:00
|
|
|
for (i, feat) in enumerate(selran):
|
2017-06-22 18:23:38 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2017-06-28 19:55:08 +10:00
|
|
|
self.sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(i * total))
|
2017-06-28 19:55:08 +10:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.OUTPUT: self.dest_id}
|