source: subversion/applications/utils/import/srtm2wayinfo/osmtypes.h

Last change on this file was 17101, checked in by herm, 8 years ago

Support different storage systems for ways.

File size: 5.2 KB
Line 
1/* Copyright (c) 2009 Hermann Kraus
2 * This software is available under a "MIT Style" license
3 * (see COPYING).
4 */
5/** \file
6  * The basic types (nodes, ways) and efficient storage for them.
7  */
8#ifndef __OSMTYPES_H__
9#define __OSMTYPES_H__
10
11#include <QStringRef>
12#include <QVector>
13#include <QMap>
14#include <QDebug>
15#include <QTemporaryFile>
16
17/** Typedef for node IDs. Allows easy identification of node IDs in code and changing to a different data type possible. */
18typedef int OsmNodeId;
19
20/** Typedef for way IDs. Allows easy identification of way IDs in code and changing to a different data type possible. */
21typedef int OsmWayId;
22
23/** Typedef for relation IDs. Allows easy identification of relation IDs in code and changing to a different data type possible. */
24typedef int OsmRelationId;
25
26
27/** Stores information about an OSM node.
28  * Most functions should be defined in the header so the can be inlined
29  * because they will be called very often.
30  * This class should be optimized for storage size and can store different
31  * values together. All data is accessed via functions to allow unpacking of
32  * data.
33  *
34  * We only want to know if a node is a junction. Therefore we can stop counting at
35  * order 2. Data about the order is stored in the lat value.
36  * Normal lat values: -90° to +90°         (+  0°)
37  * 1st order node:    110° to 290°         (+200°)
38  * 2nd order node:    310° to 490°         (+400°)
39  * I choose 200° instead of 180° to avoid corner cases and because the values are
40  * easier to read for a human.
41  */
42class OsmNode
43{
44    public:
45        /** Default constructor.
46          * Creates an invalid node.
47          * \note This function is required for QList<OsmNode>.
48          */
49        OsmNode() { lat_ = 0; lon_ = 0; }
50
51        /** Constructor with initialisation. */
52        OsmNode(float lat, float lon)
53        {
54            lat_ = lat;
55            lon_ = lon;
56        }
57
58        /** Return latitude value. */
59        float lat() {
60            if (lat_ <= 90.0) {
61                return lat_;
62            } else if (lat_ <= 290.0) {
63                return lat_ - 200.0;
64            } else {
65                return lat_ - 400.0;
66            }
67        }
68
69        /** Return longitude value. */
70        float lon() { return lon_; }
71
72        /** Increase order of this node.
73          * \note The counter is only required to count up to order 2, but is
74          * allowed to count up to any value. */
75        void incOrder() {
76            if (lat_ <= 290.0) lat_ += 200.0;
77        }
78
79        /** Check if a node is an intersection. */
80        bool isIntersection() { return lat_ > 290.0; }
81
82    private:
83        float lat_, lon_;
84};
85
86/** Stores information about a way. */
87class OsmWay
88{
89    public:
90
91        /** Sets the way id */
92        void setId(OsmWayId id_)
93        {
94            id = id_;
95        }
96
97        /** Add a node to this way. */
98        void addNode(OsmNodeId nodeid)
99        {
100            nodes.append(nodeid);
101        }
102
103        /** Deletes all data and make this way object reusable. */
104        void clear()
105        {
106            nodes.clear();
107            id = 0;
108        }
109
110        /** Way id. */
111        OsmWayId id;
112
113        /** List of all node IDs that are part of this way. */
114        QVector<OsmNodeId> nodes;
115};
116
117
118/** Provides optimized storage for OsmNode objects.
119  * Can be configured to behave differently depending on the input
120  * dataset size.
121  * \note This is an abstract base class.
122  */
123class OsmNodeStorage
124{
125    public:
126        virtual OsmNode& operator[](OsmNodeId id) = 0;
127};
128
129/** Node storage for a small amount of nodes. */
130class OsmNodeStorageSmall: public OsmNodeStorage
131{
132    public:
133        OsmNodeStorageSmall() { qDebug() << "Small node storage."; };
134        virtual OsmNode& operator[](OsmNodeId id) { return nodes[id]; }
135    private:
136        QMap<OsmNodeId, OsmNode> nodes;
137};
138
139/** Node storage for a small amount of nodes. */
140class OsmNodeStorageMedium: public OsmNodeStorage
141{
142    public:
143        OsmNodeStorageMedium();
144        virtual OsmNode& operator[](OsmNodeId id);
145    private:
146        QMap<int, OsmNode*> blocks; /*maps blocknr to block-array*/
147        OsmNode dummyNode;
148};
149
150/** Node storage for a large amount of nodes. */
151class OsmNodeStorageLarge: public OsmNodeStorage
152{
153    public:
154        OsmNodeStorageLarge();
155        virtual OsmNode& operator[](OsmNodeId id);
156    private:
157        OsmNode** nodes;
158        QMap<OsmNodeId, OsmNode> negative_nodes;
159        OsmNode dummyNode;
160};
161
162/** FIFO system storing ways. */
163class OsmWayStorage
164{
165    public:
166        virtual ~OsmWayStorage() {}
167        virtual void append(OsmWay &input)=0;
168        virtual void startReading() {};
169        virtual bool get(OsmWay &output)=0;
170};
171
172class OsmWayStorageMem: public OsmWayStorage
173{
174    public:
175        OsmWayStorageMem() { pos = 0; }
176        virtual void append(OsmWay &input);
177        virtual bool get(OsmWay &output);
178    private:
179        QVector<OsmWay *> ways;
180        int pos;
181};
182
183class OsmWayStorageDisk: public OsmWayStorage
184{
185    public:
186        OsmWayStorageDisk(QString tmp_dir);
187        virtual ~OsmWayStorageDisk();
188        virtual void append(OsmWay &input);
189        virtual bool get(OsmWay &output);
190        virtual void startReading();
191    private:
192        QDataStream stream;
193        QTemporaryFile *file;
194};
195
196#endif
Note: See TracBrowser for help on using the repository browser.