summaryrefslogtreecommitdiff
path: root/src/mbgl/util/mapbox.cpp
diff options
context:
space:
mode:
authorLucas Wojciechowski <lucas@mapbox.com>2015-08-20 15:50:39 -0700
committerLucas Wojciechowski <lucas@mapbox.com>2015-08-21 14:39:26 -0700
commite97662972fef0e56132a03f2c2510c0da8537aad (patch)
tree00c1725ad6a64928f42bbf0924d320def23ea08c /src/mbgl/util/mapbox.cpp
parentb7a4000a3ce273d6f5024df7dc02b68112999f94 (diff)
downloadqtlocation-mapboxgl-e97662972fef0e56132a03f2c2510c0da8537aad.tar.gz
Support v8 sprite, glyph, and style mapbox:// urls
Diffstat (limited to 'src/mbgl/util/mapbox.cpp')
-rw-r--r--src/mbgl/util/mapbox.cpp83
1 files changed, 57 insertions, 26 deletions
diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp
index 05e778dfb3..dee0e0f8f4 100644
--- a/src/mbgl/util/mapbox.cpp
+++ b/src/mbgl/util/mapbox.cpp
@@ -1,59 +1,90 @@
#include <mbgl/util/mapbox.hpp>
#include <stdexcept>
+#include <vector>
namespace mbgl {
namespace util {
namespace mapbox {
-const std::string mapbox = "mapbox://";
+const std::string protocol = "mapbox://";
+const std::string baseURL = "https://api.mapbox.com/";
-std::string normalizeURL(const std::string& url, const std::string& pathPrefix, const std::string& accessToken) {
- if (accessToken.empty())
- throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources");
+bool isMapboxURL(const std::string& url) {
+ return url.compare(0, protocol.length(), protocol) == 0;
+}
- return std::string("https://api.tiles.mapbox.com")
- + pathPrefix
- + url.substr(mapbox.length())
- + "?access_token="
- + accessToken;
+std::vector<std::string> getMapboxURLPathname(const std::string& url) {
+ std::vector<std::string> pathname;
+ std::size_t startIndex = protocol.length();
+ while (startIndex < url.length()) {
+ std::size_t endIndex = url.find("/", startIndex);
+ if (endIndex == std::string::npos) endIndex = url.length();
+ pathname.push_back(url.substr(startIndex, endIndex - startIndex));
+ startIndex = endIndex + 1;
+ }
+ return pathname;
}
std::string normalizeSourceURL(const std::string& url, const std::string& accessToken) {
- if (url.compare(0, mapbox.length(), mapbox) != 0)
+ if (!isMapboxURL(url)) {
return url;
+ }
- std::string result = normalizeURL(url + ".json", "/v4/", accessToken);
-
- // TileJSON requests need a secure flag appended to their URLs so
- // that the server knows to send SSL-ified resource references.
- result += "&secure";
+ if (accessToken.empty()) {
+ throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources");
+ }
- return result;
+ return baseURL + "v4/" + url.substr(protocol.length()) + ".json?access_token=" + accessToken + "&secure";
}
std::string normalizeStyleURL(const std::string& url, const std::string& accessToken) {
- if (url.compare(0, mapbox.length(), mapbox) != 0)
+ if (!isMapboxURL(url)) {
+ return url;
+ }
+
+ std::vector<std::string> pathname = getMapboxURLPathname(url);
+ std::string user = pathname[1];
+ std::string id = pathname[2];
+ bool isDraft = pathname.size() > 3;
+ return baseURL + "styles/v1/" + user + "/" + id + (isDraft ? "/draft" : "") + "?access_token=" + accessToken;
+}
+
+std::string normalizeSpriteURL(const std::string& url, const std::string& accessToken) {
+ if (!isMapboxURL(url)) {
return url;
+ }
- const std::string user = url.substr(mapbox.length(), url.find('.') - mapbox.length());
+ std::vector<std::string> pathname = getMapboxURLPathname(url);
+ std::string user = pathname[1];
+ bool isDraft = pathname.size() > 3;
- return normalizeURL(url, "/styles/v1/" + user + "/", accessToken);
+ std::string id, extension;
+ if (isDraft) {
+ size_t index = pathname[3].find_first_of("@.");
+ id = pathname[2];
+ extension = pathname[3].substr(index);
+ } else {
+ size_t index = pathname[2].find_first_of("@.");
+ id = pathname[2].substr(0, index);
+ extension = pathname[2].substr(index);
+ }
+
+ return baseURL + "styles/v1/" + user + "/" + id + "/" + (isDraft ? "draft/" : "") + "sprite" + extension + "?access_token=" + accessToken;
}
std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken) {
- if (url.compare(0, mapbox.length(), mapbox) != 0) {
+ if (!isMapboxURL(url)) {
return url;
- } else {
- const std::string prefix = "mapbox://fonts/";
- const std::string user = url.substr(prefix.length(), url.find("/", prefix.length()) - prefix.length());
- return normalizeURL("mapbox://" + user + "/{fontstack}/{range}.pbf", "/fonts/v1/", accessToken);
}
+
+ std::vector<std::string> pathname = getMapboxURLPathname(url);
+ std::string user = pathname[1];
+ return baseURL + "fonts/v1/" + user + "/{fontstack}/{range}.pbf?access_token=" + accessToken;
}
std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType) {
- if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0 ||
- sourceType != SourceType::Raster) {
+ if (sourceURL.empty() || !isMapboxURL(sourceURL) || sourceType != SourceType::Raster) {
return url;
}