Skip to content
Permalink
Browse files

[processing] adapt network analysis algorithms to latest API changes

  • Loading branch information
alexbruy committed Jul 17, 2017
1 parent 4a6ceff commit f3f74a9ddf1801cfbb80a00106ac35ad0ceca1d4
@@ -46,7 +46,6 @@
QgsProcessingParameterString,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterDefinition)
from qgis.analysis import (QgsVectorLayerDirector,
QgsNetworkDistanceStrategy,
@@ -97,9 +96,9 @@ def initAlgorithm(self, config=None):
self.tr('Fastest')
]

self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.START_POINTS,
self.tr('Vector layer with start points'),
[QgsProcessing.TypeVectorPoint]))
@@ -165,7 +164,7 @@ def displayName(self):
return self.tr('Service area (from layer)')

def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
network = self.parameterAsSource(parameters, self.INPUT, context)
startPoints = self.parameterAsSource(parameters, self.START_POINTS, context)
strategy = self.parameterAsEnum(parameters, self.STRATEGY, context)
travelCost = self.parameterAsDouble(parameters, self.TRAVEL_COST, context)
@@ -188,12 +187,12 @@ def processAlgorithm(self, parameters, context, feedback):

directionField = -1
if directionFieldName:
directionField = layer.fields().lookupField(directionFieldName)
directionField = network.fields().lookupField(directionFieldName)
speedField = -1
if speedFieldName:
speedField = layer.fields().lookupField(speedFieldName)
speedField = network.fields().lookupField(speedFieldName)

director = QgsVectorLayerDirector(layer,
director = QgsVectorLayerDirector(network,
directionField,
forwardValue,
backwardValue,
@@ -217,9 +216,9 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Loading start points...'))
request = QgsFeatureRequest()
request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes)
request.setDestinationCrs(layer.crs())
features = source.getFeatures(request)
total = 100.0 / source.featureCount() if source.featureCount() else 0
request.setDestinationCrs(network.sourceCrs())
features = startPoints.getFeatures(request)
total = 100.0 / startPoints.featureCount() if startPoints.featureCount() else 0

points = []
for current, f in enumerate(features):
@@ -230,17 +229,17 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(int(current * total))

feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points)
snappedPoints = director.makeGraph(builder, points, feedback)

feedback.pushInfo(self.tr('Calculating service areas...'))
graph = builder.graph()

results = {}
(sinkPoints, pointsId) = self.parameterAsSink(parameters, self.OUTPUT_POINTS, context,
fields, QgsWkbTypes.MultiPoint, layer.crs())
fields, QgsWkbTypes.MultiPoint, network.sourceCrs())

(sinkPolygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())
fields, QgsWkbTypes.Polygon, network.sourceCrs())

if sinkPoints:
results[self.OUTPUT_POINTS] = pointsId
@@ -45,7 +45,7 @@
QgsProcessingParameterNumber,
QgsProcessingParameterString,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterDefinition)
from qgis.analysis import (QgsVectorLayerDirector,
QgsNetworkDistanceStrategy,
@@ -96,9 +96,9 @@ def initAlgorithm(self, config=None):
self.tr('Fastest')
]

self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterPoint(self.START_POINT,
self.tr('Start point')))
self.addParameter(QgsProcessingParameterEnum(self.STRATEGY,
@@ -163,7 +163,7 @@ def displayName(self):
return self.tr('Service area (from point)')

def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
network = self.parameterAsSource(parameters, self.INPUT, context)
startPoint = self.parameterAsPoint(parameters, self.START_POINT, context)
strategy = self.parameterAsEnum(parameters, self.STRATEGY, context)
travelCost = self.parameterAsDouble(parameters, self.TRAVEL_COST, context)
@@ -179,12 +179,12 @@ def processAlgorithm(self, parameters, context, feedback):

directionField = -1
if directionFieldName:
directionField = layer.fields().lookupField(directionFieldName)
directionField = network.fields().lookupField(directionFieldName)
speedField = -1
if speedFieldName:
speedField = layer.fields().lookupField(speedFieldName)
speedField = network.fields().lookupField(speedFieldName)

director = QgsVectorLayerDirector(layer,
director = QgsVectorLayerDirector(network,
directionField,
forwardValue,
backwardValue,
@@ -205,7 +205,7 @@ def processAlgorithm(self, parameters, context, feedback):
True,
tolerance)
feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, [startPoint])
snappedPoints = director.makeGraph(builder, [startPoint], feedback)

feedback.pushInfo(self.tr('Calculating service area...'))
graph = builder.graph()
@@ -238,10 +238,10 @@ def processAlgorithm(self, parameters, context, feedback):
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

(sinkPoints, pointsId) = self.parameterAsSink(parameters, self.OUTPUT_POINTS, context,
fields, QgsWkbTypes.MultiPoint, layer.crs())
fields, QgsWkbTypes.MultiPoint, network.sourceCrs())

(sinkPolygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())
fields, QgsWkbTypes.Polygon, network.sourceCrs())
results = {}
if sinkPoints:
feat.setGeometry(geomUpper)
@@ -47,7 +47,6 @@
QgsProcessingParameterString,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterDefinition)
from qgis.analysis import (QgsVectorLayerDirector,
QgsNetworkDistanceStrategy,
@@ -97,9 +96,9 @@ def initAlgorithm(self, config=None):
self.tr('Fastest')
]

self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.START_POINTS,
self.tr('Vector layer with start points'),
[QgsProcessing.TypeVectorPoint]))
@@ -158,7 +157,7 @@ def displayName(self):
return self.tr('Shortest path (layer to point)')

def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
network = self.parameterAsSource(parameters, self.INPUT, context)
startPoints = self.parameterAsSource(parameters, self.START_POINTS, context)
endPoint = self.parameterAsPoint(parameters, self.END_POINT, context)
strategy = self.parameterAsEnum(parameters, self.STRATEGY, context)
@@ -181,16 +180,16 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setFields(fields)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.LineString, layer.crs())
fields, QgsWkbTypes.LineString, network.sourceCrs())

directionField = -1
if directionFieldName:
directionField = layer.fields().lookupField(directionFieldName)
directionField = network.fields().lookupField(directionFieldName)
speedField = -1
if speedFieldName:
speedField = layer.fields().lookupField(speedFieldName)
speedField = network.fields().lookupField(speedFieldName)

director = QgsVectorLayerDirector(layer,
director = QgsVectorLayerDirector(network,
directionField,
forwardValue,
backwardValue,
@@ -215,9 +214,9 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Loading start points...'))
request = QgsFeatureRequest()
request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes)
request.setDestinationCrs(layer.crs())
features = source.getFeatures(request)
total = 100.0 / source.featureCount() if source.featureCount() else 0
request.setDestinationCrs(network.sourceCrs())
features = startPoints.getFeatures(request)
total = 100.0 / startPoints.featureCount() if startPoints.featureCount() else 0

points = [endPoint]
for current, f in enumerate(features):
@@ -228,15 +227,16 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(int(current * total))

feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points)
snappedPoints = director.makeGraph(builder, points, feedback)

feedback.pushInfo(self.tr('Calculating shortest paths...'))
graph = builder.graph()

idxEnd = graph.findVertex(snappedPoints[0])
route = []

total = 100.0 / source.featureCount() if source.featureCount() else 1
nPoints = len(snappedPoints)
total = 100.0 / nPoints if nPoints else 1
for i in range(1, count + 1):
if feedback.isCanceled():
break
@@ -47,7 +47,6 @@
QgsProcessingParameterString,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterDefinition)
from qgis.analysis import (QgsVectorLayerDirector,
QgsNetworkDistanceStrategy,
@@ -97,9 +96,9 @@ def initAlgorithm(self, config=None):
self.tr('Fastest')
]

self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Vector layer representing network'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterPoint(self.START_POINT,
self.tr('Start point')))
self.addParameter(QgsProcessingParameterFeatureSource(self.END_POINTS,
@@ -158,7 +157,7 @@ def displayName(self):
return self.tr('Shortest path (point to layer)')

def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
network = self.parameterAsSource(parameters, self.INPUT, context)
startPoint = self.parameterAsPoint(parameters, self.START_POINT, context)
endPoints = self.parameterAsSource(parameters, self.END_POINTS, context)
strategy = self.parameterAsEnum(parameters, self.STRATEGY, context)
@@ -181,16 +180,16 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setFields(fields)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.LineString, layer.crs())
fields, QgsWkbTypes.LineString, network.sourceCrs())

directionField = -1
if directionFieldName:
directionField = layer.fields().lookupField(directionFieldName)
directionField = network.fields().lookupField(directionFieldName)
speedField = -1
if speedFieldName:
speedField = layer.fields().lookupField(speedFieldName)
speedField = network.fields().lookupField(speedFieldName)

director = QgsVectorLayerDirector(layer,
director = QgsVectorLayerDirector(network,
directionField,
forwardValue,
backwardValue,
@@ -215,9 +214,9 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Loading end points...'))
request = QgsFeatureRequest()
request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes)
request.setDestinationCrs(layer.crs())
features = source.getFeatures(request)
total = 100.0 / source.featureCount() if source.featureCount() else 0
request.setDestinationCrs(network.sourceCrs())
features = endPoints.getFeatures(request)
total = 100.0 / endPoints.featureCount() if endPoints.featureCount() else 0

points = [startPoint]
for current, f in enumerate(features):
@@ -228,7 +227,7 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(int(current * total))

feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points)
snappedPoints = director.makeGraph(builder, points, feedback)

feedback.pushInfo(self.tr('Calculating shortest paths...'))
graph = builder.graph()
@@ -237,7 +236,8 @@ def processAlgorithm(self, parameters, context, feedback):
tree, cost = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)
route = []

total = 100.0 / source.featureCount() if source.featureCount() else 1
nPoints = len(snappedPoints)
total = 100.0 / nPoints if nPoints else 1
for i in range(1, count + 1):
if feedback.isCanceled():
break

0 comments on commit f3f74a9

Please sign in to comment.
You can’t perform that action at this time.