Extract SourceInfo::tileURL

This commit is contained in:
John Firebaugh
2015-04-06 11:48:40 -07:00
parent 15969f1369
commit d2314aedc6
3 changed files with 22 additions and 20 deletions

View File

@@ -18,6 +18,7 @@
#include <mbgl/style/style_layer.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/map/vector_tile_data.hpp>
#include <mbgl/map/raster_tile_data.hpp>
@@ -95,6 +96,25 @@ void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
parse(value, bounds, "bounds");
}
std::string SourceInfo::tileURL(const Tile::ID& id, float pixelRatio) const {
std::string result = tiles[(id.x + id.y) % tiles.size()];
result = util::mapbox::normalizeTileURL(result, url, type);
result = util::replaceTokens(result, [&](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);
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()
{
}

View File

@@ -45,6 +45,7 @@ public:
std::array<float, 4> bounds = {{-180, -90, 180, 90}};
void parseTileJSONProperties(const rapidjson::Value&);
std::string tileURL(const Tile::ID& id, float pixelRatio) const;
};
class Source : public std::enable_shared_from_this<Source>, private util::noncopyable {

View File

@@ -1,11 +1,7 @@
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/environment.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/platform/log.hpp>
@@ -37,22 +33,7 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function<void(
if (source.tiles.empty())
return;
std::string url = source.tiles[(id.x + id.y) % source.tiles.size()];
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);
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 "";
});
std::string url = source.tileURL(id, pixelRatio);
state = State::loading;
std::weak_ptr<TileData> weak_tile = shared_from_this();