2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RandomSelection.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
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__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-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2017-06-23 14:34:38 +10:00
|
|
|
from qgis.core import QgsFeatureSink, QgsProcessingUtils
|
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
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.outputs import OutputVector
|
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 RandomSelection(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
NUMBER = 'NUMBER'
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'random_selection.png'))
|
|
|
|
|
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')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Method'), self.methods, 0))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.NUMBER,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Number/percentage of selected features'), 0, None, 10))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Selection'), True))
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'randomselection'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random selection')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2012-10-03 18:58:56 +03:00
|
|
|
filename = self.getParameterValue(self.INPUT)
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(filename, context)
|
2012-09-15 18:25:25 +03:00
|
|
|
method = self.getParameterValue(self.METHOD)
|
2012-10-03 18:58:56 +03:00
|
|
|
|
|
|
|
featureCount = layer.featureCount()
|
|
|
|
value = int(self.getParameterValue(self.NUMBER))
|
|
|
|
|
2013-05-11 20:40:57 +05:30
|
|
|
layer.removeSelection()
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
if method == 0:
|
2012-10-03 18:58:56 +03:00
|
|
|
if value > featureCount:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Selected number is greater than feature count. '
|
|
|
|
'Choose a lower value and try again.'))
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2012-10-03 18:58:56 +03:00
|
|
|
if value > 100:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr("Percentage can't be greater than 100. Set a "
|
|
|
|
"different value and try again."))
|
2016-02-17 09:36:59 +02:00
|
|
|
value = int(round(value / 100.0, 4) * featureCount)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
selran = random.sample(list(range(featureCount)), value)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-08-06 15:44:34 +02:00
|
|
|
layer.selectByIds(selran)
|
2012-10-03 18:58:56 +03:00
|
|
|
self.setOutputValue(self.OUTPUT, filename)
|