Skip to content

Commit

Permalink
[processing] Keep attributes from start/end points layer in
Browse files Browse the repository at this point in the history
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 31, 2017
1 parent 4925438 commit 36a38f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 9 additions & 5 deletions python/plugins/processing/algs/qgis/ShortestPathLayerToPoint.py
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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[:] = []
Expand Down
14 changes: 9 additions & 5 deletions python/plugins/processing/algs/qgis/ShortestPathPointToLayer.py
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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[:] = []
Expand Down

0 comments on commit 36a38f4

Please sign in to comment.