1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/tileset.hpp>
#include <stdexcept>
#include <vector>
#include <cstring>
namespace {
const char* protocol = "mapbox://";
const std::size_t protocolLength = 9;
} // namespace
namespace mbgl {
namespace util {
namespace mapbox {
bool isMapboxURL(const std::string& url) {
return url.compare(0, protocolLength, protocol) == 0;
}
static bool equals(const std::string& str, const URL::Segment& segment, const char* ref) {
return str.compare(segment.first, segment.second, ref) == 0;
}
std::string normalizeSourceURL(const std::string& baseURL,
const std::string& str,
const std::string& accessToken) {
if (!isMapboxURL(str)) {
return str;
}
if (accessToken.empty()) {
throw std::runtime_error(
"You must provide a Mapbox API access token for Mapbox tile sources");
}
const URL url(str);
const auto tpl = baseURL + "/v4/{domain}.json?access_token=" + accessToken + "&secure";
return transformURL(tpl, str, url);
}
std::string normalizeStyleURL(const std::string& baseURL,
const std::string& str,
const std::string& accessToken) {
if (!isMapboxURL(str)) {
return str;
}
const URL url(str);
if (!equals(str, url.domain, "styles")) {
Log::Error(Event::ParseStyle, "Invalid style URL");
return str;
}
const auto tpl = baseURL + "/styles/v1{path}?access_token=" + accessToken;
return transformURL(tpl, str, url);
}
std::string normalizeSpriteURL(const std::string& baseURL,
const std::string& str,
const std::string& accessToken) {
if (!isMapboxURL(str)) {
return str;
}
const URL url(str);
if (!equals(str, url.domain, "sprites")) {
Log::Error(Event::ParseStyle, "Invalid sprite URL");
return str;
}
const auto tpl =
baseURL + "/styles/v1{directory}{filename}/sprite{extension}?access_token=" + accessToken;
return transformURL(tpl, str, url);
}
std::string normalizeGlyphsURL(const std::string& baseURL,
const std::string& str,
const std::string& accessToken) {
if (!isMapboxURL(str)) {
return str;
}
const URL url(str);
if (!equals(str, url.domain, "fonts")) {
Log::Error(Event::ParseStyle, "Invalid glyph URL");
return str;
}
const auto tpl = baseURL + "/fonts/v1{path}?access_token=" + accessToken;
return transformURL(tpl, str, url);
}
std::string normalizeTileURL(const std::string& baseURL,
const std::string& str,
const std::string& accessToken) {
if (!isMapboxURL(str)) {
return str;
}
const URL url(str);
if (!equals(str, url.domain, "tiles")) {
Log::Error(Event::ParseStyle, "Invalid tile URL");
return str;
}
const auto tpl = baseURL + "/v4{path}?access_token=" + accessToken;
return transformURL(tpl, str, url);
}
std::string
canonicalizeTileURL(const std::string& str, const style::SourceType type, const uint16_t tileSize) {
const char* version = "/v4/";
const size_t versionLen = strlen(version);
const URL url(str);
const Path path(str, url.path.first, url.path.second);
// Make sure that we are dealing with a valid Mapbox tile URL.
// Has to be /v4/, with a valid filename + extension
if (str.compare(url.path.first, versionLen, version) != 0 || path.filename.second == 0 ||
path.extension.second <= 1) {
// Not a proper Mapbox tile URL.
return str;
}
// Reassemble the canonical URL from the parts we've parsed before.
std::string result = "mapbox://tiles/";
result.append(str, path.directory.first + versionLen, path.directory.second - versionLen);
result.append(str, path.filename.first, path.filename.second);
if (type == style::SourceType::Raster || type == style::SourceType::RasterDEM) {
result += tileSize == util::tileSize ? "@2x" : "{ratio}";
}
result.append(str, path.extension.first, path.extension.second);
// Append the query string, minus the access token parameter.
if (url.query.second > 1) {
auto idx = url.query.first;
bool hasQuery = false;
while (idx != std::string::npos) {
idx++; // skip & or ?
auto ampersandIdx = str.find('&', idx);
const char* accessToken = "access_token=";
if (str.compare(idx, strlen(accessToken), accessToken) != 0) {
result.append(1, hasQuery ? '&' : '?');
result.append(str, idx, ampersandIdx != std::string::npos ? ampersandIdx - idx
: std::string::npos);
hasQuery = true;
}
idx = ampersandIdx;
}
}
return result;
}
void canonicalizeTileset(Tileset& tileset, const std::string& sourceURL, style::SourceType type, uint16_t tileSize) {
// TODO: Remove this hack by delivering proper URLs in the TileJSON to begin with.
if (isMapboxURL(sourceURL)) {
for (auto& url : tileset.tiles) {
url = canonicalizeTileURL(url, type, tileSize);
}
}
}
const uint64_t DEFAULT_OFFLINE_TILE_COUNT_LIMIT = 6000;
} // end namespace mapbox
} // end namespace util
} // end namespace mbgl
|