Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] Refactor TileJSON parsing for offline #3771

Merged
merged 1 commit into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
28 changes: 27 additions & 1 deletion src/mbgl/style/style_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/style/style_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class StyleParser {
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&);
Expand Down