Skip to content
Permalink
Browse files

[processing] Keep attributes from start/end points layer in

output layer for shortest path algorithms

Otherwise the paths are nearly impossible to link back to
the source layer for further analysis
  • Loading branch information
nyalldawson committed Oct 28, 2017
1 parent 4925438 commit 36a38f4e2d5fd8a61707622cb072b44ad0e70bc5
@@ -171,7 +171,7 @@ def processAlgorithm(self, parameters, context, feedback):
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)

fields = QgsFields()
fields = startPoints.fields()
fields.append(QgsField('start', QVariant.String, '', 254, 0))
fields.append(QgsField('end', QVariant.String, '', 254, 0))
fields.append(QgsField('cost', QVariant.Double, '', 20, 7))
@@ -213,18 +213,22 @@ 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 = [endPoint]
source_attributes = {}
i = 1
for current, f in enumerate(features):
if feedback.isCanceled():
break

points.append(f.geometry().asPoint())
source_attributes[i] = f.attributes()

feedback.setProgress(int(current * total))
i += 1

feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points, feedback)
@@ -262,9 +266,9 @@ def processAlgorithm(self, parameters, context, feedback):

geom = QgsGeometry.fromPolylineXY(route)
feat.setGeometry(geom)
feat['start'] = points[i].toString()
feat['end'] = endPoint.toString()
feat['cost'] = cost / multiplier
attrs = source_attributes[i]
attrs.extend([points[i].toString(), endPoint.toString(), cost / multiplier])
feat.setAttributes(attrs)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

route[:] = []
@@ -171,7 +171,7 @@ def processAlgorithm(self, parameters, context, feedback):
defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context)
tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context)

fields = QgsFields()
fields = endPoints.fields()
fields.append(QgsField('start', QVariant.String, '', 254, 0))
fields.append(QgsField('end', QVariant.String, '', 254, 0))
fields.append(QgsField('cost', QVariant.Double, '', 20, 7))
@@ -213,18 +213,22 @@ def processAlgorithm(self, parameters, context, feedback):

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

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

points.append(f.geometry().asPoint())
source_attributes[i] = f.attributes()

feedback.setProgress(int(current * total))
i += 1

feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points, feedback)
@@ -261,10 +265,10 @@ def processAlgorithm(self, parameters, context, feedback):
route.reverse()

geom = QgsGeometry.fromPolylineXY(route)
attrs = source_attributes[i]
attrs.extend([startPoint.toString(), points[i].toString(), cost / multiplier])
feat.setAttributes(attrs)
feat.setGeometry(geom)
feat['start'] = startPoint.toString()
feat['end'] = points[i].toString()
feat['cost'] = cost / multiplier
sink.addFeature(feat, QgsFeatureSink.FastInsert)

route[:] = []

0 comments on commit 36a38f4

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