summaryrefslogtreecommitdiff
path: root/src/mbgl/style/tile_source.cpp
blob: a4deb17c4f6d9bffe31820075a69542a5dcf3a77 (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
#include <mbgl/style/tile_source.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/parser.hpp>
#include <mbgl/util/tileset.hpp>
#include <mbgl/storage/file_source.hpp>

namespace mbgl {
namespace style {

TileSource::TileSource(SourceType type_,
                       std::string id_,
                       std::string url_,
                       uint16_t tileSize_,
                       std::unique_ptr<Tileset>&& tileset_)
    : Source(type_, std::move(id_)),
      tileSize(tileSize_),
      url(std::move(url_)),
      tileset(std::move(tileset_)) {
}

TileSource::~TileSource() = default;

Range<uint8_t> TileSource::getZoomRange() {
    return tileset->zoomRange;
}

void TileSource::load(FileSource& fileSource) {
    if (url.empty()) {
        // If the URL is empty, the TileJSON was specified inline in the stylesheet.
        loaded = true;
        return;
    }

    if (req) {
        return;
    }

    // URL may either be a TileJSON file, or a GeoJSON file.
    req = fileSource.request(Resource::source(url), [this](Response res) {
        if (res.error) {
            observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message)));
        } else if (res.notModified) {
            return;
        } else if (res.noContent) {
            observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty TileJSON")));
        } else {
            std::unique_ptr<Tileset> newTileset;

            // Create a new copy of the Tileset object that holds the base values we've parsed
            // from the stylesheet. Then merge in the values parsed from the TileJSON we retrieved
            // via the URL.
            try {
                newTileset = style::parseTileJSON(*res.data, url, type, tileSize);
            } catch (...) {
                observer->onSourceError(*this, std::current_exception());
                return;
            }

            // Check whether previous information specifies different tile
            if (tileset && tileset->tiles != newTileset->tiles) {
                // Tile URLs changed: force tiles to be reloaded.
                invalidateTiles();

                // Tile size changed: We need to recalculate the tiles we need to load because we
                // might have to load tiles for a different zoom level
                // This is done automatically when we trigger the onSourceLoaded observer below.

                // Min/Max zoom changed: We need to recalculate what tiles to load, if we have tiles
                // loaded that are outside the new zoom range
                // This is done automatically when we trigger the onSourceLoaded observer below.

                // Attribution changed: We need to notify the embedding application that this
                // changed. See https://github.com/mapbox/mapbox-gl-native/issues/2723
                // This is not yet implemented.

                // Center/bounds changed: We're not using these values currently
            }

            tileset = std::move(newTileset);
            loaded = true;
            observer->onSourceLoaded(*this);
        }
    });
}

} // namespace style
} // namespace mbgl