QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 QgsGraphEdge() = default;
47
52 QVariant cost( int strategyIndex ) const;
53
57 QVector<QVariant> strategies() const;
58
63 int toVertex() const;
64
69 int fromVertex() const;
70
71 private:
72 QVector<QVariant> mStrategies;
73
74 int mToIdx = 0;
75 int mFromIdx = 0;
76
77 friend class QgsGraph;
78};
79
80
81typedef QList<int> QgsGraphEdgeIds;
82
88class ANALYSIS_EXPORT QgsGraphVertex
89{
90 public:
91 QgsGraphVertex() = default;
92
97 QgsGraphVertex( const QgsPointXY &point );
98
103 QgsGraphEdgeIds incomingEdges() const;
104
109 QgsGraphEdgeIds outgoingEdges() const;
110
114 QgsPointXY point() const;
115
116 private:
117 QgsPointXY mCoordinate;
118 QgsGraphEdgeIds mIncomingEdges;
119 QgsGraphEdgeIds mOutgoingEdges;
120
121 friend class QgsGraph;
122};
123
130class ANALYSIS_EXPORT QgsGraph
131{
132 public:
133 QgsGraph() = default;
134
135 // Graph constructing methods
136
140 int addVertex( const QgsPointXY &pt );
141
146 int addEdge( int fromVertexIdx, int toVertexIdx, const QVector<QVariant> &strategies );
147
151 int vertexCount() const;
152
153#ifndef SIP_RUN
154
158 const QgsGraphVertex &vertex( int idx ) const;
159#else
160
166 QgsGraphVertex vertex( int idx ) const;
167 //%MethodCode
168 if ( sipCpp->hasVertex( a0 ) )
169 {
170 return sipConvertFromNewType( new QgsGraphVertex( sipCpp->vertex( a0 ) ), sipType_QgsGraphVertex, Py_None );
171 }
172 else
173 {
174 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
175 sipIsErr = 1;
176 }
177 //%End
178#endif
179
180#ifndef SIP_RUN
181
189 void removeVertex( int index );
190#else
191
200 void removeVertex( int index );
201 //%MethodCode
202 if ( sipCpp->hasVertex( a0 ) )
203 {
204 sipCpp->removeVertex( a0 );
205 }
206 else
207 {
208 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
209 sipIsErr = 1;
210 }
211 //%End
212#endif
213
217 int edgeCount() const;
218
219#ifndef SIP_RUN
220
224 const QgsGraphEdge &edge( int idx ) const;
225#else
226
232 QgsGraphEdge edge( int idx ) const;
233 //%MethodCode
234 if ( sipCpp->hasEdge( a0 ) )
235 {
236 return sipConvertFromNewType( new QgsGraphEdge( sipCpp->edge( a0 ) ), sipType_QgsGraphEdge, Py_None );
237 }
238 else
239 {
240 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
241 sipIsErr = 1;
242 }
243 //%End
244#endif
245
246
247#ifndef SIP_RUN
248
257 void removeEdge( int index );
258#else
259
269 void removeEdge( int index );
270 //%MethodCode
271 if ( sipCpp->hasEdge( a0 ) )
272 {
273 sipCpp->removeEdge( a0 );
274 }
275 else
276 {
277 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
278 sipIsErr = 1;
279 }
280 //%End
281#endif
282
287 int findVertex( const QgsPointXY &pt ) const;
288
289#ifndef SIP_RUN
290
302 int findOppositeEdge( int index ) const;
303#else
304
318 int findOppositeEdge( int index ) const;
319 //%MethodCode
320 if ( sipCpp->hasEdge( a0 ) )
321 {
322 sipRes = sipCpp->findOppositeEdge( a0 );
323 }
324 else
325 {
326 PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
327 sipIsErr = 1;
328 }
329 //%End
330#endif
331
337 bool hasEdge( int index ) const;
338
344 bool hasVertex( int index ) const;
345
346 protected:
347#ifndef SIP_RUN
349 QHash<int, QgsGraphVertex> mGraphVertices;
350
352 QHash<int, QgsGraphEdge> mGraphEdges;
353#endif
354
355
356 private:
357 int mNextVertexId = 0;
358 int mNextEdgeId = 0;
359};
360
361#endif // QGSGRAPH_H
This class implements a graph edge.
Definition qgsgraph.h:44
QgsGraphEdge()=default
This class implements a graph vertex.
Definition qgsgraph.h:89
QgsGraphVertex()=default
Mathematical graph representation.
Definition qgsgraph.h:131
QHash< int, QgsGraphVertex > mGraphVertices
Graph vertices.
Definition qgsgraph.h:349
QHash< int, QgsGraphEdge > mGraphEdges
Graph edges.
Definition qgsgraph.h:352
QgsGraph()=default
A class to represent a 2D point.
Definition qgspointxy.h:60
QList< int > QgsGraphEdgeIds
Definition qgsgraph.h:81