summaryrefslogtreecommitdiff
path: root/src/mbgl/style/layer.cpp
blob: da048ad2fa57f9e9d1cb65a2f6b9052e94c283b4 (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
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/tile/tile.hpp>

#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/util/logging.hpp>

#include <mapbox/eternal.hpp>

namespace mbgl {
namespace style {

static_assert(mbgl::underlying_type(TileKind::Geometry) == mbgl::underlying_type(LayerTypeInfo::TileKind::Geometry),
              "tile kind error");
static_assert(mbgl::underlying_type(TileKind::Raster) == mbgl::underlying_type(LayerTypeInfo::TileKind::Raster),
              "tile kind error");
static_assert(mbgl::underlying_type(TileKind::RasterDEM) == mbgl::underlying_type(LayerTypeInfo::TileKind::RasterDEM),
              "tile kind error");

static LayerObserver nullObserver;

namespace {

enum class Property : uint8_t { Visibility = 0u, Minzoom, Maxzoom, Filter, SourceLayer, Source, Type };

constexpr const auto layerProperties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>(
    {{"visibility", underlying_type(Property::Visibility)},
     {"minzoom", underlying_type(Property::Minzoom)},
     {"maxzoom", underlying_type(Property::Maxzoom)},
     {"filter", underlying_type(Property::Filter)},
     {"source-layer", underlying_type(Property::SourceLayer)},
     {"source", underlying_type(Property::Source)},
     {"type", underlying_type(Property::Type)}});

const StyleProperty kUndefinedProperty{Value(), StyleProperty::Kind::Undefined};

StyleProperty getLayerPropertyDefaultValue(Property property) {
    using namespace conversion;
    switch (property) {
        case Property::Visibility:
            return makeConstantStyleProperty(makeValue(VisibilityType::Visible));
        case Property::Minzoom:
            return makeConstantStyleProperty(-std::numeric_limits<float>::infinity());
        case Property::Maxzoom:
            return makeConstantStyleProperty(std::numeric_limits<float>::infinity());
        case Property::Filter:
            return makeExpressionStyleProperty(makeValue(Filter()));
        case Property::Source:
        case Property::SourceLayer:
            return makeConstantStyleProperty(std::string());
        case Property::Type:
            return {};
    }
    assert(false);
    return {};
}

StyleProperty getLayerProperty(const Layer* layer, Property property) {
    using namespace conversion;
    switch (property) {
        case Property::Visibility:
            return makeConstantStyleProperty(layer->getVisibility());
        case Property::Minzoom:
            return makeConstantStyleProperty(layer->getMinZoom());
        case Property::Maxzoom:
            return makeConstantStyleProperty(layer->getMaxZoom());
        case Property::Filter:
            return makeExpressionStyleProperty(layer->getFilter());
        case Property::Source:
            return makeConstantStyleProperty(layer->getSourceID());
        case Property::SourceLayer:
            return makeConstantStyleProperty(layer->getSourceLayer());
        case Property::Type:
            return makeConstantStyleProperty(layer->getTypeInfo()->type);
    }
    assert(false);
    return kUndefinedProperty;
}

} // namespace

Layer::Layer(Immutable<Impl> impl)
    : baseImpl(std::move(impl)),
      observer(&nullObserver) {
}

Layer::~Layer() = default;

std::string Layer::getID() const {
    return baseImpl->id;
}

std::string Layer::getSourceID() const {
    return baseImpl->source;
}

std::string Layer::getSourceLayer() const {
    return baseImpl->sourceLayer;
}

void Layer::setSourceLayer(const std::string& sourceLayer) {
    if (getSourceLayer() == sourceLayer) return;
    auto impl_ = mutableBaseImpl();
    impl_->sourceLayer = sourceLayer;
    baseImpl = std::move(impl_);
}

void Layer::setSourceID(const std::string& sourceID) {
    if (getSourceID() == sourceID) return;
    auto impl_ = mutableBaseImpl();
    impl_->source = sourceID;
    baseImpl = std::move(impl_);
};

const Filter& Layer::getFilter() const {
    return baseImpl->filter;
}

void Layer::setFilter(const Filter& filter) {
    if (getFilter() == filter) return;
    auto impl_ = mutableBaseImpl();
    impl_->filter = filter;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}

VisibilityType Layer::getVisibility() const {
    return baseImpl->visibility;
}

void Layer::setVisibility(VisibilityType value) {
    if (value == getVisibility())
        return;
    auto impl_ = mutableBaseImpl();
    impl_->visibility = value;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}

float Layer::getMinZoom() const {
    return baseImpl->minZoom;
}

float Layer::getMaxZoom() const {
    return baseImpl->maxZoom;
}

void Layer::setMinZoom(float minZoom) {
    if (getMinZoom() == minZoom) return;
    auto impl_ = mutableBaseImpl();
    impl_->minZoom = minZoom;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}

void Layer::setMaxZoom(float maxZoom) {
    if (getMaxZoom() == maxZoom) return;
    auto impl_ = mutableBaseImpl();
    impl_->maxZoom = maxZoom;
    baseImpl = std::move(impl_);
    observer->onLayerChanged(*this);
}

Value Layer::serialize() const {
    mapbox::base::ValueObject result;
    result.emplace(std::make_pair("id", getID()));
    for (const auto& pair : layerProperties) {
        Property property = static_cast<Property>(pair.second);
        std::string key = pair.first.c_str();
        auto styleProperty = getLayerProperty(this, property);
        if (styleProperty != getLayerPropertyDefaultValue(property)) {
            auto keyValue = std::make_pair(std::move(key), std::move(styleProperty.getValue()));
            if (property == Property::Visibility) { // :-(
                result["layout"] = mapbox::base::ValueObject{std::move(keyValue)};
            } else {
                result.emplace(std::move(keyValue));
            }
        }
    }
    return result;
}

void Layer::serializeProperty(Value& out, const StyleProperty& property, const char* propertyName, bool isPaint) const {
    assert(out.getObject());
    auto& object = *(out.getObject());
    std::string propertyType = isPaint ? "paint" : "layout";
    auto it = object.find(propertyType);
    auto pair = std::make_pair(std::string(propertyName), Value{property.getValue()});
    if (it != object.end()) {
        assert(it->second.getObject());
        it->second.getObject()->emplace(std::move(pair));
    } else {
        object[propertyType] = mapbox::base::ValueObject{{std::move(pair)}};
    }
}

void Layer::setObserver(LayerObserver* observer_) {
    observer = observer_ ? observer_ : &nullObserver;
}

optional<conversion::Error> Layer::setProperty(const std::string& name, const conversion::Convertible& value) {
    using namespace conversion;
    optional<Error> error = setPropertyInternal(name, value);
    if (!error) return error; // Successfully set by the derived class implementation.
    if (name == "visibility") return setVisibility(value);
    if (name == "minzoom") {
        if (auto zoom = convert<float>(value, *error)) {
            setMinZoom(*zoom);
            return nullopt;
        }
    } else if (name == "maxzoom") {
        if (auto zoom = convert<float>(value, *error)) {
            setMaxZoom(*zoom);
            return nullopt;
        }
    } else if (name == "filter") {
        if (auto filter = convert<Filter>(value, *error)) {
            setFilter(*filter);
            return nullopt;
        }
    } else if (name == "source-layer") {
        if (auto sourceLayer = convert<std::string>(value, *error)) {
            if (getTypeInfo()->source != LayerTypeInfo::Source::Required) {
                Log::Warning(mbgl::Event::General,
                             "'source-layer' property cannot be set to"
                             "the layer %s",
                             baseImpl->id.c_str());
                return nullopt;
            }
            setSourceLayer(*sourceLayer);
            return nullopt;
        }
    } else if (name == "source") {
        if (auto sourceID = convert<std::string>(value, *error)) {
            if (getTypeInfo()->source != LayerTypeInfo::Source::Required) {
                Log::Warning(mbgl::Event::General,
                             "'source' property cannot be set to"
                             "the layer %s",
                             baseImpl->id.c_str());
                return nullopt;
            }
            setSourceID(*sourceID);
            return nullopt;
        }
    }
    return error;
}

StyleProperty Layer::getProperty(const std::string& name) const {
    const auto it = layerProperties.find(name.c_str());
    if (it != layerProperties.end()) {
        return getLayerProperty(this, static_cast<Property>(it->second));
    }
    return getPropertyInternal(name);
}

// static
StyleProperty Layer::getPropertyDefaultValue(const std::string& name) {
    const auto it = layerProperties.find(name.c_str());
    if (it != layerProperties.end()) {
        return getLayerPropertyDefaultValue(static_cast<Property>(it->second));
    }
    return {};
}

optional<conversion::Error> Layer::setVisibility(const conversion::Convertible& value) {
    using namespace conversion;

    if (isUndefined(value)) {
        setVisibility(VisibilityType::Visible);
        return nullopt;
    }

    Error error;
    optional<VisibilityType> visibility = convert<VisibilityType>(value, error);
    if (!visibility) {
        return error;
    }

    setVisibility(*visibility);
    return nullopt;
}

const LayerTypeInfo* Layer::getTypeInfo() const noexcept {
    return baseImpl->getTypeInfo();
}

} // namespace style
} // namespace mbgl