QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsgraph.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraph.h
3 --------------------------------------
4 Date : 2011-04-01
5 Copyright : (C) 2010 by Yakushev Sergey
6 Email : YakushevS <at> list.ru
7****************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
16/*
17 * This file describes the built-in QGIS classes for modeling a mathematical graph.
18 * Vertices are identified by their geographic coordinates and have no additional
19 * properties. Number of strategies for calculating edge cost is not limited.
20 * Graph may have incidence edges.
21 *
22 * \file qgsgraph.h
23 */
24
25#ifndef QGSGRAPH_H
26#define QGSGRAPH_H
27
28#include <QList>
29#include <QVector>
30#include <QVariant>
31
32#include "qgspointxy.h"
33#include "qgis_analysis.h"
34
35class QgsGraphVertex;
36
37
43class ANALYSIS_EXPORT QgsGraphEdge
44{
45 public:
46
50 QgsGraphEdge() = default;
51
56 QVariant cost( int strategyIndex ) const;
57
61 QVector< QVariant > strategies() const;
62
67 int toVertex() const;
68
73 int fromVertex() const;
74
75 private:
76
77 QVector< QVariant > mStrategies;
78
79 int mToIdx = 0;
80 int mFromIdx = 0;
81
82 friend class QgsGraph;
83};
84
85
86typedef QList< int > QgsGraphEdgeIds;
87
93class ANALYSIS_EXPORT QgsGraphVertex
94{
95 public:
96
100 QgsGraphVertex() = default;
101
106 QgsGraphVertex( const QgsPointXY &point );
107
112 QgsGraphEdgeIds incomingEdges() const;
113
118 QgsGraphEdgeIds outgoingEdges() const;
119
123 QgsPointXY point() const;
124
125 private:
126 QgsPointXY mCoordinate;
127 QgsGraphEdgeIds mIncomingEdges;
128 QgsGraphEdgeIds mOutgoingEdges;
129
130 friend class QgsGraph;
131};
132
139class ANALYSIS_EXPORT QgsGraph
140{
141 public:
142
146 QgsGraph() = default;
147
148 // Graph constructing methods
149
153 int addVertex( const QgsPointXY &pt );
154
159 int addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies );
160
164 int vertexCount() const;
165
166#ifndef SIP_RUN
167
171 const QgsGraphVertex &vertex( int idx ) const;
172#else
173
179 QgsGraphVertex vertex( int idx ) const;
180 % MethodCode
181 if ( sipCpp->hasVertex( a0 ) )
182 {
183 return sipConvertFromNewType( new QgsGraphVertex( sipCpp->vertex( a0 ) ), sipType_QgsGraphVertex, Py_None );
184 }
185 else
186 {
187 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
188 sipIsErr = 1;
189 }
190 % End
191#endif
192
193#ifndef SIP_RUN
194
202 void removeVertex( int index );
203#else
204
213 void removeVertex( int index );
214 % MethodCode
215 if ( sipCpp->hasVertex( a0 ) )
216 {
217 sipCpp->removeVertex( a0 );
218 }
219 else
220 {
221 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
222 sipIsErr = 1;
223 }
224 % End
225#endif
226
230 int edgeCount() const;
231
232#ifndef SIP_RUN
233
237 const QgsGraphEdge &edge( int idx ) const;
238#else
239
245 QgsGraphEdge edge( int idx ) const;
246 % MethodCode
247 if ( sipCpp->hasEdge( a0 ) )
248 {
249 return sipConvertFromNewType( new QgsGraphEdge( sipCpp->edge( a0 ) ), sipType_QgsGraphEdge, Py_None );
250 }
251 else
252 {
253 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
254 sipIsErr = 1;
255 }
256 % End
257#endif
258
259
260#ifndef SIP_RUN
261
270 void removeEdge( int index );
271#else
272
282 void removeEdge( int index );
283 % MethodCode
284 if ( sipCpp->hasEdge( a0 ) )
285 {
286 sipCpp->removeEdge( a0 );
287 }
288 else
289 {
290 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
291 sipIsErr = 1;
292 }
293 % End
294#endif
295
300 int findVertex( const QgsPointXY &pt ) const;
301
302#ifndef SIP_RUN
303
315 int findOppositeEdge( int index ) const;
316#else
317
331 int findOppositeEdge( int index ) const;
332 % MethodCode
333 if ( sipCpp->hasEdge( a0 ) )
334 {
335 sipRes = sipCpp->findOppositeEdge( a0 );
336 }
337 else
338 {
339 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
340 sipIsErr = 1;
341 }
342 % End
343#endif
344
350 bool hasEdge( int index ) const;
351
357 bool hasVertex( int index ) const;
358
359 protected:
360
361#ifndef SIP_RUN
363 QHash<int, QgsGraphVertex> mGraphVertices;
364
366 QHash<int, QgsGraphEdge> mGraphEdges;
367#endif
368
369
370 private:
371
372 int mNextVertexId = 0;
373 int mNextEdgeId = 0;
374};
375
376#endif // QGSGRAPH_H
This class implements a graph edge.
Definition: qgsgraph.h:44
QgsGraphEdge()=default
Constructor for QgsGraphEdge.
This class implements a graph vertex.
Definition: qgsgraph.h:94
QgsGraphVertex()=default
Default constructor.
Mathematical graph representation.
Definition: qgsgraph.h:140
QHash< int, QgsGraphVertex > mGraphVertices
Graph vertices.
Definition: qgsgraph.h:363
QHash< int, QgsGraphEdge > mGraphEdges
Graph edges.
Definition: qgsgraph.h:366
QgsGraph()=default
Constructor for QgsGraph.
A class to represent a 2D point.
Definition: qgspointxy.h:60
QList< int > QgsGraphEdgeIds
Definition: qgsgraph.h:86