summaryrefslogtreecommitdiff
path: root/include/mbgl/style/function/convert.hpp
blob: ca021eadf688da5ed36b85f343333d5f9770ead5 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#pragma once


#include <mbgl/util/enum.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/expression/array_assertion.hpp>
#include <mbgl/style/expression/case.hpp>
#include <mbgl/style/expression/coalesce.hpp>
#include <mbgl/style/expression/compound_expression.hpp>
#include <mbgl/style/expression/curve.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/literal.hpp>
#include <mbgl/style/expression/match.hpp>

#include <mbgl/style/function/exponential_stops.hpp>
#include <mbgl/style/function/interval_stops.hpp>
#include <mbgl/style/function/categorical_stops.hpp>
#include <mbgl/style/function/composite_exponential_stops.hpp>
#include <mbgl/style/function/composite_interval_stops.hpp>
#include <mbgl/style/function/composite_categorical_stops.hpp>
#include <mbgl/style/function/identity_stops.hpp>

#include <string>


namespace mbgl {
namespace style {
namespace expression {

namespace detail {

class ErrorExpression : public Expression {
public:
    ErrorExpression(std::string message_) : Expression(type::Error), message(std::move(message_)) {}
    bool isFeatureConstant() const override { return true; }
    bool isZoomConstant() const override { return true; }

    EvaluationResult evaluate(const EvaluationParameters&) const override {
        return EvaluationError{message};
    }

private:
    std::string message;
};

}

// Create expressions representing 'classic' (i.e. stop-based) style functions

struct Convert {
    // TODO: Organize. Where should each of these actually go?

    template <typename T>
    static std::unique_ptr<Literal> makeLiteral(const T& value) {
        return std::make_unique<Literal>(Value(toExpressionValue(value)));
    }
    
    static std::unique_ptr<Expression> makeGet(const std::string& type, const std::string& property, ParsingContext ctx) {
        std::vector<std::unique_ptr<Expression>> getArgs;
        getArgs.push_back(makeLiteral(property));
        ParseResult get = CompoundExpressions::create("get", std::move(getArgs), ctx);

        std::vector<std::unique_ptr<Expression>> assertionArgs;
        assertionArgs.push_back(std::move(*get));
        
        return std::move(*(CompoundExpressions::create(type, std::move(assertionArgs), ctx)));
    }
    
    static std::unique_ptr<Expression> makeZoom(ParsingContext ctx) {
        return std::move(*(CompoundExpressions::create("zoom", std::vector<std::unique_ptr<Expression>>(), ctx)));
    }
    
    static std::unique_ptr<Expression> makeError(std::string message) {
        return std::make_unique<detail::ErrorExpression>(message);
    }


    template <typename T>
    static ParseResult makeCoalesceToDefault(std::unique_ptr<Expression> main, optional<T> defaultValue) {
        if (!defaultValue) {
            return ParseResult(std::move(main));
        }
        
        Coalesce::Args args;
        args.push_back(std::move(main));
        args.push_back(makeLiteral(*defaultValue));
        return ParseResult(std::make_unique<Coalesce>(valueTypeToExpressionType<T>(), std::move(args)));
    }
    
    template <typename T>
    static std::map<float, std::unique_ptr<Expression>> convertStops(const std::map<float, T>& stops) {
        std::map<float, std::unique_ptr<Expression>> convertedStops;
        for(const std::pair<float, T>& stop : stops) {
            convertedStops.emplace(
                stop.first,
                makeLiteral(stop.second)
            );
        }
        return convertedStops;
    }
    
    template <typename T>
    static ParseResult makeExponentialCurve(std::unique_ptr<Expression> input,
                                                            std::map<float, std::unique_ptr<Expression>> convertedStops,
                                                            float base,
                                                            optional<T> defaultValue)
    {
        ParseResult curve = ParseResult(std::make_unique<Curve<ExponentialInterpolator<T>>>(
            valueTypeToExpressionType<T>(),
            ExponentialInterpolator<T>(base),
            std::move(input),
            std::move(convertedStops)
        ));
        assert(curve);
        return makeCoalesceToDefault(std::move(*curve), defaultValue);
    }
    
    template <typename T>
    static ParseResult makeStepCurve(std::unique_ptr<Expression> input,
                                     const std::map<float, T>& stops,
                                     optional<T> defaultValue)
    {
        std::map<float, std::unique_ptr<Expression>> convertedStops = convertStops(stops);
        auto curve = std::make_unique<Curve<StepInterpolator>>(valueTypeToExpressionType<T>(),
                                                               StepInterpolator(),
                                                               std::move(input),
                                                               std::move(convertedStops));
        return makeCoalesceToDefault(std::move(curve), defaultValue);
    }
    
    template <typename Key, typename T>
    static ParseResult makeMatch(std::unique_ptr<Expression> input,
                                 const std::map<CategoricalValue, T>& stops) {
        // match expression
        typename Match<Key>::Cases cases;
        for(const std::pair<CategoricalValue, T>& stop : stops) {
            assert(stop.first.template is<Key>());
            Key key = stop.first.template get<Key>();
            cases.emplace(
                std::move(key),
                makeLiteral(stop.second)
            );
        }
        
        return ParseResult(std::make_unique<Match<Key>>(valueTypeToExpressionType<T>(),
                                            std::move(input),
                                            std::move(cases),
                                            makeError("No matching label")));
    }
    
    template <typename T>
    static ParseResult makeCase(std::unique_ptr<Expression> input,
                                const std::map<CategoricalValue, T>& stops) {
        // case expression
        std::vector<typename Case::Branch> cases;
        
        auto it = stops.find(true);
        std::unique_ptr<Expression> true_case = it == stops.end() ?
            makeError("No matching label") :
            makeLiteral(it->second);

        it = stops.find(false);
        std::unique_ptr<Expression> false_case = it == stops.end() ?
            makeError("No matching label") :
            makeLiteral(it->second);

        cases.push_back(std::make_pair(std::move(input), std::move(true_case)));
        return ParseResult(std::make_unique<Case>(valueTypeToExpressionType<T>(), std::move(cases), std::move(false_case)));
    }
    
    template <typename T>
    static ParseResult convertCategoricalStops(std::map<CategoricalValue, T> stops, const std::string& property) {
        assert(stops.size() > 0);

        std::vector<ParsingError> errors;
        ParsingContext ctx(errors);

        const CategoricalValue& firstKey = stops.begin()->first;
        return firstKey.match(
            [&](bool) {
                return makeCase(makeGet("boolean", property, ctx), stops);
            },
            [&](const std::string&) {
                return makeMatch<std::string>(makeGet("string", property, ctx), stops);
            },
            [&](int64_t) {
                return makeMatch<int64_t>(makeGet("number", property, ctx), stops);
            }
        );
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const ExponentialStops<T>& stops)
    {
        std::vector<ParsingError> errors;
        ParseResult e = makeExponentialCurve(makeZoom(ParsingContext(errors)),
                                             convertStops(stops.stops),
                                             stops.base,
                                             optional<T>());
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const IntervalStops<T>& stops)
    {
        std::vector<ParsingError> errors;
        ParseResult e = makeStepCurve(makeZoom(ParsingContext(errors)), stops.stops, optional<T>());
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const ExponentialStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;
        ParseResult e = makeExponentialCurve(makeGet("number", property, ParsingContext(errors)),
                                             convertStops(stops.stops),
                                             stops.base,
                                             defaultValue);
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const IntervalStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;
        ParseResult e = makeStepCurve(makeGet("number", property, ParsingContext(errors)), stops.stops, defaultValue);
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const CategoricalStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        ParseResult expr = convertCategoricalStops(stops.stops, property);
        assert(expr);
        ParseResult e = makeCoalesceToDefault(std::move(*expr), defaultValue);
        assert(e);
        return std::move(*e);
    }

    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const CompositeExponentialStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;
        std::map<float, std::unique_ptr<Expression>> outerStops;
        for (const std::pair<float, std::map<float, T>>& stop : stops.stops) {
            std::unique_ptr<Expression> get = makeGet("number", property, ParsingContext(errors));
            ParseResult innerCurve = makeExponentialCurve(std::move(get),
                                                          convertStops(stop.second),
                                                          stops.base,
                                                          defaultValue);
            assert(innerCurve);
            outerStops.emplace(stop.first, std::move(*innerCurve));
        }
        ParseResult outerCurve = makeExponentialCurve(makeZoom(ParsingContext(errors)),
                                                      std::move(outerStops),
                                                      1.0f,
                                                      defaultValue);
        assert(outerCurve);
        ParseResult e = makeCoalesceToDefault(std::move(*outerCurve), defaultValue);
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const CompositeIntervalStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;
        std::map<float, std::unique_ptr<Expression>> outerStops;
        for (const std::pair<float, std::map<float, T>>& stop : stops.stops) {
            std::unique_ptr<Expression> get = makeGet("number", property, ParsingContext(errors));
            ParseResult innerCurve = makeStepCurve(std::move(get), stop.second, defaultValue);
            assert(innerCurve);
            outerStops.emplace(stop.first, std::move(*innerCurve));
        }
        ParseResult outerCurve = makeExponentialCurve(makeZoom(ParsingContext(errors)),
                                                      std::move(outerStops),
                                                      1.0f,
                                                      defaultValue);
        assert(outerCurve);
        ParseResult e = makeCoalesceToDefault(std::move(*outerCurve), defaultValue);
        assert(e);
        return std::move(*e);
    }
    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const CompositeCategoricalStops<T>& stops,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;
        std::map<float, std::unique_ptr<Expression>> outerStops;
        for (const std::pair<float, std::map<CategoricalValue, T>>& stop : stops.stops) {
            ParseResult innerCurve = convertCategoricalStops(stop.second, property);
            assert(innerCurve);
            outerStops.emplace(stop.first, std::move(*innerCurve));
        }
        ParseResult outerCurve = makeExponentialCurve(makeZoom(ParsingContext(errors)),
                                                      std::move(outerStops),
                                                      1.0f,
                                                      defaultValue);
        assert(outerCurve);
        ParseResult e = makeCoalesceToDefault(std::move(*outerCurve), defaultValue);
        assert(e);
        return std::move(*e);
    }

    
    template <typename T>
    static std::unique_ptr<Expression> toExpression(const std::string& property,
                                                  const IdentityStops<T>&,
                                                  optional<T> defaultValue)
    {
        std::vector<ParsingError> errors;

        std::unique_ptr<Expression> input = valueTypeToExpressionType<T>().match(
            [&] (const type::StringType&) {
                return makeGet("string", property, ParsingContext(errors));
            },
            [&] (const type::NumberType&) {
                return makeGet("number", property, ParsingContext(errors));
            },
            [&] (const type::BooleanType&) {
                return makeGet("boolean", property, ParsingContext(errors));
            },
            [&] (const type::ColorType&) {
                std::vector<std::unique_ptr<Expression>> args;
                args.push_back(makeGet("string", property, ParsingContext(errors)));
                ParseResult color = CompoundExpressions::create("parse_color", std::move(args), ParsingContext(errors));
                assert(color);
                return std::move(*color);
            },
            [&] (const type::Array& arr) {
                std::vector<std::unique_ptr<Expression>> getArgs;
                getArgs.push_back(makeLiteral(property));
                ParseResult get = CompoundExpressions::create("get", std::move(getArgs), ParsingContext(errors));
                return std::make_unique<ArrayAssertion>(arr, std::move(*get));
            },
            [&] (const auto&) -> std::unique_ptr<Expression> {
                return makeLiteral(Null);
            }
        );
        
        ParseResult e = makeCoalesceToDefault(std::move(input), defaultValue);
        assert(e);
        return std::move(*e);
    }
};

} // namespace expression
} // namespace style
} // namespace mbgl