summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/geojson_options.cpp
blob: 08553e34bbe046c79f7b7cf93d234b7452a16340 (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
#include <mbgl/style/conversion/geojson_options.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/expression/dsl.hpp>

#include <sstream>

namespace mbgl {
namespace style {
namespace conversion {

optional<GeoJSONOptions> Converter<GeoJSONOptions>::operator()(const Convertible& value,
                                                               Error& error) const {
    GeoJSONOptions options;

    const auto minzoomValue = objectMember(value, "minzoom");
    if (minzoomValue) {
        if (toNumber(*minzoomValue)) {
            options.minzoom = static_cast<uint8_t>(*toNumber(*minzoomValue));
        } else {
            error.message = "GeoJSON source minzoom value must be a number";
            return nullopt;
        }
    }

    const auto maxzoomValue = objectMember(value, "maxzoom");
    if (maxzoomValue) {
        if (toNumber(*maxzoomValue)) {
            options.maxzoom = static_cast<uint8_t>(*toNumber(*maxzoomValue));
        } else {
            error.message = "GeoJSON source maxzoom value must be a number";
            return nullopt;
        }
    }

    const auto bufferValue = objectMember(value, "buffer");
    if (bufferValue) {
        if (toNumber(*bufferValue)) {
            options.buffer = static_cast<uint16_t>(*toNumber(*bufferValue));
        } else {
            error.message = "GeoJSON source buffer value must be a number";
            return nullopt;
        }
    }

    const auto toleranceValue = objectMember(value, "tolerance");
    if (toleranceValue) {
        if (toNumber(*toleranceValue)) {
            options.tolerance = static_cast<double>(*toNumber(*toleranceValue));
        } else {
            error.message = "GeoJSON source tolerance value must be a number";
            return nullopt;
        }
    }

    const auto clusterValue = objectMember(value, "cluster");
    if (clusterValue) {
        if (toBool(*clusterValue)) {
            options.cluster = *toBool(*clusterValue);
        } else {
            error.message = "GeoJSON source cluster value must be a boolean";
            return nullopt;
        }
    }

    const auto clusterMaxZoomValue = objectMember(value, "clusterMaxZoom");
    if (clusterMaxZoomValue) {
        if (toNumber(*clusterMaxZoomValue)) {
            options.clusterMaxZoom = static_cast<uint8_t>(*toNumber(*clusterMaxZoomValue));
        } else {
            error.message = "GeoJSON source clusterMaxZoom value must be a number";
            return nullopt;
        }
    }

    const auto clusterRadiusValue = objectMember(value, "clusterRadius");
    if (clusterRadiusValue) {
        if (toNumber(*clusterRadiusValue)) {
            options.clusterRadius = static_cast<double>(*toNumber(*clusterRadiusValue));
        } else {
            error.message = "GeoJSON source clusterRadius value must be a number";
            return nullopt;
        }
    }

    const auto lineMetricsValue = objectMember(value, "lineMetrics");
    if (lineMetricsValue) {
        if (toBool(*lineMetricsValue)) {
            options.lineMetrics = *toBool(*lineMetricsValue);
        } else {
            error.message = "GeoJSON source lineMetrics value must be a boolean";
            return nullopt;
        }
    }

    const auto clusterProperties = objectMember(value, "clusterProperties");
    if (clusterProperties) {
        if (!isObject(*clusterProperties)) {
            error.message = "GeoJSON source clusterProperties value must be an object";
            return nullopt;
        }
        GeoJSONOptions::ClusterProperties result;
        assert(error.message.empty());
        eachMember(
            *clusterProperties,
            [&](const std::string& k,
                const mbgl::style::conversion::Convertible& v) -> optional<conversion::Error> {
                // Each property shall be formed as ["key" : [operator, [mapExpression]]]
                // or ["key" : [[operator, ['accumulated'], ['get', key]], [mapExpression]]]
                if (!isArray(v) || arrayLength(v) != 2) {
                    error.message =
                        "GeoJSON source clusterProperties member must be an array with length of 2";
                    return nullopt;
                }
                auto map = expression::dsl::createExpression(arrayMember(v, 1));
                if (!map) {
                    error.message =
                        "Failed to convert GeoJSON source clusterProperties map expression";
                    return nullopt;
                }
                std::unique_ptr<expression::Expression> reduce;
                if (isArray(arrayMember(v, 0))) {
                    reduce = expression::dsl::createExpression(arrayMember(v, 0));
                } else {
                    auto reduceOp = toString(arrayMember(v, 0));
                    if (!reduceOp) {
                        error.message =
                            "GeoJSON source clusterProperties member must contain a valid operator";
                        return nullopt;
                    }
                    std::stringstream ss;
                    // Reformulate reduce expression to [operator, ['accumulated'], ['get', key]]
                    // The reason to create expression via parsing string instead of invoking function
                    // createCompoundExpression is due to expression type disunity can’t be resolved
                    // with current logic of createCompoundExpression
                    ss << std::string(R"([")") << *reduceOp
                       << std::string(R"(", ["accumulated"], ["get", ")") << k
                       << std::string(R"("]])");
                    reduce = expression::dsl::createExpression(ss.str().c_str());
                }
                if (!reduce) {
                    error.message =
                        "Failed to convert GeoJSON source clusterProperties reduce expression";
                    return nullopt;
                }
                result.emplace(k, std::make_pair(std::move(map), std::move(reduce)));
                return nullopt;
            });
        if (!error.message.empty()) {
            return nullopt;
        }
        options.clusterProperties = std::move(result);
    }

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

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