2013-10-07 18:41:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RandomExtract.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
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
from qgis.core import (QgsFeatureSink,
|
2017-07-13 08:55:17 +03:00
|
|
|
QgsProcessingException,
|
2017-06-22 18:23:38 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterNumber,
|
2017-07-12 07:39:43 +10:00
|
|
|
QgsProcessingParameterFeatureSink)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class RandomExtract(QgisAlgorithm):
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
NUMBER = 'NUMBER'
|
|
|
|
|
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__()
|
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-06-22 18:23:38 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
|
2017-06-22 18:23:38 +10:00
|
|
|
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)')))
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'randomextract'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random extract')
|
|
|
|
|
2017-07-01 12:21:11 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
|
|
|
|
|
|
|
features = source.getFeatures()
|
|
|
|
featureCount = source.featureCount()
|
|
|
|
value = self.parameterAsInt(parameters, self.NUMBER, context)
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-07-01 12:21:11 +10:00
|
|
|
if method == 0:
|
|
|
|
if value > featureCount:
|
2017-07-13 08:55:17 +03:00
|
|
|
raise QgsProcessingException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Selected number is greater than feature count. '
|
|
|
|
'Choose a lower value and try again.'))
|
2013-10-07 18:41:07 +02:00
|
|
|
else:
|
2017-07-01 12:21:11 +10:00
|
|
|
if value > 100:
|
2017-07-13 08:55:17 +03:00
|
|
|
raise QgsProcessingException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr("Percentage can't be greater than 100. Set a "
|
|
|
|
"different value and try again."))
|
2017-07-01 12:21:11 +10:00
|
|
|
value = int(round(value / 100.0000, 4) * featureCount)
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-07-01 12:21:11 +10:00
|
|
|
selran = random.sample(list(range(featureCount)), value)
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-07-01 12:21:11 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
source.fields(), source.wkbType(), source.sourceCrs())
|
2013-10-24 15:26:39 +02:00
|
|
|
|
2017-07-01 12:21:11 +10:00
|
|
|
total = 100.0 / featureCount if featureCount else 1
|
2016-02-17 09:36:59 +02:00
|
|
|
for i, feat in enumerate(features):
|
2017-06-22 18:23:38 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2013-10-07 18:41:07 +02:00
|
|
|
if i in selran:
|
2017-07-01 12:21:11 +10:00
|
|
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(i * total))
|
2017-07-01 12:21:11 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|