diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 16e2630a9..548c837ad 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -33,93 +33,6 @@ namespace mbgl { -void parse(const rapidjson::Value& value, std::vector& target, const char *name) { - if (!value.HasMember(name)) - return; - - const rapidjson::Value& property = value[name]; - if (!property.IsArray()) - return; - - for (rapidjson::SizeType i = 0; i < property.Size(); i++) - if (!property[i].IsString()) - return; - - for (rapidjson::SizeType i = 0; i < property.Size(); i++) - target.emplace_back(std::string(property[i].GetString(), property[i].GetStringLength())); -} - -void parse(const rapidjson::Value& value, std::string& target, const char* name) { - if (!value.HasMember(name)) - return; - - const rapidjson::Value& property = value[name]; - if (!property.IsString()) - return; - - target = { property.GetString(), property.GetStringLength() }; -} - -void parse(const rapidjson::Value& value, uint16_t& target, const char* name) { - if (!value.HasMember(name)) - return; - - const rapidjson::Value& property = value[name]; - if (!property.IsUint()) - return; - - unsigned int uint = property.GetUint(); - if (uint > std::numeric_limits::max()) - return; - - target = uint; -} - -template -void parse(const rapidjson::Value& value, std::array& target, const char* name) { - if (!value.HasMember(name)) - return; - - const rapidjson::Value& property = value[name]; - if (!property.IsArray() || property.Size() != N) - return; - - for (rapidjson::SizeType i = 0; i < property.Size(); i++) - if (!property[i].IsNumber()) - return; - - for (rapidjson::SizeType i = 0; i < property.Size(); i++) - target[i] = property[i].GetDouble(); -} - -void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) { - parse(value, tiles, "tiles"); - parse(value, min_zoom, "minzoom"); - parse(value, max_zoom, "maxzoom"); - parse(value, attribution, "attribution"); - parse(value, center, "center"); - parse(value, bounds, "bounds"); -} - -std::string SourceInfo::tileURL(const TileID& id, float pixelRatio) const { - std::string result = tiles.at(0); - result = util::mapbox::normalizeTileURL(result, url, type); - result = util::replaceTokens(result, [&](const std::string &token) -> std::string { - if (token == "z") return util::toString(std::min(id.z, static_cast(max_zoom))); - if (token == "x") return util::toString(id.x); - if (token == "y") return util::toString(id.y); - if (token == "prefix") { - std::string prefix { 2 }; - prefix[0] = "0123456789abcdef"[id.x % 16]; - prefix[1] = "0123456789abcdef"[id.y % 16]; - return prefix; - } - if (token == "ratio") return pixelRatio > 1.0 ? "@2x" : ""; - return ""; - }); - return result; -} - Source::Source() {} Source::~Source() = default; diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index 4d985091f..dd94dc1fe 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -1,24 +1,13 @@ #ifndef MBGL_MAP_SOURCE #define MBGL_MAP_SOURCE -#include -#include #include -#include +#include -#include #include -#include -#include -#include -#include - -#include #include -#include #include -#include namespace mbgl { @@ -30,23 +19,6 @@ class Tile; struct ClipID; struct box; -class SourceInfo : private util::noncopyable { -public: - SourceType type = SourceType::Vector; - std::string url; - std::vector tiles; - uint16_t tile_size = util::tileSize; - uint16_t min_zoom = 0; - uint16_t max_zoom = 22; - std::string attribution; - std::array center = {{0, 0, 0}}; - std::array bounds = {{-180, -90, 180, 90}}; - std::string source_id = ""; - - void parseTileJSONProperties(const rapidjson::Value&); - std::string tileURL(const TileID& id, float pixelRatio) const; -}; - class Source : private util::noncopyable { public: class Observer { diff --git a/src/mbgl/map/source_info.cpp b/src/mbgl/map/source_info.cpp new file mode 100644 index 000000000..b8ab84b55 --- /dev/null +++ b/src/mbgl/map/source_info.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + +namespace mbgl { + +namespace { + +void parse(const rapidjson::Value& value, std::vector& target, const char* name) { + if (!value.HasMember(name)) { + return; + } + + const rapidjson::Value& property = value[name]; + if (!property.IsArray()) { + return; + } + + for (rapidjson::SizeType i = 0; i < property.Size(); i++) { + if (!property[i].IsString()) { + return; + } + } + + for (rapidjson::SizeType i = 0; i < property.Size(); i++) { + target.emplace_back(std::string(property[i].GetString(), property[i].GetStringLength())); + } +} + +void parse(const rapidjson::Value& value, std::string& target, const char* name) { + if (!value.HasMember(name)) { + return; + } + + const rapidjson::Value& property = value[name]; + if (!property.IsString()) { + return; + } + + target = { property.GetString(), property.GetStringLength() }; +} + +void parse(const rapidjson::Value& value, uint16_t& target, const char* name) { + if (!value.HasMember(name)) { + return; + } + + const rapidjson::Value& property = value[name]; + if (!property.IsUint()) { + return; + } + + unsigned int uint = property.GetUint(); + if (uint > std::numeric_limits::max()) { + return; + } + + target = uint; +} + +template +void parse(const rapidjson::Value& value, std::array& target, const char* name) { + if (!value.HasMember(name)) { + return; + } + + const rapidjson::Value& property = value[name]; + if (!property.IsArray() || property.Size() != N) { + return; + } + + for (rapidjson::SizeType i = 0; i < property.Size(); i++) { + if (!property[i].IsNumber()) { + return; + } + } + + for (rapidjson::SizeType i = 0; i < property.Size(); i++) { + target[i] = property[i].GetDouble(); + } +} + +} // end namespace + +void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) { + parse(value, tiles, "tiles"); + parse(value, min_zoom, "minzoom"); + parse(value, max_zoom, "maxzoom"); + parse(value, attribution, "attribution"); + parse(value, center, "center"); + parse(value, bounds, "bounds"); +} + +std::string SourceInfo::tileURL(const TileID& id, float pixelRatio) const { + std::string result = tiles.at(0); + result = util::mapbox::normalizeTileURL(result, url, type); + result = util::replaceTokens(result, [&](const std::string& token) -> std::string { + if (token == "z") { + return util::toString(std::min(id.z, static_cast(max_zoom))); + } else if (token == "x") { + return util::toString(id.x); + } else if (token == "y") { + return util::toString(id.y); + } else if (token == "prefix") { + std::string prefix{ 2 }; + prefix[0] = "0123456789abcdef"[id.x % 16]; + prefix[1] = "0123456789abcdef"[id.y % 16]; + return prefix; + } else if (token == "ratio") { + return pixelRatio > 1.0 ? "@2x" : ""; + } else { + return ""; + } + }); + return result; +} + +} // namespace mbgl diff --git a/src/mbgl/map/source_info.hpp b/src/mbgl/map/source_info.hpp new file mode 100644 index 000000000..725e9f424 --- /dev/null +++ b/src/mbgl/map/source_info.hpp @@ -0,0 +1,38 @@ +#ifndef MBGL_MAP_SOURCE_INFO +#define MBGL_MAP_SOURCE_INFO + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include + +namespace mbgl { + +class SourceInfo : private util::noncopyable { +public: + SourceType type = SourceType::Vector; + std::string url; + std::vector tiles; + uint16_t tile_size = util::tileSize; + uint16_t min_zoom = 0; + uint16_t max_zoom = 22; + std::string attribution; + std::array center = { { 0, 0, 0 } }; + std::array bounds = { { -180, -90, 180, 90 } }; + std::string source_id = ""; + + void parseTileJSONProperties(const rapidjson::Value&); + std::string tileURL(const TileID& id, float pixelRatio) const; +}; + +} // namespace mbgl + +#endif // MBGL_MAP_SOURCE_INFO