QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
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
47 QgsGraphEdge() = default;
48
53 QVariant cost( int strategyIndex ) const;
54
58 QVector< QVariant > strategies() const;
59
64 int toVertex() const;
65
70 int fromVertex() const;
71
72 private:
73
74 QVector< QVariant > mStrategies;
75
76 int mToIdx = 0;
77 int mFromIdx = 0;
78
79 friend class QgsGraph;
80};
81
82
83typedef QList< int > QgsGraphEdgeIds;
84
90class ANALYSIS_EXPORT QgsGraphVertex
91{
92 public:
93
94 QgsGraphVertex() = default;
95
100 QgsGraphVertex( const QgsPointXY &point );
101
106 QgsGraphEdgeIds incomingEdges() const;
107
112 QgsGraphEdgeIds outgoingEdges() const;
113
117 QgsPointXY point() const;
118
119 private:
120 QgsPointXY mCoordinate;
121 QgsGraphEdgeIds mIncomingEdges;
122 QgsGraphEdgeIds mOutgoingEdges;
123
124 friend class QgsGraph;
125};
126
133class ANALYSIS_EXPORT QgsGraph
134{
135 public:
136
137 QgsGraph() = default;
138
139 // Graph constructing methods
140
144 int addVertex( const QgsPointXY &pt );
145
150 int addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies );
151
155 int vertexCount() const;
156
157#ifndef SIP_RUN
158
162 const QgsGraphVertex &vertex( int idx ) const;
163#else
164
170 QgsGraphVertex vertex( int idx ) const;
171 % MethodCode
172 if ( sipCpp->hasVertex( a0 ) )
173 {
174 return sipConvertFromNewType( new QgsGraphVertex( sipCpp->vertex( a0 ) ), sipType_QgsGraphVertex, Py_None );
175 }
176 else
177 {
178 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
179 sipIsErr = 1;
180 }
181 % End
182#endif
183
184#ifndef SIP_RUN
185
193 void removeVertex( int index );
194#else
195
204 void removeVertex( int index );
205 % MethodCode
206 if ( sipCpp->hasVertex( a0 ) )
207 {
208 sipCpp->removeVertex( a0 );
209 }
210 else
211 {
212 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
213 sipIsErr = 1;
214 }
215 % End
216#endif
217
221 int edgeCount() const;
222
223#ifndef SIP_RUN
224
228 const QgsGraphEdge &edge( int idx ) const;
229#else
230
236 QgsGraphEdge edge( int idx ) const;
237 % MethodCode
238 if ( sipCpp->hasEdge( a0 ) )
239 {
240 return sipConvertFromNewType( new QgsGraphEdge( sipCpp->edge( a0 ) ), sipType_QgsGraphEdge, Py_None );
241 }
242 else
243 {
244 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
245 sipIsErr = 1;
246 }
247 % End
248#endif
249
250
251#ifndef SIP_RUN
252
261 void removeEdge( int index );
262#else
263
273 void removeEdge( int index );
274 % MethodCode
275 if ( sipCpp->hasEdge( a0 ) )
276 {
277 sipCpp->removeEdge( a0 );
278 }
279 else
280 {
281 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
282 sipIsErr = 1;
283 }
284 % End
285#endif
286
291 int findVertex( const QgsPointXY &pt ) const;
292
293#ifndef SIP_RUN
294
306 int findOppositeEdge( int index ) const;
307#else
308
322 int findOppositeEdge( int index ) const;
323 % MethodCode
324 if ( sipCpp->hasEdge( a0 ) )
325 {
326 sipRes = sipCpp->findOppositeEdge( a0 );
327 }
328 else
329 {
330 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
331 sipIsErr = 1;
332 }
333 % End
334#endif
335
341 bool hasEdge( int index ) const;
342
348 bool hasVertex( int index ) const;
349
350 protected:
351
352#ifndef SIP_RUN
354 QHash<int, QgsGraphVertex> mGraphVertices;
355
357 QHash<int, QgsGraphEdge> mGraphEdges;
358#endif
359
360
361 private:
362
363 int mNextVertexId = 0;
364 int mNextEdgeId = 0;
365};
366
367#endif // QGSGRAPH_H
This class implements a graph edge.
Definition qgsgraph.h:44
QgsGraphEdge()=default
This class implements a graph vertex.
Definition qgsgraph.h:91
QgsGraphVertex()=default
Mathematical graph representation.
Definition qgsgraph.h:134
QHash< int, QgsGraphVertex > mGraphVertices
Graph vertices.
Definition qgsgraph.h:354
QHash< int, QgsGraphEdge > mGraphEdges
Graph edges.
Definition qgsgraph.h:357
QgsGraph()=default
A class to represent a 2D point.
Definition qgspointxy.h:60
QList< int > QgsGraphEdgeIds
Definition qgsgraph.h:83