QGIS/python/plugins/processing/algs/qgis/RandomSelection.py

88 lines
3.5 KiB
Python
Raw Normal View History

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. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
import random
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
2012-09-15 18:25:25 +03:00
class RandomSelection(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
METHOD = 'METHOD'
NUMBER = 'NUMBER'
def defineCharacteristics(self):
self.allowOnlyOpenedLayers = True
self.name, self.i18n_name = self.trAlgorithm('Random selection')
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
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,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterSelection(self.METHOD,
self.tr('Method'), self.methods, 0))
self.addParameter(ParameterNumber(self.NUMBER,
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-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
filename = self.getParameterValue(self.INPUT)
layer = dataobjects.getObjectFromUri(filename)
2012-09-15 18:25:25 +03:00
method = self.getParameterValue(self.METHOD)
featureCount = layer.featureCount()
value = int(self.getParameterValue(self.NUMBER))
2013-05-11 20:40:57 +05:30
layer.removeSelection()
2012-09-15 18:25:25 +03:00
if method == 0:
if value > featureCount:
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:
if value > 100:
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."))
value = int(round(value / 100.0000, 4) * featureCount)
2012-09-15 18:25:25 +03:00
selran = random.sample(xrange(0, featureCount), value)
2012-09-15 18:25:25 +03:00
layer.setSelectedFeatures(selran)
self.setOutputValue(self.OUTPUT, filename)