diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index 53d36dfe8..7b379e9fe 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,7 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function std::string { if (token == "z") return util::toString(id.z); if (token == "x") return util::toString(id.x); diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 277b647f3..d5b467246 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -39,6 +39,33 @@ std::string normalizeGlyphsURL(const std::string& url, const std::string& access return normalizeURL(url, accessToken); } +std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType) { + if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0 || + sourceType != SourceType::Raster) { + return url; + } + + std::string::size_type queryIdx = url.rfind("?"); + // Trim off the right end but never touch anything before the extension dot. + std::string urlSansParams((queryIdx == std::string::npos) ? url : url.substr(0, queryIdx)); + + while (!urlSansParams.empty() && isdigit(urlSansParams.back())) { + urlSansParams.pop_back(); + } + + std::string::size_type basenameIdx = url.rfind("/"); + std::string::size_type extensionIdx = url.rfind("."); + if (basenameIdx == std::string::npos || extensionIdx == std::string::npos || + basenameIdx > extensionIdx) { + // No file extension: probably not a file name we can tack a ratio onto. + return url; + } + + std::string normalizedURL(url); + normalizedURL.insert(extensionIdx, "{ratio}"); + return normalizedURL; +} + } } } diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 0fbb9a91e..8ad92c0b4 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -2,6 +2,7 @@ #define MBGL_UTIL_MAPBOX #include +#include namespace mbgl { namespace util { @@ -9,6 +10,7 @@ namespace mapbox { std::string normalizeSourceURL(const std::string& url, const std::string& accessToken); std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken); +std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType); } } diff --git a/test/miscellaneous/mapbox.cpp b/test/miscellaneous/mapbox.cpp new file mode 100644 index 000000000..e82478874 --- /dev/null +++ b/test/miscellaneous/mapbox.cpp @@ -0,0 +1,65 @@ +#include "../fixtures/util.hpp" + +#include +#include +#include + +using namespace mbgl; + +TEST(Mapbox, SourceURL) { + EXPECT_EQ(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", "key"), "https://api.tiles.mapbox.com/v4/user.map.json?access_token=key"); + EXPECT_EQ(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", "token"), "https://api.tiles.mapbox.com/v4/user.map.json?access_token=token"); + EXPECT_THROW(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", ""), std::runtime_error); +} + +TEST(Mapbox, GlyphsURL) { + EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("mapbox://fontstack/{fontstack}/{range}.pbf", "key"), "https://api.tiles.mapbox.com/v4/fontstack/{fontstack}/{range}.pbf?access_token=key"); + EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("http://path", "key"), "http://path"); +} + +TEST(Mapbox, TileURL) { + try { + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png32", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png32"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png70", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png70"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png?access_token=foo", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png?access_token=foo"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "http://path", SourceType::Raster), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "", SourceType::Raster), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map", SourceType::Vector), "http://path.png/tile.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf", "mapbox://user.map", SourceType::Vector), "http://path.png/tile.pbf"); + } catch (const std::regex_error& e) { + std::cout << "regex_error caught: " << e.what() << '\n'; + std::cout << e.code() << '\n'; + switch (e.code()) { + case std::regex_constants::error_collate: + std::cout << "error_collate" << '\n'; break; + case std::regex_constants::error_ctype: + std::cout << "error_ctype" << '\n'; break; + case std::regex_constants::error_escape: + std::cout << "error_escape" << '\n'; break; + case std::regex_constants::error_backref: + std::cout << "error_backref" << '\n'; break; + case std::regex_constants::error_paren: + std::cout << "error_paren" << '\n'; break; + case std::regex_constants::error_brace: + std::cout << "error_brace" << '\n'; break; + case std::regex_constants::error_badbrace: + std::cout << "error_badbrace" << '\n'; break; + case std::regex_constants::error_range: + std::cout << "error_range" << '\n'; break; + case std::regex_constants::error_space: + std::cout << "error_space" << '\n'; break; + case std::regex_constants::error_badrepeat: + std::cout << "error_badrepeat" << '\n'; break; + case std::regex_constants::error_complexity: + std::cout << "error_complexity" << '\n'; break; + case std::regex_constants::error_stack: + std::cout << "error_stack" << '\n'; break; + + default: + break; + } + throw e; + } +} diff --git a/test/test.gyp b/test/test.gyp index 7759caced..bee2f4c4e 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -41,6 +41,7 @@ 'miscellaneous/comparisons.cpp', 'miscellaneous/enums.cpp', 'miscellaneous/functions.cpp', + 'miscellaneous/mapbox.cpp', 'miscellaneous/merge_lines.cpp', 'miscellaneous/rotation_range.cpp', 'miscellaneous/style_parser.cpp',