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

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

Support different storage systems for ways.

File size: 2.9 KB
Line 
1/* Copyright (c) 2009 Hermann Kraus
2 * This software is available under a "MIT Style" license
3 * (see COPYING).
4 * Parts of this file are taken from osm2pgsql.
5 */
6/** \file
7  * The basic types (nodes, ways) and efficient storage for them.
8  */
9#include "osmtypes.h"
10#include <QDebug>
11
12/** Defines the block size. 2^BLOCK_SHIFT nodes are allocated at a time. */
13#define LARGE_BLOCK_SHIFT 8
14/** Defines the block size. 2^BLOCK_SHIFT nodes are allocated at a time. */
15#define MEDIUM_BLOCK_SHIFT 5
16
17OsmNodeStorageLarge::OsmNodeStorageLarge()
18{
19    qDebug() << "Large node storage.";
20    nodes = new OsmNode*[(1 << (31 - LARGE_BLOCK_SHIFT))];
21}
22
23/** Returns the node object for the given id.
24  * If required additional storage space is allocated. */
25OsmNode& OsmNodeStorageLarge::operator[](OsmNodeId id)
26{
27    if (id < 0) {
28        /* This saves a lot of overhead for the usually very few IDs */
29        return negative_nodes[id];
30    }
31    int block  =  (id >> LARGE_BLOCK_SHIFT);
32    int offset = id & ((1 << LARGE_BLOCK_SHIFT)-1);
33    if (!nodes[block]) {
34        nodes[block] = (OsmNode *)malloc((1 << LARGE_BLOCK_SHIFT) * sizeof(OsmNode));
35        if (!nodes[block]) {
36            qCritical() << "Error allocating nodes";
37            exit(1);
38        }
39    }
40    return nodes[block][offset];
41}
42
43/********************************************************************************/
44
45OsmNodeStorageMedium::OsmNodeStorageMedium()
46{
47    qDebug() << "Medium node storage.";
48}
49
50/** Returns the node object for the given id.
51  * If required additional storage space is allocated. */
52OsmNode& OsmNodeStorageMedium::operator[](OsmNodeId id)
53{
54    int block  =  (id >> MEDIUM_BLOCK_SHIFT);
55    int offset = id & ((1 << MEDIUM_BLOCK_SHIFT)-1);
56    if (!blocks.contains(block)) {
57        blocks[block] = (OsmNode *)malloc((1 << MEDIUM_BLOCK_SHIFT) * sizeof(OsmNode));
58        if (!blocks[block]) {
59            qCritical() << "Error allocating nodes";
60            exit(1);
61        }
62    }
63    return blocks[block][offset];
64}
65
66
67void OsmWayStorageMem::append(OsmWay &way)
68{
69    OsmWay *cloned = new OsmWay(way);
70    cloned->nodes.squeeze();
71    ways.append(cloned);
72}
73
74bool OsmWayStorageMem::get(OsmWay &output)
75{
76    if (pos >= ways.size()) {
77        return false;
78    }
79    output = *(ways.at(pos++));
80    return true;
81}
82
83OsmWayStorageDisk::OsmWayStorageDisk(QString tmp_dir)
84{
85    file = new QTemporaryFile(tmp_dir+"/ways_XXXXXX");
86    if (file->open()) {
87        stream.setDevice(file);
88    } else {
89        qCritical() << "Could not create temporary file in" << tmp_dir;
90        exit(1);
91    }
92}
93
94
95void OsmWayStorageDisk::append(OsmWay &input)
96{
97    stream << input.id << input.nodes;
98}
99
100void OsmWayStorageDisk::startReading()
101{
102    file->seek(0);
103}
104
105
106bool OsmWayStorageDisk::get(OsmWay &output)
107{
108    stream >> output.id >> output.nodes;
109    return stream.status() == QDataStream::Ok;
110}
111
112
113OsmWayStorageDisk::~OsmWayStorageDisk()
114{
115    stream.setDevice(0);
116    file->close();
117    //delete file;
118}
Note: See TracBrowser for help on using the repository browser.