Skip to content
Permalink
Browse files

Merge pull request #4783 from nyalldawson/script

processing: restore script algorithm provider and tests
  • Loading branch information
nyalldawson committed Jun 27, 2017
2 parents 1468eef + 4f096a6 commit 7f7842a11d93675add44961f930587a51650b564
@@ -63,6 +63,9 @@
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RegularPoints import RegularPoints
from .SaveSelectedFeatures import SaveSelectedFeatures
from .SelectByAttribute import SelectByAttribute
from .SelectByExpression import SelectByExpression
from .SimplifyGeometries import SimplifyGeometries
from .Smooth import Smooth
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
@@ -98,7 +101,6 @@
# from .SpatialJoin import SpatialJoin
# from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
# from .TextToFloat import TextToFloat
# from .SelectByAttribute import SelectByAttribute
# from .GridLine import GridLine
# from .Gridify import Gridify
# from .HubDistancePoints import HubDistancePoints
@@ -110,7 +112,6 @@
# from .StatisticsByCategories import StatisticsByCategories
# from .EquivalentNumField import EquivalentNumField
# from .FieldsCalculator import FieldsCalculator
# from .SaveSelectedFeatures import SaveSelectedFeatures
# from .Explode import Explode
# from .FieldPyculator import FieldsPyculator
# from .JoinAttributes import JoinAttributes
@@ -128,7 +129,6 @@
# from .PointsToPaths import PointsToPaths
# from .SetVectorStyle import SetVectorStyle
# from .SetRasterStyle import SetRasterStyle
# from .SelectByExpression import SelectByExpression
# from .SelectByAttributeSum import SelectByAttributeSum
# from .HypsometricCurves import HypsometricCurves
# from .SplitWithLines import SplitWithLines
@@ -202,11 +202,10 @@ def getAlgs(self):
# ExtractByLocation(),
# SpatialJoin(),
# DeleteDuplicateGeometries(), TextToFloat(),
# SelectByAttribute(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# JoinAttributes(),
# Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
# StatisticsByCategories(), ConcaveHull(),
@@ -217,7 +216,7 @@ def getAlgs(self):
# RandomPointsPolygonsVariable(),
# RandomPointsAlongLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# SelectByExpression(), HypsometricCurves(),
# HypsometricCurves(),
# SplitWithLines(), CreateConstantRaster(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
@@ -265,6 +264,9 @@ def getAlgs(self):
RandomExtract(),
RandomExtractWithinSubsets(),
RegularPoints(),
SaveSelectedFeatures(),
SelectByAttribute(),
SelectByExpression(),
SimplifyGeometries(),
Smooth(),
SpatialiteExecuteSQL(),
@@ -25,30 +25,29 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from qgis.core import (QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector


class SaveSelectedFeatures(QgisAlgorithm):

OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'

def group(self):
return self.tr('Vector general tools')

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))

self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Selection')))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Selection')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Selection")))

def name(self):
return 'saveselectedfeatures'
@@ -57,20 +56,20 @@ def displayName(self):
return self.tr('Save selected features')

def processAlgorithm(self, parameters, context, feedback):
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vectorLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

vectorLayer = QgsProcessingUtils.mapLayerFromString(inputFilename, context)

writer = output.getVectorWriter(vectorLayer.fields(), vectorLayer.wkbType(), vectorLayer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
vectorLayer.fields(), vectorLayer.wkbType(), vectorLayer.sourceCrs())

features = vectorLayer.getSelectedFeatures()
count = int(vectorLayer.selectedFeatureCount())
if count == 0:
raise GeoAlgorithmExecutionException(self.tr('There are no selected features in the input layer.'))

total = 100.0 / count if count else 1
for current, feat in enumerate(features):
writer.addFeature(feat, QgsFeatureSink.FastInsert)
if feedback.isCanceled():
break

sink.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
del writer

return {self.OUTPUT: dest_id}
@@ -28,14 +28,14 @@
from qgis.core import (QgsApplication)
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsExpression,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterString
from processing.core.outputs import OutputVector


class SelectByAttribute(QgisAlgorithm):
@@ -82,15 +82,15 @@ def __init__(self):
self.tr('does not contain')
]

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input Layer')))
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Selection attribute'), self.INPUT))
self.addParameter(ParameterSelection(self.OPERATOR,
self.tr('Operator'), self.i18n_operators))
self.addParameter(ParameterString(self.VALUE, self.tr('Value')))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer')))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Selected (attribute)'), True))
self.addParameter(QgsProcessingParameterField(self.FIELD,
self.tr('Selection attribute'), parentLayerParameterName=self.INPUT))
self.addParameter(QgsProcessingParameterEnum(self.OPERATOR,
self.tr('Operator'), self.i18n_operators))
self.addParameter(QgsProcessingParameterString(self.VALUE, self.tr('Value')))

self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)')))

def name(self):
return 'selectbyattribute'
@@ -99,11 +99,11 @@ def displayName(self):
return self.tr('Select by attribute')

def processAlgorithm(self, parameters, context, feedback):
fileName = self.getParameterValue(self.INPUT)
layer = QgsProcessingUtils.mapLayerFromString(fileName, context)
fieldName = self.getParameterValue(self.FIELD)
operator = self.OPERATORS[self.getParameterValue(self.OPERATOR)]
value = self.getParameterValue(self.VALUE)
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

fieldName = self.parameterAsString(parameters, self.FIELD, context)
operator = self.OPERATORS[self.parameterAsEnum(parameters, self.OPERATOR, context)]
value = self.parameterAsString(parameters, self.VALUE, context)

fields = layer.fields()

@@ -135,4 +135,4 @@ def processAlgorithm(self, parameters, context, feedback):
raise GeoAlgorithmExecutionException(expression.parserErrorString())

layer.selectByExpression(expression_string)
self.setOutputValue(self.OUTPUT, fileName)
return {self.OUTPUT: parameters[self.INPUT]}
@@ -24,23 +24,21 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsExpression,
from qgis.core import (QgsExpression,
QgsVectorLayer,
QgsProcessingUtils)
QgsProcessingParameterVectorLayer,
QgsProcessingParameterExpression,
QgsProcessingParameterEnum,
QgsProcessingOutputVectorLayer)
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterExpression


class SelectByExpression(QgisAlgorithm):

LAYERNAME = 'LAYERNAME'
INPUT = 'INPUT'
EXPRESSION = 'EXPRESSION'
RESULT = 'RESULT'
OUTPUT = 'OUTPUT'
METHOD = 'METHOD'

def group(self):
@@ -53,13 +51,14 @@ def __init__(self):
self.tr('removing from current selection'),
self.tr('selecting within current selection')]

self.addParameter(ParameterVector(self.LAYERNAME,
self.tr('Input Layer')))
self.addParameter(ParameterExpression(self.EXPRESSION,
self.tr("Expression"), parent_layer=self.LAYERNAME))
self.addParameter(ParameterSelection(self.METHOD,
self.tr('Modify current selection by'), self.methods, 0))
self.addOutput(OutputVector(self.RESULT, self.tr('Selected (expression)'), True))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT, self.tr('Input layer')))

self.addParameter(QgsProcessingParameterExpression(self.EXPRESSION,
self.tr('Expression'), parentLayerParameterName=self.INPUT))
self.addParameter(QgsProcessingParameterEnum(self.METHOD,
self.tr('Modify current selection by'), self.methods, 0))

self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Selected (attribute)')))

def name(self):
return 'selectbyexpression'
@@ -68,9 +67,9 @@ def displayName(self):
return self.tr('Select by expression')

def processAlgorithm(self, parameters, context, feedback):
filename = self.getParameterValue(self.LAYERNAME)
layer = QgsProcessingUtils.mapLayerFromString(filename, context)
method = self.getParameterValue(self.METHOD)
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)

method = self.parameterAsEnum(parameters, self.METHOD, context)

if method == 0:
behavior = QgsVectorLayer.SetSelection
@@ -81,10 +80,10 @@ def processAlgorithm(self, parameters, context, feedback):
elif method == 3:
behavior = QgsVectorLayer.IntersectSelection

expression = self.getParameterValue(self.EXPRESSION)
expression = self.parameterAsString(parameters, self.EXPRESSION, context)
qExp = QgsExpression(expression)
if qExp.hasParserError():
raise GeoAlgorithmExecutionException(qExp.parserErrorString())

layer.selectByExpression(expression, behavior)
self.setOutputValue(self.RESULT, filename)
return {self.OUTPUT: parameters[self.INPUT]}

0 comments on commit 7f7842a

Please sign in to comment.
You can’t perform that action at this time.