1

With the guide of Network analysis library

I found the shortest route on the network I'm analyzing. The script I used is as follows:

from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *

vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

pStart = QgsPoint(594869.256,4279586.388z)
pStop = QgsPoint(594011.309,4279760.098)

tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

tStart = tiedPoints[0]
tStop = tiedPoints[1]

idStart = graph.findVertex(tStart)
tree = QgsGraphAnalyzer.shortestTree(graph, idStart, 0)

idStart = tree.findVertex(tStart)
idStop = tree.findVertex(tStop)

if idStop == -1:
  print("Path not found")
else:
  p = []
  while (idStart != idStop):
    l = tree.vertex(idStop).inArc()
    if len(l) == 0:
      break
    e = tree.arc(l[0])
    p.insert(0, tree.vertex(e.inVertex()).point())
    idStop = e.outVertex()

  p.insert(0, tStart)
  rb = QgsRubberBand(qgis.utils.iface.mapCanvas())
  rb.setColor(Qt.green)

  for pnt in p:
    rb.addPoint(pnt)

Unfortunately, the result I get is a path highlighted in a different color. Is there a way to save the path as a shapefile containing the length of the path as information? Or, even better, save this information in a text file?

I'm following this suggestion:

lastPoint = None
d = 0
for point in p:
    if lastPoint is not None:
         dx = point.x - lastPoint.x
         dy = point.y - lastPoint.y  
         d += sqrt(dx*dx+dy*dy)
    lastPoint = point
print "distance is "+str(d/1000.0)+"km"

but I get an error: File "path/to/scipt.py", line 52, in dx = point.x - lastPoint.x TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method'

3
  • 1
    at this line: ` p.insert(0, tStart)` you're having a complete set of points in a list p. Those are QgsPoint objects. So you have a list of QgsPoints, you can easily create shapefile out of them, or, if you wish, extract their X and Y and save to some text file. This answer might help you creating shapefile out of points. If you want to store X and Y of each point, just iterate over them in the last for loop and access them using pnt.x() and pnt.y(). Does this solve the problem for you?
    – adamczi
    Commented Jun 2, 2018 at 13:49
  • @adamczi can you explain me how to use point.x() and point.y()? Commented Jun 2, 2018 at 14:10
  • I see you already got it.
    – adamczi
    Commented Jun 3, 2018 at 8:09

1 Answer 1

1

[SOLVED]

from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from math import *

vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

pStart = QgsPoint(594869.256,4279586.388)
pStop = QgsPoint(594011.309,4279760.098)

tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

tStart = tiedPoints[0]
tStop = tiedPoints[1]

idStart = graph.findVertex(tStart)
tree = QgsGraphAnalyzer.shortestTree(graph, idStart, 0)

idStart = tree.findVertex(tStart)
idStop = tree.findVertex(tStop)

if idStop == -1:
  print("Path not found")
else:
  p = []
  while (idStart != idStop):
    l = tree.vertex(idStop).inArc()
    if len(l) == 0:
      break
    e = tree.arc(l[0])
    p.insert(0, tree.vertex(e.inVertex()).point())
    idStop = e.outVertex()

  p.insert(0, tStart)
  rb = QgsRubberBand(qgis.utils.iface.mapCanvas())
  rb.setColor(Qt.green)

  lastPoint = None
  d = 0

  for pnt in p:
    rb.addPoint(pnt)


    if lastPoint is not None:
            dx = pnt.x() - lastPoint.x()
            dy = pnt.y() - lastPoint.y()  
            d += sqrt(dx*dx+dy*dy)
    lastPoint = pnt
print "distance is "+str(d/1000.0)+"km"
1
  • That's a pity... This script doesn't work in QGIS 3.12 anymore. I'm getting name 'QgsLineVectorLayerDirector' is not defined. In fact, there is no QgsLineVector* in auto completion. Was It renamed/replaced?
    – Jecogeo
    Commented Apr 9, 2020 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.