QGIS Network Analysis – Using Python Library to Calculate Short Path Based on Speed and Distance

dijkstranetwork analysispyqgisqgis-plugins

I want to calculate and show short path from starting point to an end point in a qgis python plugin.
in my research i stopped by an article about QGis network analysis library; http://www.qgis.org/en/docs/pyqgis_developer_cookbook/network_analysis.html,
and i found this:
"To calculate edge properties programming pattern strategy is used. For now only QgsDistanceArcProperter strategy is available, that takes into account the length of the route. You can implement your own strategy that will use all necessary parameters. For example, RoadGraph plugin uses strategy that compute travel time using edge length and speed value from attributes."
I'm wondering if i can define my own strategy based on (speed&distance) meaning time cost using QgsDistanceArcProperter.

Can you explain to me if it is possible and how?

Best Answer

i found how to do it. i converted RgSpeedProperter class from RoadGraphPlugin cpp Source to python. the python class definition is below:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *
from qgis.utils import *
import qgis 
class SpeedProperter(QgsArcProperter):
    def __init__(self, attributeId, defaultValue, toMetricFactor = 1000 ):
        QgsArcProperter.__init__(self)
        self.AttributeId = attributeId
        self.DefaultValue = defaultValue
        self.ToMetricFactor = toMetricFactor
    def property(self, distance, Feature):
        map = Feature.attributeMap()
        it = map[ self.AttributeId ]
        #if ( it == map.end() ):
        #    return QVariant( distance / ( self.DefaultValue * self.ToMetricFactor ) )      
        val = distance / ( it.toDouble()[0] * self.ToMetricFactor )
        if ( val <= 0.0 ):
            return QVariant( distance / ( self.DefaultValue * self.ToMetricFactor ) )
        return QVariant( val )
    def requiredAttributes(self):
        l = []
        l.append( self.AttributeId );
        return l  

i set toMetricFactor default value as 1000 because i'm using the speed in km/h and the default speed is 25.

Related Question