summaryrefslogtreecommitdiff
path: root/src/map/tile.cpp
blob: ce56cd6134f77d7b5d789290f6051ab0c0329b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <llmr/map/tile.hpp>

#include <stdint.h>
// #include <iostream>
#include <thread>

#include "pbf.hpp"

using namespace llmr;

tile::tile(int32_t z, int32_t x, int32_t y)
    : z(z),
      x(x),
      y(y),
      loaded(false),
      data(0),
      bytes(0) {
}

void tile::setData(uint8_t *data, uint32_t bytes) {
    this->data = (uint8_t *)malloc(bytes);
    this->bytes = bytes;
    memcpy(this->data, data, bytes);
}

bool tile::parse()
{
    fprintf(stderr, "[%8zx] parsing tile\n",
        std::hash<std::thread::id>()(std::this_thread::get_id())
    );

    // try {
        pbf tile(data, bytes);
        while (tile.next()) {
            if (tile.tag == 3) { // layer
                uint32_t bytes = (uint32_t)tile.varint();
                parseLayer(tile.data, bytes);
                tile.skipBytes(bytes);
            } else {
                tile.skip();
            }
        }
    // } catch (std::exception& ex) {
    //     std::cerr << ex.what();
    //     return false;
    // }

    loaded = true;
    return true;
}

void tile::parseLayer(const uint8_t *data, uint32_t bytes) {
    pbf layer(data, bytes);
    std::string name;
    while (layer.next()) {
        if (layer.tag == 1) {
            name = layer.string();
        } else if (layer.tag == 2) {
            uint32_t bytes = (uint32_t)layer.varint();
            parseFeature(layer.data, bytes);
            layer.skipBytes(bytes);
        } else {
            layer.skip();
        }
    }
}

void tile::parseFeature(const uint8_t *data, uint32_t bytes) {
    pbf feature(data, bytes);
    while (feature.next()) {
        if (feature.tag == 1) {
            uint32_t id = feature.varint();
        } else if (feature.tag == 2) {
            const uint8_t *tag_end = feature.data + feature.varint();
            while (feature.data < tag_end) {
                uint32_t key = feature.varint();
                uint32_t value = feature.varint();
            }
        } else if (feature.tag == 3) {
            uint32_t type = feature.varint();
        } else if (feature.tag == 4) {
            uint32_t bytes = (uint32_t)feature.varint();
            loadGeometry(feature.data, bytes);
            feature.skipBytes(bytes);
        } else {
            feature.skip();
        }
    }
}

void tile::loadGeometry(const uint8_t *data, uint32_t bytes) {
     pbf geometry(data, bytes);

    uint32_t cmd = 1;
    uint32_t length = 0;
    int32_t x = 0, y = 0;

    // var lines = [];
    // var line = null;
    int32_t ox = 0, oy = 0;

    while (geometry.data < geometry.end) {
        if (!length) {
            uint32_t cmd_length = (uint32_t)geometry.varint();
            cmd = cmd_length & 0x7;
            length = cmd_length >> 3;
        }

        length--;

        if (cmd == 1 || cmd == 2) {
            x += geometry.svarint();
            y += geometry.svarint();

            if (cmd == 1) {
                // moveTo
                // fprintf(stderr, "[m %d/%d] ", x, y);
                // degenerate vertex
                lineVertex.addDegenerate();
                ox = x;
                oy = y;
            } else {
                // lineTo
                // fprintf(stderr, "[l %d/%d] ", x, y);
            }
            lineVertex.addCoordinate(x, y);
        } else if (cmd == 7) {
            // closePolygon
            // fprintf(stderr, "[c]\n");
            lineVertex.addCoordinate(ox, oy);
        } else {
            // throw new Error('unknown command ' + cmd);
            // throw std::runtime_error("unknown command");
            fprintf(stderr, "unknown command");
            exit(1);
        }
    }
}