Skip to content

Commit

Permalink
Merge pull request #21 from QGEP/inverseReaches
Browse files Browse the repository at this point in the history
Add processing algorithm to reverse reaches
  • Loading branch information
m-kuhn authored Aug 24, 2018
2 parents 8999084 + 92e18fa commit ff0ee14
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
91 changes: 91 additions & 0 deletions src/processing_provider/change_reach_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-

"""
/***************************************************************************
QGEP processing provider
-------------------
begin : 18.11.2017
copyright : (C) 2017 by OPENGIS.ch
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 {}
5 changes: 3 additions & 2 deletions src/processing_provider/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit ff0ee14

Please sign in to comment.