2

I want to use QgsGraphAnalyzer.dijkstra in a standalone script. using version 1.8

I am following the guide at: http://www.qgis.org/en/docs/pyqgis_developer_cookbook/network_analysis.html

Here is my set-up since that is a big part of standalone

#-*- coding: utf-8 -*-
import sys
import os

#QGIS step up that needs to be done before importing from QGIS
sys.path.append("/Applications/QGIS.app/Contents/Resources/python")
from qgis.core import *
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True);
QgsApplication.initQgis()

app = QgsApplication([],True)
from qgis.gui import *
from qgis.networkanalysis import *
from qgis.utils import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

... code is the same as the tutorial

when i print out tiedpoints, they are the same in the qgis console as well as my standalone however after i call dijkstra, the qgis console gives a reasonable tree value, but my standalone only gives [-1, -1, -1, -1, -1...] for tree.

edit 1: Figured out the problem! However I need help with fixing it. In my standalond script when i do print graph.arc(0).properties()[0].toDouble() i get (nan, true)

However in the Python console, i get (an actual number, true)

edit 2: I've been working on this, still can't figure out why it's not working. However, I've narrowed down the problem. Building the vectorLayer seems to be the same on both inside and outside. This means the builder.graph() or some step in that area seems to be the problem.

Any thoughts how to fix this.

edit 3: some code: Here is where I use the director: I believe the error is in here since graph is returning (nan,true) as mentioned above

def getPath(vectorLayer, startX, startY, endX, endY):
 director = QgsLineVectorLayerDirector(vectorLayer, -1, '','','',3)
 #way to calculate edge properties
 properter = QgsDistanceArcProperter()
 director.addProperter( properter )

 #making a builder which makes the graph and setting coordinate reference system
 myCRS = QgsCoordinateReferenceSystem()
 builder = QgsGraphBuilder(myCRS)

 #converting coordinates to Qqgs Points and add it to builder
 startPoint = QgsPoint(startX, startY)
 endPoint = QgsPoint(endX, endY)
 tiedPoints = director.makeGraph(builder, [startPoint, endPoint]) 
 graph = builder.graph()
 return graph, tiedPoints

to make the vector layer, I simply use hallwayLayer = QgsVectorLayer(hallways, "hallway", "ogr") where hallways is a path to the .shp file

here is where I try to use the dij function:

#getting paths
graph, tiedPts = getPath(hallwayLayer, startPointX, startPointY, endPointX, endPointY)


#getting shortest Path on a canvas
map = QgsMapCanvas()
getShortestPath(graph, tiedPts, map)

def getShortestPath(path, indices, canvas):
 startId = path.findVertex( indices[0] )
 stopId = path.findVertex( indices[1] )
 map = QgsRubberBand(canvas, False)
 (tree,cost) = QgsGraphAnalyzer.dijkstra( path, stopId, 0)

edit 4: Seemed to narrow down the problem even more inside of qgis when i do

myCRS = QgsCoordinateReferenceSystem()
builder = QgsGraphBuilder(myCRS)
print builder.distanceArea().ellipsoidSemiMajor()

I get the same number. (I checked this with different variable names to double check this) When I do it outside i get different numbers, and they all have large e values like 2.19636051247e-314, 2.13599944534e-314

  • Can you post relevant segments of your code? – Alex May 11 '14 at 16:52
  • added segments of code – user29998 May 11 '14 at 17:20
  • 1
    FIGURED IT OUT! The line "app = QgsApplication([],True)" messes with GraphAnalyzer. so instead of having it at the top with all the other setup, I had to do graphanalyzer stuff, then app = QgsApplication([],True), then making image. – user29998 May 15 '14 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.