summaryrefslogtreecommitdiff
path: root/src/map/vector_tile_data.cpp
blob: d2f73269c02f718feb458be5397da85f81e02e9a (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
#include <mbgl/map/vector_tile_data.hpp>
#include <mbgl/map/tile_parser.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>

using namespace mbgl;

VectorTileData::VectorTileData(Tile::ID const& id_, Map &map_,
                               GlyphAtlas& glyphAtlas_, GlyphStore& glyphStore_,
                               SpriteAtlas& spriteAtlas_,
                               const util::ptr<SourceInfo> &source_)
    : TileData(id_, map_, source_),
      glyphAtlas(glyphAtlas_),
      glyphStore(glyphStore_),
      spriteAtlas(spriteAtlas_),
      depth(id.z >= source->max_zoom ? map.getMaxZoom() - id.z : 1) {
}

VectorTileData::~VectorTileData() {
    glyphAtlas.removeGlyphs(id.to_uint64());
}


void VectorTileData::parse() {
    if (state != State::loaded) {
        return;
    }

    try {
        // Parsing creates state that is encapsulated in TileParser. While parsing,
        // the TileParser object writes results into this objects. All other state
        // is going to be discarded afterwards.
        TileParser parser(data, *this, map.getStyle(),
                          glyphAtlas, glyphStore,
                          spriteAtlas, map.getSprite());
        parser.parse();
    } catch (const std::exception& ex) {
#if defined(DEBUG)
        fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
#endif
        cancel();
        return;
    }

    if (state != State::obsolete) {
        state = State::parsed;
    }
}

void VectorTileData::render(Painter &painter, util::ptr<StyleLayer> layer_desc, const mat4 &matrix) {
    if (state == State::parsed && layer_desc->bucket) {
        auto databucket_it = buckets.find(layer_desc->bucket->name);
        if (databucket_it != buckets.end()) {
            assert(databucket_it->second);
            databucket_it->second->render(painter, layer_desc, id, matrix);
        }
    }
}

bool VectorTileData::hasData(StyleLayer const& layer_desc) const {
    if (state == State::parsed && layer_desc.bucket) {
        auto databucket_it = buckets.find(layer_desc.bucket->name);
        if (databucket_it != buckets.end()) {
            assert(databucket_it->second);
            return databucket_it->second->hasData();
        }
    }
    return false;
}