Class: QgsGraph

class qgis.analysis.QgsGraph

Bases: sip.wrapper

Mathematical graph representation

New in version 3.0.

QgsGraph() Constructor for QgsGraph.

QgsGraph(QgsGraph)

Methods

addEdge

Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.

addVertex

Add a vertex to the graph

edge

Returns the edge at the given index.

edgeCount

Returns number of graph edges

findOppositeEdge

Finds the first edge which is the opposite of the edge with the specified index.

findVertex

Find vertex by associated point

hasEdge

Returns whether the edge of the given index exists.

hasVertex

Returns whether the vertex of the given index exists.

removeEdge

Removes the edge at specified index.

removeVertex

Removes the vertex at specified index.

vertex

Returns the vertex at the given index.

vertexCount

Returns number of graph vertices

addEdge(self, fromVertexIdx: int, toVertexIdx: int, strategies: Iterable[Any]) int

Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.

Parameters
  • fromVertexIdx (int) –

  • toVertexIdx (int) –

  • strategies (Iterable[Any]) –

Return type

int

addVertex(self, pt: QgsPointXY) int

Add a vertex to the graph

Parameters

pt (QgsPointXY) –

Return type

int

edge(self, idx: int) QgsGraphEdge

Returns the edge at the given index.

Raises

IndexError – if the edge is not found.

Parameters

idx (int) –

Return type

QgsGraphEdge

edgeCount(self) int

Returns number of graph edges

Return type

int

findOppositeEdge(self, index: int) int

Finds the first edge which is the opposite of the edge with the specified index.

This represents the edge which has the same vertices as the specified edge, but the opposite direction in the graph.(I.e. the edge which starts at the “from” vertex of the specified edge and ends at the “to” vertex.)

Returns -1 if no opposite edge exists.

Raises

IndexError – if the edge with the specified index is not found.

New in version 3.24.

Parameters

index (int) –

Return type

int

findVertex(self, pt: QgsPointXY) int

Find vertex by associated point

Return type

int

Returns

vertex index

Parameters

pt (QgsPointXY) –

hasEdge(self, index: int) bool

Returns whether the edge of the given index exists.

New in version 3.24.

Parameters

index (int) –

Return type

bool

hasVertex(self, index: int) bool

Returns whether the vertex of the given index exists.

New in version 3.24.

Parameters

index (int) –

Return type

bool

removeEdge(self, index: int)

Removes the edge at specified index.

The incoming and outgoing edges for all graph vertices will be updated accordingly. Vertices which no longer have any incoming or outgoing edges as a result will be removed from the graph automatically.

Raises

IndexError – if the vertex is not found.

New in version 3.24.

Parameters

index (int) –

removeVertex(self, index: int)

Removes the vertex at specified index.

All edges which are incoming or outgoing edges for the vertex will also be removed.

Raises

IndexError – if the vertex is not found.

New in version 3.24.

Parameters

index (int) –

vertex(self, idx: int) QgsGraphVertex

Returns the vertex at the given index.

Raises

IndexError – if the vertex is not found.

Parameters

idx (int) –

Return type

QgsGraphVertex

vertexCount(self) int

Returns number of graph vertices

Return type

int