summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/layer.cpp
blob: d36ca494da05efd4e0c518cb19faf48077742d8b (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
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/circle_layer.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/fill_extrusion_layer.hpp>
#include <mbgl/style/layers/heatmap_layer.hpp>
#include <mbgl/style/layers/hillshade_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>

namespace mbgl {
namespace style {
namespace conversion {

optional<Error> setPaintProperties(Layer& layer, const Convertible& value) {
    auto paintValue = objectMember(value, "paint");
    if (!paintValue) {
        return nullopt;
    }
    if (!isObject(*paintValue)) {
        return { { "paint must be an object" } };
    }
    return eachMember(*paintValue, [&] (const std::string& k, const Convertible& v) {
        return layer.setPaintProperty(k, v);
    });
}

template <class LayerType>
optional<std::unique_ptr<Layer>> convertVectorLayer(const std::string& id, const Convertible& value, Error& error) {
    auto sourceValue = objectMember(value, "source");
    if (!sourceValue) {
        error.message = "layer must have a source";
        return nullopt;
    }

    optional<std::string> source = toString(*sourceValue);
    if (!source) {
        error.message = "layer source must be a string";
        return nullopt;
    }

    std::unique_ptr<LayerType> layer = std::make_unique<LayerType>(id, *source);

    auto sourceLayerValue = objectMember(value, "source-layer");
    if (sourceLayerValue) {
        optional<std::string> sourceLayer = toString(*sourceLayerValue);
        if (!sourceLayer) {
            error.message = "layer source-layer must be a string";
            return nullopt;
        }
        layer->setSourceLayer(*sourceLayer);
    }

    auto filterValue = objectMember(value, "filter");
    if (filterValue) {
        optional<Filter> filter = convert<Filter>(*filterValue, error);
        if (!filter) {
            return nullopt;
        }
        layer->setFilter(*filter);
    }

    return { std::move(layer) };
}

static optional<std::unique_ptr<Layer>> convertRasterLayer(const std::string& id, const Convertible& value, Error& error) {
    auto sourceValue = objectMember(value, "source");
    if (!sourceValue) {
        error.message = "layer must have a source";
        return nullopt;
    }

    optional<std::string> source = toString(*sourceValue);
    if (!source) {
        error.message = "layer source must be a string";
        return nullopt;
    }

    return { std::make_unique<RasterLayer>(id, *source) };
}

static optional<std::unique_ptr<Layer>> convertHillshadeLayer(const std::string& id, const Convertible& value, Error& error) {
    auto sourceValue = objectMember(value, "source");
    if (!sourceValue) {
        error.message = "layer must have a source";
        return nullopt;
    }

    optional<std::string> source = toString(*sourceValue);
    if (!source) {
        error.message = "layer source must be a string";
        return nullopt;
    }

    return { std::make_unique<HillshadeLayer>(id, *source) };
}


static optional<std::unique_ptr<Layer>> convertBackgroundLayer(const std::string& id, const Convertible&, Error&) {
    return { std::make_unique<BackgroundLayer>(id) };
}

optional<std::unique_ptr<Layer>> Converter<std::unique_ptr<Layer>>::operator()(const Convertible& value, Error& error) const {
    if (!isObject(value)) {
        error.message = "layer must be an object";
        return nullopt;
    }

    auto idValue = objectMember(value, "id");
    if (!idValue) {
        error.message = "layer must have an id";
        return nullopt;
    }

    optional<std::string> id = toString(*idValue);
    if (!id) {
        error.message = "layer id must be a string";
        return nullopt;
    }

    auto typeValue = objectMember(value, "type");
    if (!typeValue) {
        error.message = "layer must have a type";
        return nullopt;
    }

    optional<std::string> type = toString(*typeValue);
    if (!type) {
        error.message = "layer type must be a string";
        return nullopt;
    }

    optional<std::unique_ptr<Layer>> converted;

    if (*type == "fill") {
        converted = convertVectorLayer<FillLayer>(*id, value, error);
    } else if (*type == "fill-extrusion") {
        converted = convertVectorLayer<FillExtrusionLayer>(*id, value, error);
    } else if (*type == "line") {
        converted = convertVectorLayer<LineLayer>(*id, value, error);
    } else if (*type == "circle") {
        converted = convertVectorLayer<CircleLayer>(*id, value, error);
    } else if (*type == "symbol") {
        converted = convertVectorLayer<SymbolLayer>(*id, value, error);
    } else if (*type == "raster") {
        converted = convertRasterLayer(*id, value, error);
    } else if (*type == "heatmap") {
        converted = convertVectorLayer<HeatmapLayer>(*id, value, error);
    } else if (*type == "hillshade") {
        converted = convertHillshadeLayer(*id, value, error);
    } else if (*type == "background") {
        converted = convertBackgroundLayer(*id, value, error);
    } else {
        error.message = "invalid layer type";
        return nullopt;
    }

    if (!converted) {
        return converted;
    }

    std::unique_ptr<Layer> layer = std::move(*converted);

    auto minzoomValue = objectMember(value, "minzoom");
    if (minzoomValue) {
        optional<float> minzoom = toNumber(*minzoomValue);
        if (!minzoom) {
            error.message = "minzoom must be numeric";
            return nullopt;
        }
        layer->setMinZoom(*minzoom);
    }

    auto maxzoomValue = objectMember(value, "maxzoom");
    if (maxzoomValue) {
        optional<float> maxzoom = toNumber(*maxzoomValue);
        if (!maxzoom) {
            error.message = "maxzoom must be numeric";
            return nullopt;
        }
        layer->setMaxZoom(*maxzoom);
    }

    auto layoutValue = objectMember(value, "layout");
    if (layoutValue) {
        if (!isObject(*layoutValue)) {
            error.message = "layout must be an object";
            return nullopt;
        }
        optional<Error> error_ = eachMember(*layoutValue, [&] (const std::string& k, const Convertible& v) {
            return layer->setLayoutProperty(k, v);
        });
        if (error_) {
            error = *error_;
            return nullopt;
        }
    }

    optional<Error> error_ = setPaintProperties(*layer, value);
    if (error_) {
        error = *error_;
        return nullopt;
    }

    return std::move(layer);
}

} // namespace conversion
} // namespace style
} // namespace mbgl