|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mbgl/style/layer.hpp> |
| 4 | +#include <mbgl/style/layers/background_layer.hpp> |
| 5 | +#include <mbgl/style/layers/circle_layer.hpp> |
| 6 | +#include <mbgl/style/layers/fill_layer.hpp> |
| 7 | +#include <mbgl/style/layers/line_layer.hpp> |
| 8 | +#include <mbgl/style/layers/raster_layer.hpp> |
| 9 | +#include <mbgl/style/layers/symbol_layer.hpp> |
| 10 | +#include <mbgl/style/conversion.hpp> |
| 11 | +#include <mbgl/style/conversion/constant.hpp> |
| 12 | +#include <mbgl/style/conversion/filter.hpp> |
| 13 | +#include <mbgl/style/conversion/make_property_setters.hpp> |
| 14 | + |
| 15 | +namespace mbgl { |
| 16 | +namespace style { |
| 17 | +namespace conversion { |
| 18 | + |
| 19 | +template <class V> |
| 20 | +optional<Error> setLayoutProperty(Layer& layer, const std::string& name, const V& value) { |
| 21 | + static const auto setters = makeLayoutPropertySetters<V>(); |
| 22 | + auto it = setters.find(name); |
| 23 | + if (it == setters.end()) { |
| 24 | + return Error { "property not found" }; |
| 25 | + } |
| 26 | + return it->second(layer, value); |
| 27 | +} |
| 28 | + |
| 29 | +template <class V> |
| 30 | +optional<Error> setPaintProperty(Layer& layer, const std::string& name, const V& value, const optional<std::string>& klass) { |
| 31 | + static const auto setters = makePaintPropertySetters<V>(); |
| 32 | + auto it = setters.find(name); |
| 33 | + if (it == setters.end()) { |
| 34 | + return Error { "property not found" }; |
| 35 | + } |
| 36 | + return it->second(layer, value, klass); |
| 37 | +} |
| 38 | + |
| 39 | +template <class V> |
| 40 | +optional<Error> setPaintProperties(Layer& layer, const V& value) { |
| 41 | + return eachMember(value, [&] (const std::string& paintName, const V& paintValue) -> optional<Error> { |
| 42 | + if (paintName.compare(0, 5, "paint") != 0) { |
| 43 | + return {}; |
| 44 | + } |
| 45 | + |
| 46 | + optional<std::string> klass; |
| 47 | + if (paintName.compare(0, 6, "paint.") == 0) { |
| 48 | + klass = paintName.substr(6); |
| 49 | + } |
| 50 | + |
| 51 | + return eachMember(paintValue, [&] (const std::string& k, const V& v) { |
| 52 | + return setPaintProperty(layer, k, v, klass); |
| 53 | + }); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +template <> |
| 58 | +struct Converter<std::unique_ptr<Layer>> { |
| 59 | +public: |
| 60 | + template <class V> |
| 61 | + Result<std::unique_ptr<Layer>> operator()(const V& value) const { |
| 62 | + if (!isObject(value)) { |
| 63 | + return Error { "layer must be an object" }; |
| 64 | + } |
| 65 | + |
| 66 | + auto idValue = objectMember(value, "id"); |
| 67 | + if (!idValue) { |
| 68 | + return Error { "layer must have an id" }; |
| 69 | + } |
| 70 | + |
| 71 | + optional<std::string> id = toString(*idValue); |
| 72 | + if (!id) { |
| 73 | + return Error { "layer id must be a string" }; |
| 74 | + } |
| 75 | + |
| 76 | + auto typeValue = objectMember(value, "type"); |
| 77 | + if (!typeValue) { |
| 78 | + return Error { "layer must have a type" }; |
| 79 | + } |
| 80 | + |
| 81 | + optional<std::string> type = toString(*typeValue); |
| 82 | + if (!type) { |
| 83 | + return Error { "layer type must be a string" }; |
| 84 | + } |
| 85 | + |
| 86 | + Result<std::unique_ptr<Layer>> converted; |
| 87 | + |
| 88 | + if (*type == "fill") { |
| 89 | + converted = convertVectorLayer<FillLayer>(*id, value); |
| 90 | + } else if (*type == "line") { |
| 91 | + converted = convertVectorLayer<LineLayer>(*id, value); |
| 92 | + } else if (*type == "circle") { |
| 93 | + converted = convertVectorLayer<CircleLayer>(*id, value); |
| 94 | + } else if (*type == "symbol") { |
| 95 | + converted = convertVectorLayer<SymbolLayer>(*id, value); |
| 96 | + } else if (*type == "raster") { |
| 97 | + converted = convertRasterLayer(*id, value); |
| 98 | + } else if (*type == "background") { |
| 99 | + converted = convertBackgroundLayer(*id, value); |
| 100 | + } else { |
| 101 | + return Error { "invalid layer type" }; |
| 102 | + } |
| 103 | + |
| 104 | + if (!converted) { |
| 105 | + return converted; |
| 106 | + } |
| 107 | + |
| 108 | + std::unique_ptr<Layer> layer = std::move(*converted); |
| 109 | + |
| 110 | + auto minzoomValue = objectMember(value, "minzoom"); |
| 111 | + if (minzoomValue) { |
| 112 | + optional<float> minzoom = toNumber(*minzoomValue); |
| 113 | + if (!minzoom) { |
| 114 | + return Error { "minzoom must be numeric" }; |
| 115 | + } |
| 116 | + layer->setMinZoom(*minzoom); |
| 117 | + } |
| 118 | + |
| 119 | + auto maxzoomValue = objectMember(value, "maxzoom"); |
| 120 | + if (maxzoomValue) { |
| 121 | + optional<float> maxzoom = toNumber(*maxzoomValue); |
| 122 | + if (!maxzoom) { |
| 123 | + return Error { "maxzoom must be numeric" }; |
| 124 | + } |
| 125 | + layer->setMaxZoom(*maxzoom); |
| 126 | + } |
| 127 | + |
| 128 | + auto layoutValue = objectMember(value, "layout"); |
| 129 | + if (layoutValue) { |
| 130 | + if (!isObject(*layoutValue)) { |
| 131 | + return Error { "layout must be an object" }; |
| 132 | + } |
| 133 | + optional<Error> error = eachMember(*layoutValue, [&] (const std::string& k, const V& v) { |
| 134 | + return setLayoutProperty(*layer, k, v); |
| 135 | + }); |
| 136 | + if (error) { |
| 137 | + return *error; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + optional<Error> error = setPaintProperties(*layer, value); |
| 142 | + if (error) { |
| 143 | + return *error; |
| 144 | + } |
| 145 | + |
| 146 | + return std::move(layer); |
| 147 | + } |
| 148 | + |
| 149 | +private: |
| 150 | + template <class LayerType, class V> |
| 151 | + Result<std::unique_ptr<Layer>> convertVectorLayer(const std::string& id, const V& value) const { |
| 152 | + auto sourceValue = objectMember(value, "source"); |
| 153 | + if (!sourceValue) { |
| 154 | + return Error { "layer must have a source" }; |
| 155 | + } |
| 156 | + |
| 157 | + optional<std::string> source = toString(*sourceValue); |
| 158 | + if (!source) { |
| 159 | + return Error { "layer source must be a string" }; |
| 160 | + } |
| 161 | + |
| 162 | + std::unique_ptr<LayerType> layer = std::make_unique<LayerType>(id, *source); |
| 163 | + |
| 164 | + auto sourceLayerValue = objectMember(value, "source-layer"); |
| 165 | + if (sourceLayerValue) { |
| 166 | + optional<std::string> sourceLayer = toString(*sourceLayerValue); |
| 167 | + if (!sourceLayer) { |
| 168 | + return Error { "layer source-layer must be a string" }; |
| 169 | + } |
| 170 | + layer->setSourceLayer(*sourceLayer); |
| 171 | + } |
| 172 | + |
| 173 | + auto filterValue = objectMember(value, "filter"); |
| 174 | + if (filterValue) { |
| 175 | + Result<Filter> filter = convert<Filter>(*filterValue); |
| 176 | + if (!filter) { |
| 177 | + return filter.error(); |
| 178 | + } |
| 179 | + layer->setFilter(*filter); |
| 180 | + } |
| 181 | + |
| 182 | + return std::move(layer); |
| 183 | + } |
| 184 | + |
| 185 | + template <class V> |
| 186 | + Result<std::unique_ptr<Layer>> convertRasterLayer(const std::string& id, const V& value) const { |
| 187 | + auto sourceValue = objectMember(value, "source"); |
| 188 | + if (!sourceValue) { |
| 189 | + return Error { "layer must have a source" }; |
| 190 | + } |
| 191 | + |
| 192 | + optional<std::string> source = toString(*sourceValue); |
| 193 | + if (!source) { |
| 194 | + return Error { "layer source must be a string" }; |
| 195 | + } |
| 196 | + |
| 197 | + return std::make_unique<RasterLayer>(id, *source); |
| 198 | + } |
| 199 | + |
| 200 | + template <class V> |
| 201 | + Result<std::unique_ptr<Layer>> convertBackgroundLayer(const std::string& id, const V&) const { |
| 202 | + return std::make_unique<BackgroundLayer>(id); |
| 203 | + } |
| 204 | +}; |
| 205 | + |
| 206 | +} // namespace conversion |
| 207 | +} // namespace style |
| 208 | +} // namespace mbgl |
0 commit comments