From 41c01373d5aef0a240127c47b11ac5d97cb5f138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Thu, 26 Feb 2015 19:45:46 -0800 Subject: [PATCH 1/4] =?UTF-8?q?Load=202=C3=97=20raster=20tiles=20on=20HiDP?= =?UTF-8?q?I=20screens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on mapbox.normalizeTileURL() in mapbox/mapbox-gl-js, but insert a “{ratio}” placeholder instead of “@2x” in order to keep the pixelRatio logic in TileData::request(). Fixes #919. --- src/mbgl/map/tile_data.cpp | 3 ++- src/mbgl/util/mapbox.cpp | 9 +++++++++ src/mbgl/util/mapbox.hpp | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index 53d36dfe8..b8f317a07 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,7 +38,7 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function std::string { + url = util::replaceTokens(util::mapbox::normalizeTileURL(url, source.url), [&](const std::string &token) -> std::string { if (token == "z") return util::toString(id.z); if (token == "x") return util::toString(id.x); if (token == "y") return util::toString(id.y); diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 277b647f3..3e8bbc030 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace mbgl { namespace util { @@ -39,6 +40,14 @@ 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) { + if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0) + return url; + + static std::regex extension_re("\\.((?:png|jpg)\\d*)(?=$|\\?)"); + return std::regex_replace(url, extension_re, std::string("{ratio}.$1")); +} + } } } diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 0fbb9a91e..68b821eab 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -9,6 +9,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); } } From a6c8a76694489d9ba81554fd19fd85d755eeead2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Mon, 2 Mar 2015 10:40:55 -0800 Subject: [PATCH 2/4] Replace regex usage with basic string manipulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently we can’t rely on the existence of yet. Fixes #919. --- src/mbgl/util/mapbox.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 3e8bbc030..f89ed6c00 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -1,7 +1,6 @@ #include #include -#include namespace mbgl { namespace util { @@ -44,8 +43,23 @@ std::string normalizeTileURL(const std::string& url, const std::string& sourceUR if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0) return url; - static std::regex extension_re("\\.((?:png|jpg)\\d*)(?=$|\\?)"); - return std::regex_replace(url, extension_re, std::string("{ratio}.$1")); + 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 extensionIdx = urlSansParams.length() - 4; + if (extensionIdx <= 0) + return url; + std::string extension = urlSansParams.substr(extensionIdx); + if (extension.compare(".png") != 0 && extension.compare(".jpg") != 0) + return url; + + std::string normalizedURL(url); + normalizedURL.insert(extensionIdx, "{ratio}"); + return normalizedURL; } } From a3d76dad1064d3020f8b972ed5e7427966f30983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Mon, 2 Mar 2015 15:18:36 -0800 Subject: [PATCH 3/4] Ported util::mapbox tests from mapbox-gl-js Left out several tests that were redundant because mapbox.cpp only looks at the passed in access token. --- test/miscellaneous/mapbox.cpp | 61 +++++++++++++++++++++++++++++++++++ test/test.gyp | 1 + 2 files changed, 62 insertions(+) create mode 100644 test/miscellaneous/mapbox.cpp diff --git a/test/miscellaneous/mapbox.cpp b/test/miscellaneous/mapbox.cpp new file mode 100644 index 000000000..62344e016 --- /dev/null +++ b/test/miscellaneous/mapbox.cpp @@ -0,0 +1,61 @@ +#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"), "http://path.png/tile{ratio}.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png32", "mapbox://user.map"), "http://path.png/tile{ratio}.png32"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png70", "mapbox://user.map"), "http://path.png/tile{ratio}.png70"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png?access_token=foo", "mapbox://user.map"), "http://path.png/tile{ratio}.png?access_token=foo"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "http://path"), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", ""), "http://path.png"); + } 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; + } + } +} 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', From 8804c47d2736e284bb3d6272a38ecc90797d01f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Fri, 6 Mar 2015 12:14:21 -0800 Subject: [PATCH 4/4] Only add {ratio} to raster tile URLs Thanks to @mb12 for the suggestion. --- src/mbgl/map/tile_data.cpp | 3 ++- src/mbgl/util/mapbox.cpp | 20 ++++++++++++-------- src/mbgl/util/mapbox.hpp | 3 ++- test/miscellaneous/mapbox.cpp | 16 ++++++++++------ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index b8f317a07..7b379e9fe 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -38,7 +38,8 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function std::string { + url = util::mapbox::normalizeTileURL(url, source.url, source.type); + url = util::replaceTokens(url, [&](const std::string &token) -> std::string { if (token == "z") return util::toString(id.z); if (token == "x") return util::toString(id.x); if (token == "y") return util::toString(id.y); diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index f89ed6c00..d5b467246 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -39,23 +39,27 @@ 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) { - if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0) +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())) + while (!urlSansParams.empty() && isdigit(urlSansParams.back())) { urlSansParams.pop_back(); + } - std::string::size_type extensionIdx = urlSansParams.length() - 4; - if (extensionIdx <= 0) - return url; - std::string extension = urlSansParams.substr(extensionIdx); - if (extension.compare(".png") != 0 && extension.compare(".jpg") != 0) + 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}"); diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 68b821eab..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,7 +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); +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 index 62344e016..e82478874 100644 --- a/test/miscellaneous/mapbox.cpp +++ b/test/miscellaneous/mapbox.cpp @@ -19,12 +19,15 @@ TEST(Mapbox, GlyphsURL) { TEST(Mapbox, TileURL) { try { - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map"), "http://path.png/tile{ratio}.png"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png32", "mapbox://user.map"), "http://path.png/tile{ratio}.png32"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png70", "mapbox://user.map"), "http://path.png/tile{ratio}.png70"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png?access_token=foo", "mapbox://user.map"), "http://path.png/tile{ratio}.png?access_token=foo"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "http://path"), "http://path.png"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", ""), "http://path.png"); + 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'; @@ -57,5 +60,6 @@ TEST(Mapbox, TileURL) { default: break; } + throw e; } }