[core] move raster tile URL normalization to source parsing
This commit is contained in:
committed by
John Firebaugh
parent
1ff317c1cd
commit
62973f9e5a
@@ -174,6 +174,14 @@ void Source::load() {
|
||||
|
||||
if (info.type == SourceType::Vector || info.type == SourceType::Raster) {
|
||||
parseTileJSON(d);
|
||||
|
||||
// TODO: Remove this hack by delivering proper URLs in the TileJSON to begin with.
|
||||
if (info.type == SourceType::Raster && util::mapbox::isMapboxURL(info.url)) {
|
||||
// We need to insert {ratio} into raster source URLs that are loaded from mapbox://
|
||||
// TileJSONs.
|
||||
std::transform(info.tiles.begin(), info.tiles.end(), info.tiles.begin(),
|
||||
util::mapbox::normalizeRasterTileURL);
|
||||
}
|
||||
} else if (info.type == SourceType::GeoJSON) {
|
||||
parseGeoJSON(d);
|
||||
}
|
||||
|
||||
@@ -111,11 +111,7 @@ std::string normalizeGlyphsURL(const std::string& url, const std::string& access
|
||||
return baseURL + "fonts/v1/" + user + "/" + fontstack + "/" + range + "?access_token=" + accessToken;
|
||||
}
|
||||
|
||||
std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType) {
|
||||
if (sourceURL.empty() || !isMapboxURL(sourceURL) || sourceType != SourceType::Raster) {
|
||||
return url;
|
||||
}
|
||||
|
||||
std::string normalizeRasterTileURL(const std::string& 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));
|
||||
|
||||
@@ -14,7 +14,7 @@ std::string normalizeSourceURL(const std::string& url, const std::string& access
|
||||
std::string normalizeStyleURL(const std::string& url, const std::string& accessToken);
|
||||
std::string normalizeSpriteURL(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);
|
||||
std::string normalizeRasterTileURL(const std::string& url);
|
||||
|
||||
// Canonicalizes Mapbox URLs by removing [a-d] subdomain prefixes, access tokens, and protocol.
|
||||
// Note that this is close, but not exactly the reverse operation as above, as this retains certain
|
||||
|
||||
@@ -37,19 +37,15 @@ TEST(Mapbox, SpriteURL) {
|
||||
|
||||
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");
|
||||
EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf?access_token=foo", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf?access_token=foo");
|
||||
EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf?access_token=foo.png", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf?access_token=foo.png");
|
||||
EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf?access_token=foo.png/bar", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf?access_token=foo.png/bar");
|
||||
EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf?access_token=foo.png/bar.png", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf?access_token=foo.png/bar.png");
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.png", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.png32", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png32"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.png70", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png70"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.png?access_token=foo", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png?access_token=foo"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.pbf", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo.png", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo.png"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo.png/bar", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo.png/bar"));
|
||||
EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo.png/bar.png", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo.png/bar.png"));
|
||||
} catch (const std::regex_error& e) {
|
||||
const char *error = "unknown";
|
||||
switch (e.code()) {
|
||||
|
||||
Reference in New Issue
Block a user