Important
La traduction est le fruit d’un effort communautaire auquel vous pouvez vous joindre. Cette page est actuellement traduite à 71.43%.
19. Bibliothèque d’analyse de réseau
Indication
Les extraits de code sur cette page nécessitent les importations suivantes si vous êtes en dehors de la console pyqgis :
from qgis.core import (
QgsVectorLayer,
QgsPointXY,
)
La bibliothèque d’analyse de réseau peut être utilisée pour :
créer un graphe mathématique à partir des données géographiques (couches vecteurs de polylignes)
mettre en œuvre les méthodes de base de la théorie des graphes (actuellement, seul l’algorithme de Dijkstra est utilisé)
Voici un résumé d’un cas d’utilisation typique:
créer un graphe depuis les données géographiques (en utilisant une couche vecteur de polylignes)
lancer une analyse de graphe
utiliser les résultats d’analyse (pour les visualiser par exemple)
19.1. Construire un graphe
La première chose à faire est de préparer les données d’entrée, c’est à dire de convertir une couche vecteur en graphe. Les actions suivantes utiliseront ce graphe et non la couche.
Comme source de données, on peut utiliser n’importe quelle couche vecteur de polylignes. Les nœuds des polylignes deviendront les sommets du graphe et les segments des polylignes seront les arcs du graphes. Si plusieurs nœuds ont les mêmes coordonnées alors ils composent le même sommet de graphe. Ainsi, deux lignes qui ont en commun un même nœud sont connectées ensemble.
Pendant la création d’un graphe, il est possible de « forcer » (« lier ») l’ajout d’un ou de plusieurs points additionnels à la couche vecteur d’entrée. Pour chaque point additionnel, un lien sera créé: le sommet du graphe le plus proche ou l’arc de graphe le plus proche. Dans le cas final, l’arc sera séparé en deux et un nouveau sommet sera ajouté.
Les attributs de la couche vecteur et la longueur d’un segment peuvent être utilisés comme propriétés du segment.
La conversion d’une couche vectorielle vers le graphe se fait en utilisant le modèle de programmation Builder. Un graphe est construit en utilisant ce qu’on appelle un Director. Il n’y a qu’un seul Director pour le moment : QgsVectorLayerDirector. Le Director définit les paramètres de base qui seront utilisés pour construire un graphique à partir d’une couche de vecteurs linéaires, utilisée par le builder pour créer le graphique. Actuellement, comme dans le cas du director, un seul constructeur existe : QgsGraphBuilder, qui crée des objets QgsGraph. Vous pouvez vouloir implémenter vos propres constructeurs qui construiront un graphe compatible avec des bibliothèques telles que BGL ou NetworkX.
To calculate edge properties the programming pattern
strategy is used.
For now only QgsNetworkDistanceStrategy
strategy (that takes into account the length of the route)
and QgsNetworkSpeedStrategy
(that also considers the speed) are available.
You can implement your own strategy that will use all necessary parameters.
Il est temps de plonger dans le processus.
First of all, to use this library we should import the analysis module:
from qgis.analysis import *
Then some examples for creating a director:
1# Don't use information about road direction from layer attributes, 2# all roads are treated as two-way 3director = QgsVectorLayerDirector( 4 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth 5)
1# Use field with index 5 as source of information about road direction. 2# one-way roads with direct direction have attribute value "yes", 3# one-way roads with reverse direction have the value "1", and accordingly 4# bidirectional roads have "no". By default roads are treated as two-way. 5# This scheme can be used with OpenStreetMap data 6director = QgsVectorLayerDirector( 7 vectorLayer, 5, "yes", "1", "no", QgsVectorLayerDirector.DirectionBoth 8)
To construct a director, we should pass a vector layer that will be used as the source for the graph structure and information about allowed movement on each road segment (one-way or bidirectional movement, direct or reverse direction). The call looks like this (find more details on the parameters at
qgis.analysis.QgsVectorLayerDirector):1director = QgsVectorLayerDirector( 2 vectorLayer, 3 directionFieldId, 4 directDirectionValue, 5 reverseDirectionValue, 6 bothDirectionValue, 7 defaultDirection, 8)
Il est ensuite impératif de créer une stratégie de calcul des propriétés des arcs:
1# The index of the field that contains information about the edge speed 2attributeId = 1 3# Default speed value 4defaultValue = 50 5# Conversion from speed to metric units ('1' means no conversion) 6toMetricFactor = 1 7strategy = QgsNetworkSpeedStrategy(attributeId, defaultValue, toMetricFactor)
Et d’informer le directeur à propos de cette stratégie:
director = QgsVectorLayerDirector(vectorLayer, -1, "", "", "", 3) director.addStrategy(strategy)
Now we can use the builder, which will create the graph, using the
QgsGraphBuilderclass constructor.# only CRS is set, all other values are defaults builder = QgsGraphBuilder(vectorLayer.crs())
Also we can define several points, which will be used in the analysis. For example:
startPoint = QgsPointXY(1179720.1871, 5419067.3507) endPoint = QgsPointXY(1180616.0205, 5419745.7839)
Now all is in place so we can build the graph and « tie » these points to it:
tiedPoints = director.makeGraph(builder, [startPoint, endPoint])
Building the graph can take some time (which depends on the number of features in a layer and layer size).
tiedPointsis a list with coordinates of « tied » points.When the build operation is finished we can get the graph and use it for the analysis:
graph = builder.graph()
With the next code we can get the vertex indexes of our points:
startId = graph.findVertex(tiedPoints[0]) endId = graph.findVertex(tiedPoints[1])
19.2. Analyse de graphe
L’analyse de graphe est utilisée pour trouver des réponses aux deux questions: quels arcs sont connectés et comment trouver le plus court chemin ? Pour résoudre ces problèmes la bibliothèque d’analyse de graphe fournit l’algorithme de Dijkstra.
L’algorithme de Dijkstra trouve le plus court chemin entre un des arcs du graphe par rapport à tous les autres en tenant compte des paramètres d’optimisation. Ces résultats peuvent être représentés comme un arbre du chemin le plus court.
L’arbre du chemin le plus court est un graphique pondéré dirigé (ou plus précisément un arbre) ayant les propriétés suivantes :
Seul un arc n’a pas d’arcs entrants: la racine de l’arbre.
Tous les autres arcs n’ont qu’un seul arc entrant.
Si un arc B est atteignable depuis l’arc A alors le chemin de A vers B est le seul chemin disponible et il est le chemin optimal (le plus court) sur ce graphe.
Pour obtenir l’arbre du chemin le plus court, utilisez les méthodes shortestTree() et dijkstra() de la classe QgsGraphAnalyzer. Il est recommandé d’utiliser la méthode dijkstra() car elle fonctionne plus rapidement et utilise la mémoire de manière plus efficace.
The shortestTree() method
is useful when you want to walk around the shortest path tree.
It always creates a new graph object (QgsGraph)
and accepts three variables:
source— Graphique d’entréestartVertexIdx— index du point sur l’arbre (la racine de l’arbre)criterionNum— nombre de propriétés de bord à utiliser (à partir de 0).
tree = QgsGraphAnalyzer.shortestTree(graph, startId, 0)
La méthode dijkstra() a les mêmes arguments mais renvoie un tuple de tableaux :
In the first array, element n contains index of the incoming edge or -1 if there are no incoming edges.
In the second array, element n contains the distance from the root of the tree to vertex n or DOUBLE_MAX if vertex n is unreachable from the root.
(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, startId, 0)
Here is some very simple code to display the shortest path tree using the graph created
with the shortestTree()
or the dijkstra() method
(select linestring layer in Layers panel and replace coordinates with your own).
Avertissement
Utilisez ce code uniquement à titre d’exemple, il crée beaucoup d’objets QgsRubberBand et peut être lent sur de grands ensembles de données.
1from qgis.core import *
2from qgis.gui import *
3from qgis.analysis import *
4from qgis.PyQt.QtCore import *
5from qgis.PyQt.QtGui import *
6
7vectorLayer = QgsVectorLayer(
8 "testdata/network.gpkg|layername=network_lines", "lines"
9)
10director = QgsVectorLayerDirector(
11 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth
12)
13strategy = QgsNetworkDistanceStrategy()
14director.addStrategy(strategy)
15builder = QgsGraphBuilder(vectorLayer.crs())
16
17pStart = QgsPointXY(1179661.925139, 5419188.074362)
18tiedPoint = director.makeGraph(builder, [pStart])
19pStart = tiedPoint[0]
20
21graph = builder.graph()
22
23idStart = graph.findVertex(pStart)
24
25tree = QgsGraphAnalyzer.shortestTree(graph, idStart, 0)
26
27i = 0
28while i < tree.edgeCount():
29 rb = QgsRubberBand(iface.mapCanvas())
30 rb.setColor(Qt.red)
31 rb.addPoint(tree.vertex(tree.edge(i).fromVertex()).point())
32 rb.addPoint(tree.vertex(tree.edge(i).toVertex()).point())
33 i = i + 1
1from qgis.core import *
2from qgis.gui import *
3from qgis.analysis import *
4from qgis.PyQt.QtCore import *
5from qgis.PyQt.QtGui import *
6
7vectorLayer = QgsVectorLayer(
8 "testdata/network.gpkg|layername=network_lines", "lines"
9)
10director = QgsVectorLayerDirector(
11 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth
12)
13strategy = QgsNetworkDistanceStrategy()
14director.addStrategy(strategy)
15builder = QgsGraphBuilder(vectorLayer.crs())
16
17pStart = QgsPointXY(1179661.925139, 5419188.074362)
18tiedPoint = director.makeGraph(builder, [pStart])
19pStart = tiedPoint[0]
20
21graph = builder.graph()
22
23idStart = graph.findVertex(pStart)
24(tree, costs) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)
25
26for edgeId in tree:
27 if edgeId == -1:
28 continue
29 rb = QgsRubberBand(iface.mapCanvas())
30 rb.setColor(Qt.red)
31 rb.addPoint(graph.vertex(graph.edge(edgeId).fromVertex()).point())
32 rb.addPoint(graph.vertex(graph.edge(edgeId).toVertex()).point())
19.2.1. Trouver les chemins les plus courts
Pour trouver le chemin optimal entre deux points, l’approche suivante est utilisée. Les deux points (début A et fin B) sont « liés » au graphique lors de sa construction. Ensuite, en utilisant la méthode shortestTree() ou dijkstra() nous construisons l’arbre du chemin le plus court avec une racine au point de départ A. Dans le même arbre, nous trouvons également le point de fin B et commençons à parcourir l’arbre du point B au point A. L’algorithme complet peut être écrit sous la forme :
1assign T = B
2while T != B
3 add point T to path
4 get incoming edge for point T
5 look for point TT, that is start point of this edge
6 assign T = TT
7add point A to path
A ce niveau, nous avons le chemin, sous la forme d’une liste inversée d’arcs (les arcs sont listés dans un ordre inversé, depuis le point de la fin vers le point de démarrage) qui seront traversés lors de l’évolution sur le chemin.
Here is the sample code for QGIS Python Console (you may need to load and
select a linestring layer in TOC and replace coordinates in the code with yours) that
uses the shortestTree()
or dijkstra() method:
1from qgis.core import *
2from qgis.gui import *
3from qgis.analysis import *
4
5from qgis.PyQt.QtCore import *
6from qgis.PyQt.QtGui import *
7
8vectorLayer = QgsVectorLayer(
9 "testdata/network.gpkg|layername=network_lines", "lines"
10)
11director = QgsVectorLayerDirector(
12 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth
13)
14strategy = QgsNetworkDistanceStrategy()
15director.addStrategy(strategy)
16
17builder = QgsGraphBuilder(vectorLayer.sourceCrs())
18
19startPoint = QgsPointXY(1179661.925139, 5419188.074362)
20endPoint = QgsPointXY(1180942.970617, 5420040.097560)
21
22tiedPoints = director.makeGraph(builder, [startPoint, endPoint])
23tStart, tStop = tiedPoints
24
25graph = builder.graph()
26idxStart = graph.findVertex(tStart)
27
28tree = QgsGraphAnalyzer.shortestTree(graph, idxStart, 0)
29
30idxStart = tree.findVertex(tStart)
31idxEnd = tree.findVertex(tStop)
32
33if idxEnd == -1:
34 raise Exception("No route!")
35
36# Add last point
37route = [tree.vertex(idxEnd).point()]
38
39# Iterate the graph
40while idxEnd != idxStart:
41 edgeIds = tree.vertex(idxEnd).incomingEdges()
42 if len(edgeIds) == 0:
43 break
44 edge = tree.edge(edgeIds[0])
45 route.insert(0, tree.vertex(edge.fromVertex()).point())
46 idxEnd = edge.fromVertex()
47
48# Display
49rb = QgsRubberBand(iface.mapCanvas())
50rb.setColor(Qt.green)
51
52# This may require coordinate transformation if project's CRS
53# is different from layer's CRS
54for p in route:
55 rb.addPoint(p)
1from qgis.core import *
2from qgis.gui import *
3from qgis.analysis import *
4
5from qgis.PyQt.QtCore import *
6from qgis.PyQt.QtGui import *
7
8vectorLayer = QgsVectorLayer(
9 "testdata/network.gpkg|layername=network_lines", "lines"
10)
11director = QgsVectorLayerDirector(
12 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth
13)
14strategy = QgsNetworkDistanceStrategy()
15director.addStrategy(strategy)
16
17builder = QgsGraphBuilder(vectorLayer.sourceCrs())
18
19startPoint = QgsPointXY(1179661.925139, 5419188.074362)
20endPoint = QgsPointXY(1180942.970617, 5420040.097560)
21
22tiedPoints = director.makeGraph(builder, [startPoint, endPoint])
23tStart, tStop = tiedPoints
24
25graph = builder.graph()
26idxStart = graph.findVertex(tStart)
27idxEnd = graph.findVertex(tStop)
28
29(tree, costs) = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)
30
31if tree[idxEnd] == -1:
32 raise Exception('No route!')
33
34# Total cost
35cost = costs[idxEnd]
36
37# Add last point
38route = [graph.vertex(idxEnd).point()]
39
40# Iterate the graph
41while idxEnd != idxStart:
42 idxEnd = graph.edge(tree[idxEnd]).fromVertex()
43 route.insert(0, graph.vertex(idxEnd).point())
44
45# Display
46rb = QgsRubberBand(iface.mapCanvas())
47rb.setColor(Qt.red)
48
49# This may require coordinate transformation if project's CRS
50# is different from layer's CRS
51for p in route:
52 rb.addPoint(p)
19.2.2. Surfaces de disponibilité
La surface de disponibilité d’un arc A est le sous-ensemble des arcs du graphe qui sont accessibles à partir de l’arc A et où le coût des chemins à partir de A vers ces arcs ne dépasse pas une certaine valeur.
Plus clairement, cela peut être illustré par l’exemple suivant: « Il y a une caserne de pompiers. Quelles parties de la ville peuvent être atteintes par un camion de pompier en 5 minutes ? 10 minutes ? 15 minutes ? » La réponse à ces questions correspond aux surface de disponibilité de la caserne de pompiers.
Pour trouver les zones de disponibilité, nous pouvons utiliser la méthode dijkstra() de la classe QgsGraphAnalyzer. Il suffit de comparer les éléments du tableau des coûts avec une valeur prédéfinie. Si le coût [i] est inférieur ou égal à une valeur prédéfinie, alors le sommet i est à l’intérieur de la zone de disponibilité, sinon il est à l’extérieur.
Un problème plus difficile à régler est d’obtenir les frontières de la surface de disponibilité. La frontière inférieure est constituée par l’ensemble des arcs qui sont toujours accessibles et la frontière supérieure est composée des arcs qui ne sont pas accessibles. En fait, c’est très simple: c’est la limite de disponibilité des arcs de l’arbre du plus court chemin pour lesquels l’arc source de l’arc est accessible et l’arc cible ne l’est pas.
Here is an example:
1director = QgsVectorLayerDirector(
2 vectorLayer, -1, "", "", "", QgsVectorLayerDirector.DirectionBoth
3)
4strategy = QgsNetworkDistanceStrategy()
5director.addStrategy(strategy)
6builder = QgsGraphBuilder(vectorLayer.crs())
7
8
9pStart = QgsPointXY(1179661.925139, 5419188.074362)
10delta = iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel() * 1
11
12rb = QgsRubberBand(iface.mapCanvas())
13rb.setColor(Qt.green)
14rb.addPoint(QgsPointXY(pStart.x() - delta, pStart.y() - delta))
15rb.addPoint(QgsPointXY(pStart.x() + delta, pStart.y() - delta))
16rb.addPoint(QgsPointXY(pStart.x() + delta, pStart.y() + delta))
17rb.addPoint(QgsPointXY(pStart.x() - delta, pStart.y() + delta))
18
19tiedPoints = director.makeGraph(builder, [pStart])
20graph = builder.graph()
21tStart = tiedPoints[0]
22
23idStart = graph.findVertex(tStart)
24
25(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)
26
27upperBound = []
28r = 1500.0
29i = 0
30tree.reverse()
31
32while i < len(cost):
33 if cost[i] > r and tree[i] != -1:
34 outVertexId = graph.edge(tree [i]).toVertex()
35 if cost[outVertexId] < r:
36 upperBound.append(i)
37 i = i + 1
38
39for i in upperBound:
40 centerPoint = graph.vertex(i).point()
41 rb = QgsRubberBand(iface.mapCanvas())
42 rb.setColor(Qt.red)
43 rb.addPoint(QgsPointXY(centerPoint.x() - delta, centerPoint.y() - delta))
44 rb.addPoint(QgsPointXY(centerPoint.x() + delta, centerPoint.y() - delta))
45 rb.addPoint(QgsPointXY(centerPoint.x() + delta, centerPoint.y() + delta))
46 rb.addPoint(QgsPointXY(centerPoint.x() - delta, centerPoint.y() + delta))