2

I have the following layers in QGIS:

  • origins points
  • destination points
  • a layer of lines that is the route

I want to calculate the matrix with the distance from origin to destination using Network Analysis.

I've tried to do a python script using this, but I'm not seeing any results.

I honestly do not know anything about gis or python. But I need to make a data analysis. This is what I have done, I can not even read the layers. I'm also not sure if the cost represents the distance traveled

#Definition of inputs and outputs
#==================================
##[my scripts]=group
##origin_layer=vector
##origin_id_column_index=field origin_layer
##destination_layer=vector
##destination_id_column_index=field destination_layer
##route_layer=vector
##output=string 

#Algorithm body
#==================================


from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

from processing.core.VectorWriter import VectorWriter
import processing 
from scipy.spatial.distance import cdist
import numpy as np
from math import sqrt


origin_layer = processing.getObject(origin_layer)
origin_id_column_index = origin_layer.fieldNameIndex(origin_id_column_index)


destination_layer = processing.getObject(destination_layer)
destination_id_column_index = destination_layer.fieldNameIndex(destination_id_column_index)


route_layer = processing.getObject(route_layer)



# don't use information about road direction from layer attributes,
# all roads are treated as two-way
director = QgsLineVectorLayerDirector(route_layer, -1, '', '', '', 3)


for origin_feature in origin_layer:
    pStart = origin_feature.geometry().centroid().asPoint()
    for destination_feature in destination_layer:
        pStop = destination_feature.geometry().centroid().asPoint()
        tiedPoints = director.makeGraph(builder, [pStart, pStop])
        graph = builder.graph()
        tStart = tiedPoints[0]
        tStop = tiedPoints[1]
        idStart = graph.findVertex(tStart)
        idStop = graph.findVertex(tStop)
        (tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)
1

I could find the solution

#Definition of inputs and outputs
#==================================
##[my scripts]=group
##origin_layer=vector
##origin_id_column_index=field origin_layer
##destination_layer=vector
##destination_id_column_index=field destination_layer
##route_layer=vector
##outpath=string 

#Algorithm body
#==================================


from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

from processing.core.VectorWriter import VectorWriter
import processing 
from scipy.spatial.distance import cdist
import numpy as np
from math import sqrt


origin_layer = processing.getobject(origin_layer)

origin_id_column_index = origin_layer.fieldNameIndex(origin_id_column_index)


destination_layer = processing.getobject(destination_layer)

destination_id_column_index = destination_layer.fieldNameIndex(destination_id_column_index)

route_layer = processing.getobject(route_layer)



# don't use information about road direction from layer attributes,
# all roads are treated as two-way


file = open(outpath + 'matrizDistancia.csv','w')
file.write( 'indexGenerador, indexRecolector, distancia\n')



for origin_feature in origin_layer.getFeatures():
    pStart = origin_feature.geometry().centroid().asPoint()
    indexGenerador = origin_feature.attributes()[origin_id_column_index]
    for destination_feature in destination_layer.getFeatures():
        director = QgsLineVectorLayerDirector(route_layer, -1, '', '', '', 3)
        properter = QgsDistanceArcProperter()
        director.addProperter( properter )
        builder = QgsGraphBuilder( route_layer.crs() )
        #aca empieza a itrear
        pStop = destination_feature.geometry().centroid().asPoint()
        indexRecolector = destination_feature.attributes()[destination_id_column_index]
        tiedPoints = director.makeGraph(builder, [pStart, pStop])
        graph = builder.graph()
        tStart = tiedPoints[0]
        tStop = tiedPoints[1]
        idStart = graph.findVertex(tStart)
        idStop = graph.findVertex(tStop)
        (tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)
        file.write( '{0}, {1}, {2}\n'.format(indexGenerador, indexRecolector, cost[idStop]))
file.close()

Your Answer

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

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