diff --git a/src/processing_provider/change_reach_direction.py b/src/processing_provider/change_reach_direction.py new file mode 100644 index 00000000..0c539e27 --- /dev/null +++ b/src/processing_provider/change_reach_direction.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +""" +/*************************************************************************** + QGEP processing provider + ------------------- + begin : 18.11.2017 + copyright : (C) 2017 by OPENGIS.ch + email : matthias@opengis.ch + ***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ +""" + +from qgis.core import ( + QgsExpression, + QgsFeatureRequest, + QgsGeometry, + QgsProcessingAlgorithm, + QgsProcessingParameterNumber, + QgsProcessingParameterBoolean, + QgsProcessingParameterVectorLayer +) + +from PyQt5.QtCore import QCoreApplication + +from .qgep_algorithm import QgepAlgorithm + +__author__ = 'Matthias Kuhn & Maxime Trolliet' +__date__ = '2018-08-07' +__copyright__ = '(C) 2018 by OPENGIS.ch' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + + +class ChangeReachDirection(QgepAlgorithm): + """ + Change the direction of the selected reaches + """ + + REACH_LAYER = 'REACH_LAYER' + + + def name(self): + return 'change_direction' + + def displayName(self): + return self.tr('Change reach direction') + + def flags(self): + return super().flags() | QgsProcessingAlgorithm.FlagNoThreading + + def initAlgorithm(self, config=None): + """Here we define the inputs and output of the algorithm, along + with some other properties. + """ + + self.addParameter(QgsProcessingParameterVectorLayer(self.REACH_LAYER, description=self.tr( + 'Selected features only - Reach layer, will be modified in place and its direction will be inverted'))) + + def processAlgorithm(self, parameters, context, feedback): + """Here is where the processing itself takes place.""" + reach_layer = self.parameterAsVectorLayer(parameters, self.REACH_LAYER, context) + + reach_layer.startEditing() + + feature_count = 0 + + iterator = reach_layer.getSelectedFeatures() + feature_count = reach_layer.selectedFeatureCount() + + # Loop through relevant reaches + reach_layer.beginEditCommand('change directions') + transaction = reach_layer.dataProvider().transaction() + #if not transaction: + # raise Exception: if there is no transaction, complain to the user! + selected_obj_ids = [feature['obj_id'] for feature in iterator] + transaction.executeSql('SELECT qgep_od.reach_direction_change(\'{{{obj_ids}}}\');'.format(obj_ids=','.join(selected_obj_ids))) + reach_layer.endEditCommand() + feedback.setProgress(100) + + return {} diff --git a/src/processing_provider/provider.py b/src/processing_provider/provider.py index fae5ef1b..efaaebe0 100644 --- a/src/processing_provider/provider.py +++ b/src/processing_provider/provider.py @@ -22,6 +22,7 @@ from qgis.core import QgsProcessingProvider from .snap_reach import SnapReachAlgorithm from .flow_times import FlowTimesAlgorithm +from .change_reach_direction import ChangeReachDirection from PyQt5.QtGui import QIcon import os @@ -43,12 +44,12 @@ def __init__(self): self.activate = True # Load algorithms - self.alglist = [SnapReachAlgorithm(), FlowTimesAlgorithm()] + self.alglist = [SnapReachAlgorithm(), FlowTimesAlgorithm(), ChangeReachDirection()] for alg in self.alglist: alg.provider = self def getAlgs(self): - algs = [SnapReachAlgorithm(), FlowTimesAlgorithm()] + algs = [SnapReachAlgorithm(), FlowTimesAlgorithm(), ChangeReachDirection()] return algs def id(self):