Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/python/src/database.hh
Parent Directory
|
Revision Log
Revision 1966 -
(show annotations)
(download)
(as text)
Wed Nov 14 18:59:55 2018 UTC (6 years, 4 months ago) by amb
File MIME type: text/x-c++hdr
File size: 11272 byte(s)
Wed Nov 14 18:59:55 2018 UTC (6 years, 4 months ago) by amb
File MIME type: text/x-c++hdr
File size: 11272 byte(s)
Add a Python3 module to access the data in the Routino database.
1 | /*************************************** |
2 | Header file for interface between Routino database and Python. |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2018 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | #ifndef DATABASE_H |
23 | #define DATABASE_H /*+ To stop multiple inclusions. +*/ |
24 | |
25 | #include <vector> |
26 | |
27 | extern "C" { |
28 | |
29 | #include "types.h" |
30 | |
31 | #include "routino.h" |
32 | |
33 | } |
34 | |
35 | |
36 | /* Constants that are not automatically picked up from types.h */ |
37 | |
38 | const nodeflags_t Nodeflag_Super = NODE_SUPER; |
39 | const nodeflags_t Nodeflag_U_Turn = NODE_UTURN; |
40 | const nodeflags_t Nodeflag_Mini_Roundabout = NODE_MINIRNDBT; |
41 | const nodeflags_t Nodeflag_Turn_Restrict = NODE_TURNRSTRCT; |
42 | const nodeflags_t Nodeflag_Turn_Restrict2 = NODE_TURNRSTRCT2; |
43 | |
44 | const distance_t Segmentflag_Area = SEGMENT_AREA; |
45 | const distance_t Segmentflag_Oneway_1to2 = ONEWAY_1TO2; |
46 | const distance_t Segmentflag_Oneway_2to1 = ONEWAY_2TO1; |
47 | const distance_t Segmentflag_Super = SEGMENT_SUPER; |
48 | const distance_t Segmentflag_Normal = SEGMENT_NORMAL; |
49 | |
50 | |
51 | /* Classes (much easier to use them than C for doing this with swig) */ |
52 | |
53 | class PythonDatabase; |
54 | |
55 | class PythonNode; |
56 | class PythonSegment; |
57 | class PythonWay; |
58 | class PythonRelation; |
59 | |
60 | template <class T> class PythonDatabaseIter; |
61 | template <class T> class PythonNodeIter; |
62 | |
63 | |
64 | /* The database as seen by Python */ |
65 | |
66 | PythonDatabase *LoadDatabase(const char *dirname, const char *prefix); |
67 | PythonDatabase *LoadDatabase(const char *dirname); |
68 | |
69 | class PythonDatabase |
70 | { |
71 | public: |
72 | PythonDatabase(const char *_dirname,const char *_prefix, Routino_Database* database); /*+ A constructor +*/ |
73 | ~PythonDatabase(); /*+ A destructor to unload the database. +*/ |
74 | |
75 | PythonNode *GetNode(index_t item); /*+ Get a single node from the database. +*/ |
76 | PythonSegment *GetSegment(index_t item); /*+ Get a single segment from the database. +*/ |
77 | PythonWay *GetWay(index_t item); /*+ Get a single way from the database. +*/ |
78 | PythonRelation *GetRelation(index_t item); /*+ Get a single relation from the database. +*/ |
79 | |
80 | PythonDatabaseIter<PythonNode> *Nodes(); /*+ Create a node iterator to get all the nodes from the database. +*/ |
81 | PythonDatabaseIter<PythonSegment> *Segments(); /*+ Create a segment iterator to get all the segments from the database. +*/ |
82 | PythonDatabaseIter<PythonWay> *Ways(); /*+ Create a way iterator to get all the ways from the database. +*/ |
83 | PythonDatabaseIter<PythonRelation> *Relations(); /*+ Create a relation iterator to get all the relations from the database. +*/ |
84 | |
85 | index_t nnodes; /*+ The number of nodes in the database. +*/ |
86 | index_t nsegments; /*+ The number of segments in the database. +*/ |
87 | index_t nways; /*+ The number of ways in the database. +*/ |
88 | index_t nrelations; /*+ The number of relations in the database. +*/ |
89 | |
90 | char *__str__(); /*+ Convert the Python database to a string. +*/ |
91 | |
92 | friend class PythonNode; |
93 | friend class PythonSegment; |
94 | friend class PythonWay; |
95 | friend class PythonRelation; |
96 | |
97 | private: |
98 | |
99 | char *dirname; /*+ A copy of the database directory name. +*/ |
100 | char *prefix; /*+ A copy of the database prefix. +*/ |
101 | |
102 | Routino_Database *database; /*+ The database opened using the libroutino function. +*/ |
103 | }; |
104 | |
105 | |
106 | /* A node as seen by Python - copied from ../src/nodes.h and then modified */ |
107 | |
108 | class PythonNode |
109 | { |
110 | public: |
111 | PythonNode(PythonDatabase* _pydatabase) { pydatabase = _pydatabase; } /*+ A constructor passed the database. +*/ |
112 | |
113 | index_t id; /*+ The index of this node. +*/ |
114 | |
115 | index_t firstsegment; /*+ The index of the first segment. +*/ |
116 | |
117 | PythonNodeIter<PythonSegment> *Segments(); |
118 | |
119 | double latitude; /*+ The node latitude in degrees. +*/ |
120 | double longitude; /*+ The node longitude in degrees. +*/ |
121 | |
122 | transports_t allow; /*+ The types of transport that are allowed through the node. +*/ |
123 | nodeflags_t flags; /*+ Flags containing extra information (e.g. super-node, turn restriction). +*/ |
124 | |
125 | char *__str__(); /*+ Convert the Python node to a string. +*/ |
126 | |
127 | private: |
128 | |
129 | friend class PythonNodeIter<PythonSegment>; |
130 | |
131 | PythonDatabase *pydatabase; /*+ A pointer to the database that this node came from. +*/ |
132 | |
133 | std::vector<index_t> segments; /*+ The list of segments for this node, only filled in after calling Segments(). +*/ |
134 | |
135 | PythonSegment *get_segment(index_t item); /*+ Get a single segment from the node. +*/ |
136 | void fill_segments(); /*+ Fill in the list of segments. +*/ |
137 | }; |
138 | |
139 | |
140 | /* A segment as seen by Python - copied from ../src/segments.h and then modified */ |
141 | |
142 | class PythonSegment |
143 | { |
144 | public: |
145 | PythonSegment(PythonDatabase* _pydatabase) { pydatabase = _pydatabase; } /*+ A constructor passed the database. +*/ |
146 | |
147 | index_t id; /*+ The index of this segment. +*/ |
148 | |
149 | index_t node1; /*+ The index of the starting node. +*/ |
150 | index_t node2; /*+ The index of the finishing node. +*/ |
151 | |
152 | PythonNode *Node1() { return pydatabase->GetNode(node1); } |
153 | PythonNode *Node2() { return pydatabase->GetNode(node2); } |
154 | |
155 | index_t next2; /*+ The index of the next segment sharing node2. +*/ |
156 | |
157 | index_t way; /*+ The index of the way associated with the segment. +*/ |
158 | |
159 | PythonWay *Way() { return pydatabase->GetWay(way); } |
160 | |
161 | double distance; /*+ The distance between the nodes. +*/ |
162 | |
163 | distance_t flags; /*+ The flags associated with the segment. +*/ |
164 | |
165 | char *__str__(); /*+ Convert the Python segment to a string. +*/ |
166 | |
167 | private: |
168 | |
169 | PythonDatabase *pydatabase; /*+ A pointer to the database that this segment came from. +*/ |
170 | }; |
171 | |
172 | |
173 | /* A way as seen by Python - copied from ../src/ways.h and then modified */ |
174 | |
175 | class PythonWay |
176 | { |
177 | public: |
178 | PythonWay(PythonDatabase* _pydatabase) { pydatabase = _pydatabase; } /*+ A constructor passed the database. +*/ |
179 | |
180 | index_t id; /*+ The index of this way. +*/ |
181 | |
182 | char *name; /*+ The offset of the name of the way in the names array. +*/ |
183 | |
184 | transports_t allow; /*+ The type of traffic allowed on the way. +*/ |
185 | |
186 | highway_t type; /*+ The highway type of the way. +*/ |
187 | |
188 | properties_t props; /*+ The properties of the way. +*/ |
189 | |
190 | double speed; /*+ The defined maximum speed limit of the way. +*/ |
191 | |
192 | double weight; /*+ The defined maximum weight of traffic on the way. +*/ |
193 | double height; /*+ The defined maximum height of traffic on the way. +*/ |
194 | double width; /*+ The defined maximum width of traffic on the way. +*/ |
195 | double length; /*+ The defined maximum length of traffic on the way. +*/ |
196 | |
197 | char *__str__(); /*+ Convert the Python way to a string. +*/ |
198 | |
199 | private: |
200 | |
201 | PythonDatabase *pydatabase; /*+ A pointer to the database that this segment came from. +*/ |
202 | }; |
203 | |
204 | |
205 | /* A relation as seen by Python - copied from ../src/relations.h and then modified */ |
206 | |
207 | class PythonRelation |
208 | { |
209 | public: |
210 | PythonRelation(PythonDatabase* _pydatabase) { pydatabase = _pydatabase; } /*+ A constructor passed the database. +*/ |
211 | |
212 | index_t id; /*+ The index of this relation. +*/ |
213 | |
214 | index_t from_seg; /*+ The segment that the path comes from. +*/ |
215 | index_t via_node; /*+ The node that the path goes via. +*/ |
216 | index_t to_seg; /*+ The segment that the path goes to. +*/ |
217 | |
218 | PythonSegment *FromSegment() { return pydatabase->GetSegment(from_seg); } |
219 | PythonNode *ViaNode() { return pydatabase->GetNode(via_node); } |
220 | PythonSegment *ToSegment() { return pydatabase->GetSegment(to_seg); } |
221 | |
222 | index_t from_way; /*+ The way that the path comes from. +*/ |
223 | index_t to_way; /*+ The way that the path goes to. +*/ |
224 | |
225 | PythonWay *FromWay() { return pydatabase->GetWay(from_way); } |
226 | PythonWay *ToWay() { return pydatabase->GetWay(to_way); } |
227 | |
228 | index_t from_node; /*+ The node that the path comes from. +*/ |
229 | index_t to_node; /*+ The node that the path goes to. +*/ |
230 | |
231 | PythonNode *FromNode() { return pydatabase->GetNode(from_node); } |
232 | PythonNode *ToNode() { return pydatabase->GetNode(to_node); } |
233 | |
234 | transports_t except_transport; /*+ The types of transports that that this relation does not apply to. +*/ |
235 | |
236 | char *__str__(); /*+ Convert the Python relation to a string. +*/ |
237 | |
238 | private: |
239 | |
240 | PythonDatabase *pydatabase; /*+ A pointer to the database that this segment came from. +*/ |
241 | }; |
242 | |
243 | |
244 | /* A generic node/segment/way/relation iterator */ |
245 | |
246 | template <class T> class PythonDatabaseIter |
247 | { |
248 | public: |
249 | |
250 | PythonDatabaseIter(PythonDatabase* _pydatabase, index_t _number) { pydatabase = _pydatabase; number = _number; } /*+ A constructor passed the database. +*/ |
251 | |
252 | index_t __len__() { return number; } /*+ When used as a list return the length of it. +*/ |
253 | T *__getitem__(index_t index); /*+ When used as a list get a particular item from it. +*/ |
254 | |
255 | PythonDatabaseIter *__iter__() { return this; } /*+ When used as an iterator return itself. +*/ |
256 | T *__next__() { if( next < number ) return __getitem__(next++); else return NULL; } /*+ When used as an iterator return the next item. +*/ |
257 | |
258 | private: |
259 | |
260 | index_t next=0; /*+ The next node/segment/way/relation to be returned. +*/ |
261 | index_t number; /*+ The number of nodes/segments/ways/relations in total. +*/ |
262 | |
263 | PythonDatabase *pydatabase; /*+ A pointer to the database that this node/segment/way/relation came from. +*/ |
264 | }; |
265 | |
266 | |
267 | /* A segment iterator for nodes */ |
268 | |
269 | template <class T> class PythonNodeIter |
270 | { |
271 | public: |
272 | |
273 | PythonNodeIter(PythonNode *_pynode, index_t _number) { pynode = _pynode; number = _number; } /*+ A constructor passed the node. +*/ |
274 | |
275 | index_t __len__() { return number; } /*+ When used as a list return the length of it. +*/ |
276 | T *__getitem__(index_t index); /*+ When used as a list get a particular item from it. +*/ |
277 | |
278 | PythonNodeIter *__iter__() { return this; } /*+ When used as an iterator return itself. +*/ |
279 | T *__next__() { if( next < number ) return __getitem__(next++); else return NULL; } /*+ When used as an iterator return the next item. +*/ |
280 | |
281 | private: |
282 | |
283 | index_t next=0; /*+ The next segment to be returned. +*/ |
284 | index_t number; /*+ The number of segments in total. +*/ |
285 | |
286 | PythonNode *pynode; /*+ A pointer to the node that these segments come from. +*/ |
287 | }; |
288 | |
289 | #endif /* DATABASE_H */ |