Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Allow using tileSize: 512 as a switch to trade retina support for 512…
Browse files Browse the repository at this point in the history
…px raster tiles
  • Loading branch information
yhahn authored and jfirebaugh committed Feb 11, 2016
1 parent 7ca602b commit 43c44ec
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 31 deletions.
4 changes: 2 additions & 2 deletions platform/default/mbgl/storage/offline_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ OfflineRegionStatus OfflineDownload::getStatus() const {
optional<Response> sourceResponse = offlineDatabase.get(Resource::source(source->url));
if (sourceResponse) {
result.requiredResourceCount += tileResources(source->type, source->tileSize,
*StyleParser::parseTileJSON(*sourceResponse->data, source->url, source->type)).size();
*StyleParser::parseTileJSON(*sourceResponse->data, source->url, source->type, source->tileSize)).size();
} else {
result.requiredResourceCountIsIndeterminate = true;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ void OfflineDownload::activateDownload() {
requiredSourceURLs.insert(url);

ensureResource(Resource::source(url), [=] (Response sourceResponse) {
ensureTiles(type, tileSize, *StyleParser::parseTileJSON(*sourceResponse.data, url, type));
ensureTiles(type, tileSize, *StyleParser::parseTileJSON(*sourceResponse.data, url, type, tileSize));

requiredSourceURLs.erase(url);
if (requiredSourceURLs.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Source::load() {
// from the stylesheet. Then merge in the values parsed from the TileJSON we retrieved
// via the URL.
try {
newInfo = StyleParser::parseTileJSON(*res.data, url, type);
newInfo = StyleParser::parseTileJSON(*res.data, url, type, tileSize);
} catch (...) {
observer->onSourceError(*this, std::current_exception());
return;
Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/style/style_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> StyleParser::parseGeoJSON(const JS
}
}

std::unique_ptr<SourceInfo> StyleParser::parseTileJSON(const std::string& json, const std::string& sourceURL, SourceType type) {
std::unique_ptr<SourceInfo> StyleParser::parseTileJSON(const std::string& json, const std::string& sourceURL, SourceType type, uint16_t tileSize) {
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document;
document.Parse<0>(json.c_str());

Expand All @@ -270,10 +270,9 @@ std::unique_ptr<SourceInfo> StyleParser::parseTileJSON(const std::string& json,

// TODO: Remove this hack by delivering proper URLs in the TileJSON to begin with.
if (util::mapbox::isMapboxURL(sourceURL)) {
std::transform(result->tiles.begin(),
result->tiles.end(),
result->tiles.begin(),
std::bind(util::mapbox::canonicalizeTileURL, std::placeholders::_1, type));
for (auto& url : result->tiles) {
url = util::mapbox::canonicalizeTileURL(url, type, tileSize);
}
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/style/style_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class StyleParser {
// Statically evaluate layer properties to determine what font stacks are used.
std::vector<std::string> fontStacks() const;

static std::unique_ptr<SourceInfo> parseTileJSON(const std::string& json, const std::string& sourceURL, SourceType);
static std::unique_ptr<SourceInfo> parseTileJSON(const std::string& json, const std::string& sourceURL, SourceType, uint16_t tileSize);
static std::unique_ptr<SourceInfo> parseTileJSON(const JSValue&);

static std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue&);
Expand Down
12 changes: 9 additions & 3 deletions src/mbgl/util/mapbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ std::string normalizeTileURL(const std::string& url, const std::string& accessTo
return baseURL + "v4/" + url.substr(sizeof("mapbox://tiles/") - 1) + "?access_token=" + accessToken;
}

std::string canonicalizeTileURL(const std::string& url, SourceType type) {
std::string canonicalizeTileURL(const std::string& url, SourceType type, uint16_t tileSize) {
auto tilesetStartIdx = url.find("/v4/");
if (tilesetStartIdx == std::string::npos) {
return url;
Expand Down Expand Up @@ -159,8 +159,14 @@ std::string canonicalizeTileURL(const std::string& url, SourceType type) {
}
#endif // !defined(__ANDROID__) && !defined(__APPLE__)

return "mapbox://tiles/" + tileset + "/{z}/{x}/{y}" +
(type == SourceType::Raster ? "{ratio}" : "") + "." + extension;
std::string result = "mapbox://tiles/" + tileset + "/{z}/{x}/{y}";

if (type == SourceType::Raster) {
result += tileSize == 512 ? "@2x" : "{ratio}";
}

result += "." + extension;
return result;
}

} // end namespace mapbox
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/util/mapbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ std::string normalizeGlyphsURL(const std::string& url, const std::string& access
std::string normalizeTileURL(const std::string& url, const std::string& accessToken);

// Return a "mapbox://tiles/..." URL (suitable for normalizeTileURL) for the given Mapbox tile URL.
std::string canonicalizeTileURL(const std::string& url, SourceType);
std::string canonicalizeTileURL(const std::string& url, SourceType, uint16_t tileSize);

} // namespace mapbox
} // namespace util
Expand Down
6 changes: 4 additions & 2 deletions test/style/style_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ TEST(StyleParser, ParseTileJSONRaster) {
auto result = StyleParser::parseTileJSON(
util::read_file("test/fixtures/style_parser/tilejson.raster.json"),
"mapbox://mapbox.satellite",
SourceType::Raster);
SourceType::Raster,
256);

EXPECT_EQ(0, result->minZoom);
EXPECT_EQ(15, result->maxZoom);
Expand All @@ -106,7 +107,8 @@ TEST(StyleParser, ParseTileJSONVector) {
auto result = StyleParser::parseTileJSON(
util::read_file("test/fixtures/style_parser/tilejson.vector.json"),
"mapbox://mapbox.streets",
SourceType::Vector);
SourceType::Vector,
256);

EXPECT_EQ(0, result->minZoom);
EXPECT_EQ(15, result->maxZoom);
Expand Down
50 changes: 34 additions & 16 deletions test/util/mapbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,56 +58,74 @@ TEST(Mapbox, TileURL) {
TEST(Mapbox, CanonicalURL) {
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf",
mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector));
mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf",
mbgl::util::mapbox::canonicalizeTileURL("http://b.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector));
mbgl::util::mapbox::canonicalizeTileURL("http://b.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector, 512));
EXPECT_EQ(
"mapbox://tiles/a.b,c.d/{z}/{x}/{y}.vector.pbf",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b,c.d/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b,c.d/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg70",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", SourceType::Raster, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg70",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", SourceType::Raster, 512));

#if defined(__ANDROID__) || defined(__APPLE__)
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.png",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.png",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 512));
#else
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.webp",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.webp",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 256));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.webp",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 512));
EXPECT_EQ(
"mapbox://tiles/a.b/{z}/{x}/{y}@2x.webp",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 512));
#endif // defined(__ANDROID__) || defined(__APPLE__)

// We don't ever expect to see these inputs, but be safe anyway.
EXPECT_EQ(
"",
mbgl::util::mapbox::canonicalizeTileURL("", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("", SourceType::Raster, 256));
EXPECT_EQ(
"http://path",
mbgl::util::mapbox::canonicalizeTileURL("http://path", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://path", SourceType::Raster, 256));
EXPECT_EQ(
"http://api.mapbox.com/v4/",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/", SourceType::Raster, 256));
EXPECT_EQ(
"http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", SourceType::Raster, 256));
EXPECT_EQ(
"http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.",
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", SourceType::Raster));
mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", SourceType::Raster, 256));
}

0 comments on commit 43c44ec

Please sign in to comment.