Skip to content

Commit a60048c

Browse files
committedDec 13, 2016
[processing] add shortest path algorithm to Processing
1 parent a8b9723 commit a60048c

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
 
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ShortestPath.py
6+
---------------------
7+
Date : November 2016
8+
Copyright : (C) 2016 by Alexander Bruy
9+
Email : alexander dot bruy 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__ = 'Alexander Bruy'
21+
__date__ = 'November 2016'
22+
__copyright__ = '(C) 2016, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.PyQt.QtGui import QIcon
31+
32+
from qgis.analysis import (QgsLineVectorLayerDirector,
33+
QgsDistanceArcProperter,
34+
QgsGraphBuilder,
35+
QgsGraphAnalyzer
36+
)
37+
38+
from processing.core.GeoAlgorithm import GeoAlgorithm
39+
from processing.core.parameters import (ParameterVector,
40+
ParameterPoint,
41+
ParameterNumber,
42+
ParameterString,
43+
ParameterTableField,
44+
ParameterSelection
45+
)
46+
from processing.core.outputs import (OutputNumber,
47+
OutputVector
48+
)
49+
from processing.tools import dataobjects
50+
51+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
52+
53+
54+
class ShortestPath(GeoAlgorithm):
55+
56+
INPUT_VECTOR = 'INPUT_VECTOR'
57+
START_POINT = 'START_POINT'
58+
END_POINT = 'END_POINT'
59+
DIRECTION_FIELD = 'DIRECTION_FIELD'
60+
VALUE_DIRECT = 'VALUE_DIRECT'
61+
VALUE_REVERSED = 'VALUE_REVERSED'
62+
VALUE_BIDIRECTIONAL = 'VALUE_BIDIRECTIONAL'
63+
DEFAULT_DIRECTION = 'DEFAULT_DIRECTION'
64+
PATH_LENGTH = 'PATH_LENGTH'
65+
OUTPUT_LAYER = 'OUTPUT_LAYER'
66+
67+
def getIcon(self):
68+
return QIcon(os.path.join(pluginPath, 'images', 'networkanalysis.png'))
69+
70+
def defineCharacteristics(self):
71+
self.DIRECTIONS = {1: self.tr('Direct direction'),
72+
2: self.tr('Inverse direction'),
73+
3: self.tr('Bidirectional')
74+
}
75+
76+
self.UNITS = {0: self.tr('Meters'),
77+
1: self.tr('Kilometers')
78+
}
79+
80+
self.name, self.i18n_name = self.trAlgorithm('Shortest path')
81+
self.group, self.i18n_group = self.trAlgorithm('Network analysis')
82+
83+
self.addParameter(ParameterVector(self.INPUT_VECTOR,
84+
self.tr('Vector layer representing road network'),
85+
[dataobjects.TYPE_VECTOR_LINE]))
86+
self.addParameter(ParameterPoint(self.START_POINT,
87+
self.tr('Start point')))
88+
self.addParameter(ParameterPoint(self.END_POINT,
89+
self.tr('End point')))
90+
91+
self.addParameter(ParameterTableField(self.DIRECTION_FIELD,
92+
self.tr('Road direction field'),
93+
self.INPUT_VECTOR))
94+
self.addParameter(ParameterString(self.VALUE_DIRECT,
95+
self.tr('Value for direct direction road'),
96+
''))
97+
self.addParameter(ParameterString(self.VALUE_REVERSED,
98+
self.tr('Value for reversed direction road'),
99+
''))
100+
self.addParameter(ParameterString(self.VALUE_BIDIRECTIONAL,
101+
self.tr('Value for bidirectional road'),
102+
''))
103+
self.addParameter(ParameterSelection(self.DEFAULT_DIRECTION,
104+
self.tr('Default road direction'),
105+
list(self.DIRECTIONS.keys())))
106+
107+
self.addOutput(OutputNumber(self.PATH_LENGTH,
108+
self.tr('Path length')))
109+
self.addOutput(OutputVector(self.OUTPUT_LAYER,
110+
self.tr('Shortest path'),
111+
datatype=[dataobjects.TYPE_VECTOR_LINE]))
112+
113+
def processAlgorithm(self, progress):
114+
layer = dataobjects.getObjectFromUri(
115+
self.getParameterValue(self.INPUT_VECTOR))
116+
fielName = self.getParameterValue(self.RASTER_BAND)
117+
columnPrefix = self.getParameterValue(self.COLUMN_PREFIX)
118+
st = self.getParameterValue(self.STATISTICS)
119+
120+
vectorLayer = dataobjects.getObjectFromUri(vectorPath)

0 commit comments

Comments
 (0)
Please sign in to comment.