Skip to content

Commit d0625b8

Browse files
committedNov 18, 2015
[processing] Add algorithm for reversing line directions
1 parent 9009ef0 commit d0625b8

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed
 

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
from CheckValidity import CheckValidity
133133
from OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
134134
from Smooth import Smooth
135+
from ReverseLineDirection import ReverseLineDirection
135136

136137
pluginPath = os.path.normpath(os.path.join(
137138
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -180,7 +181,8 @@ def __init__(self):
180181
SelectByExpression(), HypsometricCurves(),
181182
SplitLinesWithLines(), CreateConstantRaster(),
182183
FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
183-
CheckValidity(), OrientedMinimumBoundingBox(), Smooth()
184+
CheckValidity(), OrientedMinimumBoundingBox(), Smooth(),
185+
ReverseLineDirection()
184186
]
185187

186188
if hasMatplotlib:
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ReverseLineDirection.py
6+
-----------------------
7+
Date : November 2015
8+
Copyright : (C) 2015 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'November 2015'
22+
__copyright__ = '(C) 2015, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QGis, QgsGeometry, QgsFeature
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
31+
from processing.core.parameters import ParameterVector, ParameterNumber
32+
from processing.core.outputs import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class ReverseLineDirection(GeoAlgorithm):
37+
38+
INPUT_LAYER = 'INPUT_LAYER'
39+
OUTPUT_LAYER = 'OUTPUT_LAYER'
40+
41+
def defineCharacteristics(self):
42+
self.name, self.i18n_name = self.trAlgorithm('Reverse line direction')
43+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
44+
45+
self.addParameter(ParameterVector(self.INPUT_LAYER,
46+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
47+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Reversed')))
48+
49+
def processAlgorithm(self, progress):
50+
layer = dataobjects.getObjectFromUri(
51+
self.getParameterValue(self.INPUT_LAYER))
52+
provider = layer.dataProvider()
53+
54+
writer = self.getOutputFromName(
55+
self.OUTPUT_LAYER).getVectorWriter(
56+
layer.fields().toList(),
57+
provider.geometryType(),
58+
layer.crs())
59+
60+
outFeat = QgsFeature()
61+
62+
features = vector.features(layer)
63+
total = 100.0 / float(len(features))
64+
current = 0
65+
66+
for inFeat in features:
67+
inGeom = inFeat.constGeometry()
68+
attrs = inFeat.attributes()
69+
70+
outGeom = None
71+
if inGeom and not inGeom.isEmpty():
72+
reversedLine = inGeom.geometry().reversed()
73+
if reversedLine is None:
74+
raise GeoAlgorithmExecutionException(
75+
self.tr('Error reversing line'))
76+
outGeom = QgsGeometry(reversedLine)
77+
78+
outFeat.setGeometry(outGeom)
79+
outFeat.setAttributes(attrs)
80+
writer.addFeature(outFeat)
81+
current += 1
82+
progress.setPercentage(int(current * total))
83+
84+
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.