summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/sources/render_geojson_source.cpp
blob: e451dab6d3a57dd2cffdfce2d93d0cacc9bd7faa (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
#include <mbgl/renderer/sources/render_geojson_source.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/renderer/tile_parameters.hpp>

#include <mapbox/eternal.hpp>

namespace mbgl {

using namespace style;

namespace {

template<typename T, typename C>
optional<T> getProperty(const C& cont, const typename C::key_type& name) {
    const auto it = cont.find(name);
    if (it == cont.end() || !(it->second.template is<T>())) {
        return nullopt;
    }
    return it->second.template get<T>();
}

using FeatureExtensionGetterPtr = FeatureExtensionValue (*)(std::shared_ptr<style::GeoJSONData>,
                                                            std::uint32_t,
                                                            const optional<std::map<std::string, Value>>&);

FeatureExtensionValue getChildren(std::shared_ptr<style::GeoJSONData> clusterData,
                                  std::uint32_t clusterID,
                                  const optional<std::map<std::string, Value>>&) {
    return clusterData->getChildren(clusterID);
}

FeatureExtensionValue getLeaves(std::shared_ptr<style::GeoJSONData> clusterData,
                                std::uint32_t clusterID,
                                const optional<std::map<std::string, Value>>& args) {
    if (args) {
        const auto limit = getProperty<uint64_t>(*args, "limit");
        const auto offset = getProperty<uint64_t>(*args, "offset");
        // Offset cannot be set without limit.
        if (limit) {
            if (offset) {
                return clusterData->getLeaves(clusterID,
                                              static_cast<std::uint32_t>(*limit),
                                              static_cast<std::uint32_t>(*offset));
            }
            return clusterData->getLeaves(clusterID, static_cast<std::uint32_t>(*limit));
        }
    }

    return clusterData->getLeaves(clusterID);
}

FeatureExtensionValue getClusterExpansionZoom(std::shared_ptr<style::GeoJSONData> clusterData,
                                              std::uint32_t clusterID,
                                              const optional<std::map<std::string, Value>>&) {
    return Value{static_cast<uint64_t>(clusterData->getClusterExpansionZoom(clusterID))};
}

MAPBOX_ETERNAL_CONSTEXPR const auto extensionGetters = mapbox::eternal::hash_map<mapbox::eternal::string, FeatureExtensionGetterPtr>({
    {"children", &getChildren},
    {"leaves", &getLeaves},
    {"expansion-zoom", &getClusterExpansionZoom}
});

} // namespace

RenderGeoJSONSource::RenderGeoJSONSource(Immutable<style::GeoJSONSource::Impl> impl_)
    : RenderTileSource(std::move(impl_)) {
}

RenderGeoJSONSource::~RenderGeoJSONSource() = default;

const style::GeoJSONSource::Impl& RenderGeoJSONSource::impl() const {
    return static_cast<const style::GeoJSONSource::Impl&>(*baseImpl);
}

void RenderGeoJSONSource::update(Immutable<style::Source::Impl> baseImpl_,
                                 const std::vector<Immutable<LayerProperties>>& layers,
                                 const bool needsRendering,
                                 const bool needsRelayout,
                                 const TileParameters& parameters) {
    std::swap(baseImpl, baseImpl_);

    enabled = needsRendering;

    auto data_ = impl().getData().lock();

    if (data.lock() != data_) {
        data = data_;
        tilePyramid.reduceMemoryUse();

        if (data_) {
            const uint8_t maxZ = impl().getZoomRange().max;
            for (const auto& pair : tilePyramid.getTiles()) {
                if (pair.first.canonical.z <= maxZ) {
                    static_cast<GeoJSONTile*>(pair.second.get())->updateData(data_->getTile(pair.first.canonical));
                }
            }
        }
    }

    if (!data_) {
        tilePyramid.clearAll();
        return;
    }

    tilePyramid.update(layers,
                       needsRendering,
                       needsRelayout,
                       parameters,
                       SourceType::GeoJSON,
                       util::tileSize,
                       impl().getZoomRange(),
                       optional<LatLngBounds>{},
                       [&, data_] (const OverscaledTileID& tileID) {
                           return std::make_unique<GeoJSONTile>(tileID, impl().id, parameters, data_->getTile(tileID.canonical));
                       });
}

mapbox::util::variant<Value, FeatureCollection>
RenderGeoJSONSource::queryFeatureExtensions(const Feature& feature,
                                            const std::string& extension,
                                            const std::string& extensionField,
                                            const optional<std::map<std::string, Value>>& args) const {
    if (extension != "supercluster") {
        return {};
    }

    const auto extensionIt = extensionGetters.find(extensionField.c_str());
    if (extensionIt == extensionGetters.end()) {
        return {};
    }

    const auto clusterID = getProperty<uint64_t>(feature.properties, "cluster_id");
    if (!clusterID) {
        return {};
    }

    auto jsonData = data.lock();
    if (!jsonData) {
        return {};
    }

    return extensionIt->second(std::move(jsonData), static_cast<std::uint32_t>(*clusterID), args);
}

} // namespace mbgl