diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2020-04-29 12:28:50 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2020-05-26 20:35:03 +0300 |
commit | 57972231e864cf4af18f12664dbb4a838e9e99db (patch) | |
tree | 7d39fcbfe12ad7c6b3355acd444d1413960d3a70 /src/mbgl/style | |
parent | 1d892b1feef4a4d15711d4596f36e6f558afae26 (diff) | |
download | qtlocation-mapboxgl-57972231e864cf4af18f12664dbb4a838e9e99db.tar.gz |
source manager and factories
Diffstat (limited to 'src/mbgl/style')
-rw-r--r-- | src/mbgl/style/conversion/source.cpp | 193 | ||||
-rw-r--r-- | src/mbgl/style/conversion/url_or_tileset.cpp | 31 |
2 files changed, 40 insertions, 184 deletions
diff --git a/src/mbgl/style/conversion/source.cpp b/src/mbgl/style/conversion/source.cpp index cf42b40489..5c5da63171 100644 --- a/src/mbgl/style/conversion/source.cpp +++ b/src/mbgl/style/conversion/source.cpp @@ -1,185 +1,17 @@ -#include <mbgl/style/conversion/source.hpp> +#include <mbgl/sourcemanager/source_manager.hpp> #include <mbgl/style/conversion/coordinate.hpp> -#include <mbgl/style/conversion/geojson.hpp> -#include <mbgl/style/conversion/geojson_options.hpp> +#include <mbgl/style/conversion/source.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; - } - auto maxzoomValue = objectMember(value, "maxzoom"); - optional<float> maxzoom; - if (maxzoomValue) { - maxzoom = toNumber(*maxzoomValue); - if (!maxzoom || *maxzoom < 0 || *maxzoom > std::numeric_limits<uint8_t>::max()) { - error.message = "invalid maxzoom"; - return nullopt; - } - } - auto minzoomValue = objectMember(value, "minzoom"); - optional<float> minzoom; - if (minzoomValue) { - minzoom = toNumber(*minzoomValue); - if (!minzoom || *minzoom < 0 || *minzoom > std::numeric_limits<uint8_t>::max()) { - error.message = "invalid minzoom"; - return nullopt; - } - } - return {std::make_unique<VectorSource>(id, std::move(*urlOrTileset), std::move(maxzoom), std::move(minzoom))}; -} - -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; - } - - Immutable<GeoJSONOptions> options = GeoJSONOptions::defaultOptions(); - if (optional<GeoJSONOptions> converted = convert<GeoJSONOptions>(value, error)) { - options = makeMutable<GeoJSONOptions>(std::move(*converted)); - } - - auto result = std::make_unique<GeoJSONSource>(id, std::move(options)); - - if (isObject(*dataValue)) { - optional<GeoJSON> geoJSON = convert<GeoJSON>(*dataValue, error); - if (!geoJSON) { - return nullopt; - } - result->setGeoJSON(*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 { +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; @@ -197,18 +29,11 @@ optional<std::unique_ptr<Source>> Converter<std::unique_ptr<Source>>::operator() return nullopt; } const std::string& tname = type.value(); - if (tname == "raster") { - return convertRasterSource(id, value, error); - } else if (tname == "raster-dem") { - return convertRasterDEMSource(id, value, error); - } else if (tname == "vector") { - return convertVectorSource(id, value, error); - } else if (tname == "geojson") { - return convertGeoJSONSource(id, value, error); - } else if (tname == "image") { - return convertImageSource(id, value, error); + + auto source = SourceManager::get()->createSource(tname, id, value, error); + if (source) { + return source; } else { - error.message = "invalid source type"; return nullopt; } } diff --git a/src/mbgl/style/conversion/url_or_tileset.cpp b/src/mbgl/style/conversion/url_or_tileset.cpp new file mode 100644 index 0000000000..c7a8fc63e4 --- /dev/null +++ b/src/mbgl/style/conversion/url_or_tileset.cpp @@ -0,0 +1,31 @@ +#include <mbgl/style/conversion/tileset.hpp> +#include <mbgl/style/conversion/url_or_tileset.hpp> +#include <mbgl/style/conversion_impl.hpp> + +namespace mbgl { +namespace style { +namespace conversion { + +optional<variant<std::string, Tileset>> Converter<variant<std::string, Tileset>>::operator()(const Convertible& value, + Error& error) const { + 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}; +} + +} // namespace conversion +} // namespace style +} // namespace mbgl |