summaryrefslogtreecommitdiff
path: root/src/mbgl/style/conversion/source.cpp
blob: 5ecbd3b474338c70209a5c3a85f9d3114187d5c2 (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
#include <mbgl/style/conversion/source.hpp>
#include <mbgl/style/conversion/coordinate.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
#include <mbgl/style/conversion/tileset.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/raster_dem_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/sources/image_source.hpp>
#include <mbgl/util/geo.hpp>

namespace mbgl {
namespace style {
namespace conversion {

// A tile source can either specify a URL to TileJSON, or inline TileJSON.
static optional<variant<std::string, Tileset>> convertURLOrTileset(const Convertible& value, Error& error) {
    auto urlVal = objectMember(value, "url");
    if (!urlVal) {
        optional<Tileset> tileset = convert<Tileset>(value, error);
        if (!tileset) {
            return nullopt;
        }
        return { *tileset };
    }

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

    return { *url };
}

static optional<std::unique_ptr<Source>> convertRasterSource(const std::string& id,
                                                             const Convertible& value,
                                                             Error& error) {
    optional<variant<std::string, Tileset>> urlOrTileset = convertURLOrTileset(value, error);
    if (!urlOrTileset) {
        return nullopt;
    }

    uint16_t tileSize = util::tileSize;
    auto tileSizeValue = objectMember(value, "tileSize");
    if (tileSizeValue) {
        optional<float> size = toNumber(*tileSizeValue);
        if (!size || *size < 0 || *size > std::numeric_limits<uint16_t>::max()) {
            error.message = "invalid tileSize";
            return nullopt;
        }
        tileSize = *size;
    }

    return { std::make_unique<RasterSource>(id, std::move(*urlOrTileset), tileSize) };
}

static optional<std::unique_ptr<Source>> convertRasterDEMSource(const std::string& id,
                                                             const Convertible& value,
                                                             Error& error) {
    optional<variant<std::string, Tileset>> urlOrTileset = convertURLOrTileset(value, error);
    if (!urlOrTileset) {
        return nullopt;
    }

    uint16_t tileSize = util::tileSize;
    auto tileSizeValue = objectMember(value, "tileSize");
    if (tileSizeValue) {
        optional<float> size = toNumber(*tileSizeValue);
        if (!size || *size < 0 || *size > std::numeric_limits<uint16_t>::max()) {
            error.message = "invalid tileSize";
            return nullopt;
        }
        tileSize = *size;
    }

    return { std::make_unique<RasterDEMSource>(id, std::move(*urlOrTileset), tileSize) };
}

static optional<std::unique_ptr<Source>> convertVectorSource(const std::string& id,
                                                             const Convertible& value,
                                                             Error& error) {
    optional<variant<std::string, Tileset>> urlOrTileset = convertURLOrTileset(value, error);
    if (!urlOrTileset) {
        return nullopt;
    }

    return { std::make_unique<VectorSource>(id, std::move(*urlOrTileset)) };
}

static optional<std::unique_ptr<Source>> convertGeoJSONSource(const std::string& id,
                                                              const Convertible& value,
                                                              Error& error) {
    auto dataValue = objectMember(value, "data");
    if (!dataValue) {
        error.message = "GeoJSON source must have a data value";
        return nullopt;
    }

    optional<GeoJSONOptions> options = convert<GeoJSONOptions>(value, error);
    if (!options) {
        return nullopt;
    }

    auto result = std::make_unique<GeoJSONSource>(id, *options);

    if (isObject(*dataValue)) {
        optional<GeoJSON> geoJSON = convert<GeoJSON>(*dataValue, error);
        if (!geoJSON) {
            return nullopt;
        }
        result->setGeoJSON(std::move(*geoJSON));
    } else if (toString(*dataValue)) {
        result->setURL(*toString(*dataValue));
    } else {
        error.message = "GeoJSON data must be a URL or an object";
        return nullopt;
    }

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

static optional<std::unique_ptr<Source>> convertImageSource(const std::string& id,
                                                            const Convertible& value,
                                                            Error& error) {
    auto urlValue = objectMember(value, "url");
    if (!urlValue) {
        error.message = "Image source must have a url value";
        return nullopt;
    }

    auto urlString = toString(*urlValue);
    if (!urlString) {
        error.message = "Image url must be a URL string";
        return nullopt;
    }

    auto coordinatesValue = objectMember(value, "coordinates");
    if (!coordinatesValue) {
        error.message = "Image source must have a coordinates values";
        return nullopt;
    }

    if (!isArray(*coordinatesValue) || arrayLength(*coordinatesValue) != 4) {
        error.message = "Image coordinates must be an array of four longitude latitude pairs";
        return nullopt;
    }

    std::array<LatLng, 4> coordinates;
    for (std::size_t i=0; i < 4; i++) {
        auto latLng = conversion::convert<LatLng>(arrayMember(*coordinatesValue,i), error);
        if (!latLng) {
            return nullopt;
        }
        coordinates[i] = *latLng;
    }
    auto result = std::make_unique<ImageSource>(id, coordinates);
    result->setURL(*urlString);

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

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

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

    optional<std::string> type = toString(*typeValue);
    if (!type) {
        error.message = "source type must be a string";
        return nullopt;
    }
    const std::string tname = *type;
    if (*type == "raster") {
        return convertRasterSource(id, value, error);
    } else if (*type == "raster-dem") {
        return convertRasterDEMSource(id, value, error);
    } else if (*type == "vector") {
        return convertVectorSource(id, value, error);
    } else if (*type == "geojson") {
        return convertGeoJSONSource(id, value, error);
    } else if (*type == "image") {
        return convertImageSource(id, value, error);
    } else {
        error.message = "invalid source type";
        return nullopt;
    }
}

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