36
36
QgsFeature ,
37
37
QgsFeatureSink ,
38
38
QgsGeometry ,
39
- QgsPointXY ,
40
39
QgsFields ,
41
40
QgsField ,
42
- QgsProcessingParameterDefinition ,
43
- QgsProcessingUtils )
41
+ QgsProcessing ,
42
+ QgsProcessingException ,
43
+ QgsProcessingOutputNumber ,
44
+ QgsProcessingParameterEnum ,
45
+ QgsProcessingParameterPoint ,
46
+ QgsProcessingParameterField ,
47
+ QgsProcessingParameterNumber ,
48
+ QgsProcessingParameterString ,
49
+ QgsProcessingParameterFeatureSink ,
50
+ QgsProcessingParameterVectorLayer ,
51
+ QgsProcessingParameterDefinition )
44
52
from qgis .analysis import (QgsVectorLayerDirector ,
45
53
QgsNetworkDistanceStrategy ,
46
54
QgsNetworkSpeedStrategy ,
50
58
from qgis .utils import iface
51
59
52
60
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
53
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
54
- from processing .core .parameters import (ParameterVector ,
55
- ParameterPoint ,
56
- ParameterNumber ,
57
- ParameterString ,
58
- ParameterTableField ,
59
- ParameterSelection
60
- )
61
- from processing .core .outputs import (OutputNumber ,
62
- OutputVector
63
- )
64
- from processing .tools import dataobjects
65
61
66
62
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
67
63
68
64
69
65
class ShortestPathPointToPoint (QgisAlgorithm ):
70
66
71
- INPUT_VECTOR = 'INPUT_VECTOR '
67
+ INPUT = 'INPUT '
72
68
START_POINT = 'START_POINT'
73
69
END_POINT = 'END_POINT'
74
70
STRATEGY = 'STRATEGY'
@@ -81,7 +77,7 @@ class ShortestPathPointToPoint(QgisAlgorithm):
81
77
DEFAULT_SPEED = 'DEFAULT_SPEED'
82
78
TOLERANCE = 'TOLERANCE'
83
79
TRAVEL_COST = 'TRAVEL_COST'
84
- OUTPUT_LAYER = 'OUTPUT_LAYER '
80
+ OUTPUT = 'OUTPUT '
85
81
86
82
def icon (self ):
87
83
return QIcon (os .path .join (pluginPath , 'images' , 'networkanalysis.svg' ))
@@ -102,59 +98,60 @@ def initAlgorithm(self, config=None):
102
98
self .tr ('Fastest' )
103
99
]
104
100
105
- self .addParameter (ParameterVector (self .INPUT_VECTOR ,
106
- self .tr ('Vector layer representing network' ),
107
- [ dataobjects . TYPE_VECTOR_LINE ]))
108
- self .addParameter (ParameterPoint (self .START_POINT ,
109
- self .tr ('Start point' )))
110
- self .addParameter (ParameterPoint (self .END_POINT ,
111
- self .tr ('End point' )))
112
- self .addParameter (ParameterSelection (self .STRATEGY ,
113
- self .tr ('Path type to calculate' ),
114
- self .STRATEGIES ,
115
- default = 0 ))
101
+ self .addParameter (QgsProcessingParameterVectorLayer (self .INPUT ,
102
+ self .tr ('Vector layer representing network' ),
103
+ [ QgsProcessing . TypeVectorLine ]))
104
+ self .addParameter (QgsProcessingParameterPoint (self .START_POINT ,
105
+ self .tr ('Start point' )))
106
+ self .addParameter (QgsProcessingParameterPoint (self .END_POINT ,
107
+ self .tr ('End point' )))
108
+ self .addParameter (QgsProcessingParameterEnum (self .STRATEGY ,
109
+ self .tr ('Path type to calculate' ),
110
+ self .STRATEGIES ,
111
+ defaultValue = 0 ))
116
112
117
113
params = []
118
- params .append (ParameterTableField (self .DIRECTION_FIELD ,
119
- self .tr ('Direction field' ),
120
- self .INPUT_VECTOR ,
121
- optional = True ))
122
- params .append (ParameterString (self .VALUE_FORWARD ,
123
- self .tr ('Value for forward direction' ),
124
- '' ,
125
- optional = True ))
126
- params .append (ParameterString (self .VALUE_BACKWARD ,
127
- self .tr ('Value for backward direction' ),
128
- '' ,
129
- optional = True ))
130
- params .append (ParameterString (self .VALUE_BOTH ,
131
- self .tr ('Value for both directions' ),
132
- '' ,
133
- optional = True ))
134
- params .append (ParameterSelection (self .DEFAULT_DIRECTION ,
135
- self .tr ('Default direction' ),
136
- list (self .DIRECTIONS .keys ()),
137
- default = 2 ))
138
- params .append (ParameterTableField (self .SPEED_FIELD ,
139
- self .tr ('Speed field' ),
140
- self .INPUT_VECTOR ,
141
- optional = True ))
142
- params .append (ParameterNumber (self .DEFAULT_SPEED ,
143
- self .tr ('Default speed (km/h)' ),
144
- 0.0 , 99999999.999999 , 5.0 ))
145
- params .append (ParameterNumber (self .TOLERANCE ,
146
- self .tr ('Topology tolerance' ),
147
- 0.0 , 99999999.999999 , 0.0 ))
114
+ params .append (QgsProcessingParameterField (self .DIRECTION_FIELD ,
115
+ self .tr ('Direction field' ),
116
+ None ,
117
+ self .INPUT ,
118
+ optional = True ))
119
+ params .append (QgsProcessingParameterString (self .VALUE_FORWARD ,
120
+ self .tr ('Value for forward direction' ),
121
+ optional = True ))
122
+ params .append (QgsProcessingParameterString (self .VALUE_BACKWARD ,
123
+ self .tr ('Value for backward direction' ),
124
+ optional = True ))
125
+ params .append (QgsProcessingParameterString (self .VALUE_BOTH ,
126
+ self .tr ('Value for both directions' ),
127
+ optional = True ))
128
+ params .append (QgsProcessingParameterEnum (self .DEFAULT_DIRECTION ,
129
+ self .tr ('Default direction' ),
130
+ list (self .DIRECTIONS .keys ()),
131
+ defaultValue = 2 ))
132
+ params .append (QgsProcessingParameterField (self .SPEED_FIELD ,
133
+ self .tr ('Speed field' ),
134
+ None ,
135
+ self .INPUT ,
136
+ optional = True ))
137
+ params .append (QgsProcessingParameterNumber (self .DEFAULT_SPEED ,
138
+ self .tr ('Default speed (km/h)' ),
139
+ QgsProcessingParameterNumber .Double ,
140
+ 5.0 , False , 0 , 99999999.99 ))
141
+ params .append (QgsProcessingParameterNumber (self .TOLERANCE ,
142
+ self .tr ('Topology tolerance' ),
143
+ QgsProcessingParameterNumber .Double ,
144
+ 0.0 , False , 0 , 99999999.99 ))
148
145
149
146
for p in params :
150
147
p .setFlags (p .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
151
148
self .addParameter (p )
152
149
153
- self .addOutput (OutputNumber (self .TRAVEL_COST ,
154
- self .tr ('Travel cost' )))
155
- self .addOutput ( OutputVector (self .OUTPUT_LAYER ,
156
- self .tr ('Shortest path' ),
157
- datatype = [ dataobjects . TYPE_VECTOR_LINE ] ))
150
+ self .addOutput (QgsProcessingOutputNumber (self .TRAVEL_COST ,
151
+ self .tr ('Travel cost' )))
152
+ self .addParameter ( QgsProcessingParameterFeatureSink (self .OUTPUT ,
153
+ self .tr ('Shortest path' ),
154
+ QgsProcessing . TypeVectorLine ))
158
155
159
156
def name (self ):
160
157
return 'shortestpathpointtopoint'
@@ -163,40 +160,33 @@ def displayName(self):
163
160
return self .tr ('Shortest path (point to point)' )
164
161
165
162
def processAlgorithm (self , parameters , context , feedback ):
166
- layer = QgsProcessingUtils .mapLayerFromString (self .getParameterValue (self .INPUT_VECTOR ), context )
167
- startPoint = self .getParameterValue (self .START_POINT )
168
- endPoint = self .getParameterValue (self .END_POINT )
169
- strategy = self .getParameterValue (self .STRATEGY )
170
-
171
- directionFieldName = self .getParameterValue (self .DIRECTION_FIELD )
172
- forwardValue = self .getParameterValue (self .VALUE_FORWARD )
173
- backwardValue = self .getParameterValue (self .VALUE_BACKWARD )
174
- bothValue = self .getParameterValue (self .VALUE_BOTH )
175
- defaultDirection = self .getParameterValue (self .DEFAULT_DIRECTION )
176
- bothValue = self .getParameterValue (self .VALUE_BOTH )
177
- defaultDirection = self .getParameterValue (self .DEFAULT_DIRECTION )
178
- speedFieldName = self .getParameterValue (self .SPEED_FIELD )
179
- defaultSpeed = self .getParameterValue (self .DEFAULT_SPEED )
180
- tolerance = self .getParameterValue (self .TOLERANCE )
163
+ layer = self .parameterAsVectorLayer (parameters , self .INPUT , context )
164
+ startPoint = self .parameterAsPoint (parameters , self .START_POINT , context )
165
+ endPoint = self .parameterAsPoint (parameters , self .END_POINT , context )
166
+ strategy = self .parameterAsEnum (parameters , self .STRATEGY , context )
167
+
168
+ directionFieldName = self .parameterAsString (parameters , self .DIRECTION_FIELD , context )
169
+ forwardValue = self .parameterAsString (parameters , self .VALUE_FORWARD , context )
170
+ backwardValue = self .parameterAsString (parameters , self .VALUE_BACKWARD , context )
171
+ bothValue = self .parameterAsString (parameters , self .VALUE_BOTH , context )
172
+ defaultDirection = self .parameterAsEnum (parameters , self .DEFAULT_DIRECTION , context )
173
+ speedFieldName = self .parameterAsString (parameters , self .SPEED_FIELD , context )
174
+ defaultSpeed = self .parameterAsDouble (parameters , self .DEFAULT_SPEED , context )
175
+ tolerance = self .parameterAsDouble (parameters , self .TOLERANCE , context )
181
176
182
177
fields = QgsFields ()
183
178
fields .append (QgsField ('start' , QVariant .String , '' , 254 , 0 ))
184
179
fields .append (QgsField ('end' , QVariant .String , '' , 254 , 0 ))
185
180
fields .append (QgsField ('cost' , QVariant .Double , '' , 20 , 7 ))
186
181
187
- writer = self .getOutputFromName (
188
- self .OUTPUT_LAYER ).getVectorWriter (fields , QgsWkbTypes .LineString , layer .crs (), context )
189
-
190
- tmp = startPoint .split (',' )
191
- startPoint = QgsPointXY (float (tmp [0 ]), float (tmp [1 ]))
192
- tmp = endPoint .split (',' )
193
- endPoint = QgsPointXY (float (tmp [0 ]), float (tmp [1 ]))
182
+ (sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
183
+ fields , QgsWkbTypes .LineString , layer .crs ())
194
184
195
185
directionField = - 1
196
- if directionFieldName is not None :
186
+ if directionField :
197
187
directionField = layer .fields ().lookupField (directionFieldName )
198
188
speedField = - 1
199
- if speedFieldName is not None :
189
+ if speedFieldName :
200
190
speedField = layer .fields ().lookupField (speedFieldName )
201
191
202
192
director = QgsVectorLayerDirector (layer ,
@@ -230,7 +220,7 @@ def processAlgorithm(self, parameters, context, feedback):
230
220
231
221
tree , cost = QgsGraphAnalyzer .dijkstra (graph , idxStart , 0 )
232
222
if tree [idxEnd ] == - 1 :
233
- raise GeoAlgorithmExecutionException (
223
+ raise QgsProcessingException (
234
224
self .tr ('There is no route from start point to end point.' ))
235
225
236
226
route = []
@@ -244,8 +234,6 @@ def processAlgorithm(self, parameters, context, feedback):
244
234
route .append (snappedPoints [0 ])
245
235
route .reverse ()
246
236
247
- self .setOutputValue (self .TRAVEL_COST , cost / multiplier )
248
-
249
237
feedback .pushInfo (self .tr ('Writing results...' ))
250
238
geom = QgsGeometry .fromPolyline (route )
251
239
feat = QgsFeature ()
@@ -254,5 +242,9 @@ def processAlgorithm(self, parameters, context, feedback):
254
242
feat ['end' ] = endPoint .toString ()
255
243
feat ['cost' ] = cost / multiplier
256
244
feat .setGeometry (geom )
257
- writer .addFeature (feat , QgsFeatureSink .FastInsert )
258
- del writer
245
+ sink .addFeature (feat , QgsFeatureSink .FastInsert )
246
+
247
+ results = {}
248
+ results [self .TRAVEL_COST ] = cost / multiplier
249
+ results [self .OUTPUT ] = dest_id
250
+ return results
0 commit comments