Skip to content
Permalink
Browse files

[processing] Misc fixes for network analysis tools

Also maintain attributes in service area from layer alg, add test

Fixes #17325
  • Loading branch information
nyalldawson committed Oct 30, 2017
1 parent 8d32bf7 commit 8f22f5eb3b7ad277b6ef2061ef91d1085c133d4a
@@ -38,6 +38,7 @@
QgsFeatureRequest,
QgsGeometry,
QgsFields,
QgsPointXY,
QgsField,
QgsProcessing,
QgsProcessingParameterEnum,
@@ -167,12 +168,12 @@ def processAlgorithm(self, parameters, context, feedback):
forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context)
backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context)
bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context)
defaultDirection = self.DIRECTIONS[self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)]
defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)
speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context)
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)

fields = QgsFields()
fields = startPoints.fields()
fields.append(QgsField('type', QVariant.String, '', 254, 0))
fields.append(QgsField('start', QVariant.String, '', 254, 0))

@@ -209,17 +210,25 @@ def processAlgorithm(self, parameters, context, feedback):

feedback.pushInfo(self.tr('Loading start points...'))
request = QgsFeatureRequest()
request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes)
request.setDestinationCrs(network.sourceCrs())
features = startPoints.getFeatures(request)
total = 100.0 / startPoints.featureCount() if startPoints.featureCount() else 0

points = []
source_attributes = {}
i = 0
for current, f in enumerate(features):
if feedback.isCanceled():
break

points.append(f.geometry().asPoint())
if not f.hasGeometry():
continue

for p in f.geometry().vertices():
points.append(QgsPointXY(p))
source_attributes[i] = f.attributes()
i += 1

feedback.setProgress(int(current * total))

feedback.pushInfo(self.tr('Building graph...'))
@@ -245,25 +254,27 @@ def processAlgorithm(self, parameters, context, feedback):
tree, cost = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)
for j, v in enumerate(cost):
if v > travelCost and tree[j] != -1:
vertexId = graph.edge(tree[j]).toVertex()
vertexId = graph.edge(tree[j]).fromVertex()
if cost[vertexId] <= travelCost:
vertices.append(j)

for j in vertices:
upperBoundary.append(graph.vertex(graph.edge(tree[j]).fromVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[j]).toVertex()).point())
upperBoundary.append(graph.vertex(graph.edge(tree[j]).toVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[j]).fromVertex()).point())

geomUpper = QgsGeometry.fromMultiPointXY(upperBoundary)
geomLower = QgsGeometry.fromMultiPointXY(lowerBoundary)

feat.setGeometry(geomUpper)
feat['type'] = 'upper'
feat['start'] = origPoint

attrs = source_attributes[i]
attrs.extend(['upper', origPoint])
feat.setAttributes(attrs)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

feat.setGeometry(geomLower)
feat['type'] = 'lower'
feat['start'] = origPoint
attrs[-2] = 'lower'
feat.setAttributes(attrs)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

vertices[:] = []
@@ -165,7 +165,7 @@ def processAlgorithm(self, parameters, context, feedback):
forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context)
backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context)
bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context)
defaultDirection = self.DIRECTIONS[self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)]
defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)
speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context)
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
@@ -208,15 +208,15 @@ def processAlgorithm(self, parameters, context, feedback):
vertices = []
for i, v in enumerate(cost):
if v > travelCost and tree[i] != -1:
vertexId = graph.edge(tree[i]).toVertex()
vertexId = graph.edge(tree[i]).fromVertex()
if cost[vertexId] <= travelCost:
vertices.append(i)

upperBoundary = []
lowerBoundary = []
for i in vertices:
upperBoundary.append(graph.vertex(graph.edge(tree[i]).fromVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[i]).toVertex()).point())
upperBoundary.append(graph.vertex(graph.edge(tree[i]).toVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[i]).fromVertex()).point())

feedback.pushInfo(self.tr('Writing results...'))

@@ -165,7 +165,7 @@ def processAlgorithm(self, parameters, context, feedback):
forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context)
backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context)
bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context)
defaultDirection = self.DIRECTIONS[self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)]
defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)
speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context)
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
@@ -240,7 +240,6 @@ def processAlgorithm(self, parameters, context, feedback):
graph = builder.graph()

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

nPoints = len(snappedPoints)
total = 100.0 / nPoints if nPoints else 1
@@ -263,14 +262,13 @@ def processAlgorithm(self, parameters, context, feedback):
sink.addFeature(feat, QgsFeatureSink.FastInsert)
continue

cost = 0.0
route = [graph.vertex(idxEnd).point()]
cost = costs[idxEnd]
current = idxEnd
while current != idxStart:
cost += costs[current]
current = graph.edge(tree[current]).fromVertex()
route.append(graph.vertex(current).point())

route.append(snappedPoints[i])
route.reverse()

geom = QgsGeometry.fromPolylineXY(route)
@@ -280,8 +278,6 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setAttributes(attrs)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

route[:] = []

feedback.setProgress(int(i * total))

return {self.OUTPUT: dest_id}
@@ -166,7 +166,7 @@ def processAlgorithm(self, parameters, context, feedback):
forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context)
backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context)
bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context)
defaultDirection = self.DIRECTIONS[self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)]
defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)
speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context)
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
@@ -262,14 +262,13 @@ def processAlgorithm(self, parameters, context, feedback):
sink.addFeature(feat, QgsFeatureSink.FastInsert)
continue

cost = 0.0
route = [graph.vertex(idxEnd).point()]
cost = costs[idxEnd]
current = idxEnd
while current != idxStart:
cost += costs[current]
current = graph.edge(tree[current]).fromVertex()
route.append(graph.vertex(current).point())

route.append(snappedPoints[0])
route.reverse()

geom = QgsGeometry.fromPolylineXY(route)
@@ -279,8 +278,6 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setGeometry(geom)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

route[:] = []

feedback.setProgress(int(i * total))

return {self.OUTPUT: dest_id}
@@ -168,7 +168,7 @@ def processAlgorithm(self, parameters, context, feedback):
forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context)
backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context)
bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context)
defaultDirection = self.DIRECTIONS[self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)]
defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context)
speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context)
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)
@@ -222,15 +222,13 @@ def processAlgorithm(self, parameters, context, feedback):
raise QgsProcessingException(
self.tr('There is no route from start point to end point.'))

route = []
cost = 0.0
route = [graph.vertex(idxEnd).point()]
cost = costs[idxEnd]
current = idxEnd
while current != idxStart:
cost += costs[current]
current = graph.edge(tree[current]).fromVertex()
route.append(graph.vertex(current).point())

route.append(snappedPoints[0])
route.reverse()

feedback.pushInfo(self.tr('Writing results...'))
@@ -13,14 +13,14 @@

<gml:featureMember>
<ogr:service_area fid="service_area.0">
<ogr:geometryProperty><gml:MultiPoint srsName="EPSG:32733"><gml:pointMember><gml:Point><gml:coordinates>1001843.08473263,6221828.62873543</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001891.40736133,6221430.94581986</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001940.09434754,6221707.78585671</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001962.5674552,6222024.23591493</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002086.66939634,6222010.7240816</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002103.66905047,6221324.68154126</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002129.95849754,6222135.73878474</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002299.19357268,6222416.40706398</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002416.57059498,6221277.56159465</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002622.66996036,6221162.46807726</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002667.16458207,6221195.87188063</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002761.33635685,6222354.19324527</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002897.32222975,6222395.42838888</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002897.87933631,6221326.06027793</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003028.1814962,6221611.96830289</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003037.55521283,6222118.73863535</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003040.80096358,6222322.18412711</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint></ogr:geometryProperty>
<ogr:geometryProperty><gml:MultiPoint srsName="EPSG:32733"><gml:pointMember><gml:Point><gml:coordinates>1001891.40736133,6221430.94581986</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003040.80096358,6222322.18412711</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002622.66996036,6221162.46807726</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001843.08473263,6221828.62873543</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002086.66939634,6222010.7240816</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001940.09434754,6221707.78585671</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001962.5674552,6222024.23591493</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002299.19357268,6222416.40706398</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002103.66905047,6221324.68154126</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002129.95849754,6222135.73878474</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002416.57059498,6221277.56159465</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002897.32222975,6222395.42838888</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002761.33635685,6222354.19324527</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002897.87933631,6221326.06027793</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003037.55521283,6222118.73863535</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1003028.1814962,6221611.96830289</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002667.16458207,6221195.87188063</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint></ogr:geometryProperty>
<ogr:type>upper</ogr:type>
<ogr:start>1002465.00896, 6221875.43249</ogr:start>
</ogr:service_area>
</gml:featureMember>
<gml:featureMember>
<ogr:service_area fid="service_area.1">
<ogr:geometryProperty><gml:MultiPoint srsName="EPSG:32733"><gml:pointMember><gml:Point><gml:coordinates>1002015.92985521,6221953.13030737</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1001930.78353526,6221467.36521594</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002045.66496862,6221837.84991727</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002015.92985521,6221953.13030737</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002125.77638469,6222042.5574598</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002275.1898604,6221455.19588041</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002161.08326842,6222058.23120113</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002382.21032908,6222317.66803566</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002275.1898604,6221455.19588041</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002310.58466849,6221563.42200322</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002845.50542017,6221400.90692838</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002724.17727046,6222326.95840711</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002991.11072264,6222281.72914386</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002845.50542017,6221400.90692838</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002657.33259138,6222018.20620536</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002945.17950103,6222241.31856099</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002991.11072264,6222281.72914386</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint></ogr:geometryProperty>
<ogr:geometryProperty><gml:MultiPoint srsName="EPSG:32733"><gml:pointMember><gml:Point><gml:coordinates>1001930.78353526,6221467.36521594</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002991.11072264,6222281.72914386</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002310.58466849,6221563.42200322</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002015.92985521,6221953.13030737</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002125.77638469,6222042.5574598</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002045.66496862,6221837.84991727</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002015.92985521,6221953.13030737</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002382.21032908,6222317.66803566</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002275.1898604,6221455.19588041</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002161.08326842,6222058.23120113</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002275.1898604,6221455.19588041</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002991.11072264,6222281.72914386</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002724.17727046,6222326.95840711</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002845.50542017,6221400.90692838</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002945.17950103,6222241.31856099</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002657.33259138,6222018.20620536</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>1002845.50542017,6221400.90692838</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint></ogr:geometryProperty>
<ogr:type>lower</ogr:type>
<ogr:start>1002465.00896, 6221875.43249</ogr:start>
</ogr:service_area>
Binary file not shown.

0 comments on commit 8f22f5e

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