diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-29 17:48:58 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-01 15:33:37 -0800 |
commit | 04cc019f54c9b821d287f1b679708955b6519fe1 (patch) | |
tree | 8714298f13431818b0b9353528f0cfc5469083e3 | |
parent | 716997b99eed50ecf35fd1ec3124a85760a05753 (diff) | |
download | qtlocation-mapboxgl-04cc019f54c9b821d287f1b679708955b6519fe1.tar.gz |
[core] Refactor TileJSON parsing for offline
-rw-r--r-- | src/mbgl/map/source.cpp | 39 | ||||
-rw-r--r-- | src/mbgl/style/style_parser.cpp | 28 | ||||
-rw-r--r-- | src/mbgl/style/style_parser.hpp | 4 |
3 files changed, 49 insertions, 22 deletions
diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 65b9de7abb..b142a8471f 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -13,7 +13,6 @@ #include <mbgl/util/math.hpp> #include <mbgl/util/box.hpp> #include <mbgl/util/tile_coordinate.hpp> -#include <mbgl/util/mapbox.hpp> #include <mbgl/storage/file_source.hpp> #include <mbgl/style/style_layer.hpp> #include <mbgl/style/style_update_parameters.hpp> @@ -22,7 +21,6 @@ #include <mbgl/util/token.hpp> #include <mbgl/util/string.hpp> #include <mbgl/util/tile_cover.hpp> -#include <mbgl/util/url.hpp> #include <mbgl/map/vector_tile_data.hpp> #include <mbgl/map/raster_tile_data.hpp> @@ -102,29 +100,19 @@ void Source::load() { return; } - rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d; - d.Parse<0>(res.data->c_str()); - - if (d.HasParseError()) { - std::stringstream message; - message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); - observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(message.str()))); - return; - } - bool reloadTiles = false; + if (type == SourceType::Vector || type == SourceType::Raster) { + std::unique_ptr<SourceInfo> newInfo; + // Create a new copy of the SourceInfo object that holds the base values we've parsed // from the stylesheet. Then merge in the values parsed from the TileJSON we retrieved // via the URL. - auto newInfo = StyleParser::parseTileJSON(d); - - // TODO: Remove this hack by delivering proper URLs in the TileJSON to begin with. - if (type == SourceType::Raster && util::mapbox::isMapboxURL(url)) { - // We need to insert {ratio} into raster source URLs that are loaded from mapbox:// - // TileJSONs. - std::transform(newInfo->tiles.begin(), newInfo->tiles.end(), newInfo->tiles.begin(), - util::mapbox::normalizeRasterTileURL); + try { + newInfo = StyleParser::parseTileJSON(*res.data, url); + } catch (...) { + observer->onSourceError(*this, std::current_exception()); + return; } // Check whether previous information specifies different tile @@ -149,6 +137,17 @@ void Source::load() { info = std::move(newInfo); } else if (type == SourceType::GeoJSON) { info = std::make_unique<SourceInfo>(); + + rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d; + d.Parse<0>(res.data->c_str()); + + if (d.HasParseError()) { + std::stringstream message; + message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError()); + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(message.str()))); + return; + } + geojsonvt = StyleParser::parseGeoJSON(d); reloadTiles = true; } diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index aca4e647d2..1b088bd38f 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -11,10 +11,13 @@ #include <mapbox/geojsonvt.hpp> #include <mapbox/geojsonvt/convert.hpp> +#include <mbgl/util/mapbox.hpp> + #include <rapidjson/document.h> #include <rapidjson/error/en.h> #include <algorithm> +#include <sstream> namespace mbgl { @@ -101,7 +104,7 @@ StyleParser::~StyleParser() = default; void StyleParser::parse(const std::string& json) { rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document; - document.Parse<0>((const char *const)json.c_str()); + document.Parse<0>(json.c_str()); if (document.HasParseError()) { Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", document.GetErrorOffset(), rapidjson::GetParseError_En(document.GetParseError())); @@ -252,6 +255,29 @@ std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> StyleParser::parseGeoJSON(const JS } } +std::unique_ptr<SourceInfo> StyleParser::parseTileJSON(const std::string& json, const std::string& sourceURL) { + rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document; + document.Parse<0>(json.c_str()); + + if (document.HasParseError()) { + std::stringstream message; + message << document.GetErrorOffset() << " - " << rapidjson::GetParseError_En(document.GetParseError()); + throw std::runtime_error(message.str()); + } + + std::unique_ptr<SourceInfo> result = StyleParser::parseTileJSON(document); + + // 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(), + util::mapbox::normalizeRasterTileURL); + } + + return result; +} + std::unique_ptr<SourceInfo> StyleParser::parseTileJSON(const JSValue& value) { auto info = std::make_unique<SourceInfo>(); parseTileJSONMember(value, info->tiles, "tiles"); diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp index 58c4d736f2..3a0d0fb6b4 100644 --- a/src/mbgl/style/style_parser.hpp +++ b/src/mbgl/style/style_parser.hpp @@ -28,9 +28,11 @@ public: std::vector<std::unique_ptr<Source>> sources; std::vector<std::unique_ptr<StyleLayer>> layers; - static std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue&); + static std::unique_ptr<SourceInfo> parseTileJSON(const std::string& json, const std::string& sourceURL); static std::unique_ptr<SourceInfo> parseTileJSON(const JSValue&); + static std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue&); + private: void parseSources(const JSValue&); void parseLayers(const JSValue&); |