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'
|
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$'
|
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
import random
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from qgis.core import *
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2013-10-01 20:52:22 +03: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
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class RandomSelection(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
NUMBER = 'NUMBER'
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
METHODS = ['Number of selected features',
|
|
|
|
'Percentage of selected features']
|
2012-10-03 18:58:56 +03:00
|
|
|
def defineCharacteristics(self):
|
2013-07-23 18:57:06 +02:00
|
|
|
self.allowOnlyOpenedLayers = True
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Random selection'
|
|
|
|
self.group = 'Vector selection tools'
|
|
|
|
|
|
|
|
self.addParameter(ParameterVector(self.INPUT, 'Input layer',
|
|
|
|
[ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addParameter(ParameterSelection(self.METHOD, 'Method',
|
|
|
|
self.METHODS, 0))
|
|
|
|
self.addParameter(ParameterNumber(self.NUMBER,
|
|
|
|
'Number/percentage of selected features', 0, None,
|
|
|
|
10))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, 'Selection', True))
|
2012-10-03 18:58:56 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def processAlgorithm(self, progress):
|
2012-10-03 18:58:56 +03:00
|
|
|
filename = self.getParameterValue(self.INPUT)
|
2013-09-12 13:19:00 +02:00
|
|
|
layer = dataobjects.getObjectFromUri(filename)
|
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(
|
|
|
|
'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(
|
|
|
|
"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
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
selran = random.sample(xrange(0, featureCount), value)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-10-03 18:58:56 +03:00
|
|
|
layer.setSelectedFeatures(selran)
|
|
|
|
self.setOutputValue(self.OUTPUT, filename)
|