summaryrefslogtreecommitdiff
path: root/src/map/tile.cpp
blob: e8a0c1979a13d8af671d9c54014b057f62a4527d (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <llmr/map/tile.hpp>

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

#include <llmr/util/pbf.hpp>
#include <llmr/util/string.hpp>
#include <llmr/geometry/geometry.hpp>
#include <llmr/renderer/fill_bucket.hpp>
#include <llmr/style/style.hpp>
#include <llmr/style/value.hpp>
#include <cmath>

using namespace llmr;

Tile::ID Tile::parent(const ID& id, int32_t z) {
    assert(z < id.z);
    ID pos(id);
    while (pos.z > z) {
        pos.z--;
        pos.x = floor(pos.x / 2);
        pos.y = floor(pos.y / 2);
    }
    return pos;
}


std::forward_list<Tile::ID> Tile::children(const ID& id, int32_t z) {
    assert(z > id.z);
    int32_t factor = pow(2, z - id.z);

    std::forward_list<ID> children;
    for (int32_t y = id.y * factor, y_max = (id.y + 1) * factor; y < y_max; y++) {
        for (int32_t x = id.x * factor, x_max = (id.x + 1) * factor; x < x_max; x++) {
            children.emplace_front(x, y, z);
        }
    }
    return children;
}


Tile::Tile(ID id, const Style& style)
    : id(id),
      state(initial),
      lineVertex(std::make_shared<linevertexbuffer>()),
      debugFontVertex(std::make_shared<debug_font_buffer>()),
      fillBuffer(std::make_shared<FillBuffer>()),
      data(0),
      bytes(0),
      style(style) {

    // Initialize tile debug coordinates
    char coord[32];
    snprintf(coord, sizeof(coord), "%d/%d/%d", id.z, id.x, id.y);
    debugFontVertex->addText(coord, 50, 200, 5);
}

Tile::~Tile() {
    std::lock_guard<std::mutex> lock(mtx);

    // fprintf(stderr, "[%p] deleting tile %d/%d/%d\n", this, id.z, id.x, id.y);
    if (this->data) {
        free(this->data);
        this->data = NULL;
    }
}

const std::string Tile::toString() const {
    return util::sprintf("[tile %d/%d/%d]", id.z, id.x, id.y);
}


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

void Tile::cancel() {
    // TODO: thread safety
    if (state != obsolete) {
        state = obsolete;
    } else {
        assert((!"logic error? multiple cancelleations"));
    }
}

bool Tile::parse() {
    std::lock_guard<std::mutex> lock(mtx);

    if (state == obsolete) {
        return false;
    }

    try {
        pbf tile(data, bytes);
        // fprintf(stderr, "[%p] parsing tile [%d/%d/%d]...\n", this, z, x, y);

        parseLayers(tile, style.layers);
    } catch (const std::exception& ex) {
        fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
        cancel();
        return false;
    }

    if (state == obsolete) {
        return false;
    } else {
        state = ready;
    }

    return true;
}

void Tile::parseLayers(const pbf& data, const std::vector<LayerDescription>& layers) {
    for (const LayerDescription& layer_desc : layers) {
        if (layer_desc.child_layer.size()) {
            // This is a layer group.
            // TODO: create framebuffer
            parseLayers(data, layer_desc.child_layer);
            // TODO: render framebuffer on previous framebuffer
        } else {
            // This is a singular layer. Check if this bucket already exists. If not,
            // parse this bucket.
            auto bucket_it = buckets.find(layer_desc.bucket_name);
            if (bucket_it == buckets.end()) {
                auto bucket_it = style.buckets.find(layer_desc.bucket_name);
                if (bucket_it != style.buckets.end()) {
                    // Only create the new bucket if we have an actual specification
                    // for it.
                    std::shared_ptr<Bucket> bucket = createBucket(data, bucket_it->second);
                    if (bucket) {
                        // Bucket creation might fail because the data tile may not
                        // contain any data that falls into this bucket.
                        buckets[layer_desc.bucket_name] = bucket;
                    }
                } else {
                    // There is no proper specification for this bucket, even though
                    // it is referenced in the stylesheet.
                    fprintf(stderr, "Stylesheet specifies bucket %s, but it is not defined\n", layer_desc.bucket_name.c_str());
                }
            }
        }
    }
}

std::shared_ptr<Bucket> Tile::createBucket(const pbf& data, const BucketDescription& bucket_desc) {
    pbf tile(data);
    // TODO: remember data locations in tiles for faster parsing so that we don't
    // have to go through the entire vector tile all the time.
    while (tile.next()) {
        if (tile.tag == 3) { // layer
            pbf layer = tile.message();
            while (layer.next()) {
                if (layer.tag == 1) {
                    std::string name = layer.string();
                    if (name == bucket_desc.source_layer) {
                        if (bucket_desc.type == BucketType::Fill) {
                            return createFillBucket(layer, bucket_desc);
                        } else {
                            // TODO: create other bucket types.
                        }
                    }
                } else {
                    layer.skip();
                }
            }
        } else {
            tile.skip();
        }
    }

    return NULL;
}

enum geom_type {
    Unknown = 0,
    Point = 1,
    LineString = 2,
    Polygon = 3
};

std::shared_ptr<Bucket> Tile::createFillBucket(const pbf data, const BucketDescription& bucket_desc) {
    pbf layer;
    std::shared_ptr<FillBucket> bucket = std::make_shared<FillBucket>(fillBuffer);

    int32_t key = -1;
    std::set<uint32_t> values;

    // If we filter further by field/value, parse the key/value indices first
    // because protobuf doesn't mandate a particular key order.
    if (bucket_desc.source_field.size()) {
        uint32_t key_id = 0;
        uint32_t value_id = 0;

        // Find out what key/value IDs we need.
        layer = data;
        while (layer.next()) {
            if (layer.tag == 3) { // keys
                if (layer.string() == bucket_desc.source_field) {
                    // We found the key
                    key = key_id;
                }
                key_id++;
            } else if (layer.tag == 4) { // values
                Value value = parseValue(layer.message());
                if (std::find(bucket_desc.source_value.begin(), bucket_desc.source_value.end(), value) != bucket_desc.source_value.end()) {
                    values.insert(value_id);
                }
                value_id++;
            } else {
                layer.skip();
            }
        }

        if (key < 0 || values.empty()) {
            // There are no valid values that we could possibly find. Abort early.
            return bucket;
        }
    }

    // Now parse the features and optionally filter by key/value IDs.
    layer = data;
    while (layer.next()) {
        if (layer.tag == 2) { // feature
            pbf feature = layer.message();
            pbf geometry;
            geom_type type = Unknown;
            bool skip = false;

            while (!skip && feature.next()) {
                if (feature.tag == 2) { // tags
                    if (key < 0) {
                        // We want to parse the entire layer anyway
                        feature.skip();
                    } else {
                        // We only want to parse some features.
                        skip = true;
                        // tags are packed varints. They should have an even length.
                        pbf tags = feature.message();
                        while (tags) {
                            uint32_t tag_key = tags.varint();
                            if (tags) {
                                uint32_t tag_val = tags.varint();
                                // Now check if the tag_key/tag_val pair is included
                                // in the set of key/values that we are looking for.
                                if (key == tag_key && values.find(tag_val) != values.end()) {
                                    skip = false;
                                    break;
                                }
                            } else {
                                // This should not happen; otherwise the vector tile
                                // is invalid.
                                throw std::runtime_error("uneven number of feature tag ids");
                            }
                        }
                    }
                } else if (feature.tag == 3) { // geometry type
                    type = (geom_type)feature.varint();
                    if (type != Polygon) {
                        skip = true;
                        break;
                    }
                } else if (feature.tag == 4) { // geometry
                    geometry = feature.message();
                } else {
                    feature.skip();
                }
            }

            if (!skip && geometry) {
                bucket->addGeometry(geometry);
            }
        } else {
            layer.skip();
        }
    }

    return bucket;
}

// void Tile::addLineGeometry(pbf& geom) {
//     Geometry geometry(geom);

//     Geometry::command cmd;
//     int32_t x, y;
//     while ((cmd = geometry.next(x, y)) != Geometry::end) {
//         if (cmd == Geometry::move_to) {
//             lineVertex.addDegenerate();
//         }
//         lineVertex.addCoordinate(x, y);
//     }
// }