1 #include <mapbox/geometry/box.hpp>
2 #include <mbgl/storage/resource.hpp>
3 #include <mbgl/util/constants.hpp>
4 #include <mbgl/util/string.hpp>
5 #include <mbgl/util/token.hpp>
6 #include <mbgl/util/url.hpp>
7
8 #include <cmath>
9
10 namespace mbgl {
11
getQuadKey(int32_t x,int32_t y,int8_t z)12 static std::string getQuadKey(int32_t x, int32_t y, int8_t z) {
13 std::string quadKey;
14 quadKey.reserve(z);
15 int32_t mask;
16 for (int8_t i = z; i > 0; i--) {
17 mask = 1 << (i - 1);
18 quadKey += '0' + ((x & mask ? 1 : 0) + (y & mask ? 2 : 0));
19 }
20 return quadKey;
21 }
22
getMercCoord(int32_t x,int32_t y,int8_t z)23 static mapbox::geometry::point<double> getMercCoord(int32_t x, int32_t y, int8_t z) {
24 double resolution = (util::M2PI * util::EARTH_RADIUS_M / 256) / std::pow(2, z);
25 return {
26 x * resolution - util::M2PI * util::EARTH_RADIUS_M / 2,
27 y * resolution - util::M2PI * util::EARTH_RADIUS_M / 2,
28 };
29 }
30
getTileBBox(int32_t x,int32_t y,int8_t z)31 static std::string getTileBBox(int32_t x, int32_t y, int8_t z) {
32 // Alter the y for the Google/OSM tile scheme.
33 y = std::pow(2, z) - y - 1;
34
35 auto min = getMercCoord(x * 256, y * 256, z);
36 auto max = getMercCoord((x + 1) * 256, (y + 1) * 256, z);
37
38 return (util::toString(min.x) + "," + util::toString(min.y) + "," +
39 util::toString(max.x) + "," + util::toString(max.y));
40 }
41
style(const std::string & url)42 Resource Resource::style(const std::string& url) {
43 return Resource {
44 Resource::Kind::Style,
45 url
46 };
47 }
48
source(const std::string & url)49 Resource Resource::source(const std::string& url) {
50 return Resource {
51 Resource::Kind::Source,
52 url
53 };
54 }
55
image(const std::string & url)56 Resource Resource::image(const std::string& url) {
57 return Resource {
58 Resource::Kind::Image,
59 url
60 };
61 }
62
spriteImage(const std::string & base,float pixelRatio)63 Resource Resource::spriteImage(const std::string& base, float pixelRatio) {
64 util::URL url(base);
65 return Resource{ Resource::Kind::SpriteImage,
66 base.substr(0, url.path.first + url.path.second) +
67 (pixelRatio > 1 ? "@2x" : "") + ".png" +
68 base.substr(url.query.first, url.query.second) };
69 }
70
spriteJSON(const std::string & base,float pixelRatio)71 Resource Resource::spriteJSON(const std::string& base, float pixelRatio) {
72 util::URL url(base);
73 return Resource{ Resource::Kind::SpriteJSON,
74 base.substr(0, url.path.first + url.path.second) +
75 (pixelRatio > 1 ? "@2x" : "") + ".json" +
76 base.substr(url.query.first, url.query.second) };
77 }
78
glyphs(const std::string & urlTemplate,const FontStack & fontStack,const std::pair<uint16_t,uint16_t> & glyphRange)79 Resource Resource::glyphs(const std::string& urlTemplate, const FontStack& fontStack, const std::pair<uint16_t, uint16_t>& glyphRange) {
80 return Resource {
81 Resource::Kind::Glyphs,
82 util::replaceTokens(urlTemplate, [&](const std::string& token) -> optional<std::string> {
83 if (token == "fontstack") {
84 return util::percentEncode(fontStackToString(fontStack));
85 } else if (token == "range") {
86 return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second);
87 } else {
88 return {};
89 }
90 })
91 };
92 }
93
tile(const std::string & urlTemplate,float pixelRatio,int32_t x,int32_t y,int8_t z,Tileset::Scheme scheme,LoadingMethod loadingMethod)94 Resource Resource::tile(const std::string& urlTemplate,
95 float pixelRatio,
96 int32_t x,
97 int32_t y,
98 int8_t z,
99 Tileset::Scheme scheme,
100 LoadingMethod loadingMethod) {
101 bool supportsRatio = urlTemplate.find("{ratio}") != std::string::npos;
102 if (scheme == Tileset::Scheme::TMS) {
103 y = (1 << z) - y - 1;
104 }
105 return Resource {
106 Resource::Kind::Tile,
107 util::replaceTokens(urlTemplate, [&](const std::string& token) -> optional<std::string> {
108 if (token == "z") {
109 return util::toString(z);
110 } else if (token == "x") {
111 return util::toString(x);
112 } else if (token == "y") {
113 return util::toString(y);
114 } else if (token == "quadkey") {
115 return getQuadKey(x, y, z);
116 } else if (token == "bbox-epsg-3857") {
117 return getTileBBox(x, y, z);
118 } else if (token == "prefix") {
119 std::string prefix{ 2 };
120 prefix[0] = "0123456789abcdef"[x % 16];
121 prefix[1] = "0123456789abcdef"[y % 16];
122 return prefix;
123 } else if (token == "ratio") {
124 return std::string(pixelRatio > 1.0 ? "@2x" : "");
125 } else {
126 return {};
127 }
128 }),
129 Resource::TileData {
130 urlTemplate,
131 uint8_t(supportsRatio && pixelRatio > 1.0 ? 2 : 1),
132 x,
133 y,
134 z
135 },
136 loadingMethod
137 };
138 }
139
140 } // namespace mbgl
141