From 41760b5696e71c3d201d94d5c8b2423c1f348446 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 14 Jun 2016 12:46:29 -0700 Subject: [PATCH] [core] Use variant in GeoJSON source --- .../default/mbgl/storage/offline_download.cpp | 10 +++-- src/mbgl/style/sources/geojson_source.cpp | 43 ++++++++----------- src/mbgl/style/sources/geojson_source.hpp | 24 ++++++----- src/mbgl/tile/geojson_tile.cpp | 6 +-- src/mbgl/tile/geojson_tile.hpp | 2 +- 5 files changed, 41 insertions(+), 44 deletions(-) diff --git a/platform/default/mbgl/storage/offline_download.cpp b/platform/default/mbgl/storage/offline_download.cpp index 3d0ebaa68..3f21b8571 100644 --- a/platform/default/mbgl/storage/offline_download.cpp +++ b/platform/default/mbgl/storage/offline_download.cpp @@ -127,7 +127,9 @@ OfflineRegionStatus OfflineDownload::getStatus() const { case SourceType::GeoJSON: { style::GeoJSONSource* geojsonSource = static_cast(source.get()); - if (!geojsonSource->getURL().empty()) { + const variant& urlOrGeoJSON = geojsonSource->getURLOrGeoJSON(); + + if (urlOrGeoJSON.is()) { result.requiredResourceCount += 1; } break; @@ -188,8 +190,10 @@ void OfflineDownload::activateDownload() { case SourceType::GeoJSON: { style::GeoJSONSource* geojsonSource = static_cast(source.get()); - if (!geojsonSource->getURL().empty()) { - ensureResource(Resource::source(geojsonSource->getURL())); + const variant& urlOrGeoJSON = geojsonSource->getURLOrGeoJSON(); + + if (urlOrGeoJSON.is()) { + ensureResource(Resource::source(urlOrGeoJSON.get())); } break; } diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index 00c344cf0..e06b00ec9 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -15,7 +15,7 @@ namespace mbgl { namespace style { -std::unique_ptr parseGeoJSON(const JSValue& value) { +std::unique_ptr GeoJSONSource::parseGeoJSON(const JSValue& value) { using namespace mapbox::geojsonvt; Options options; @@ -32,11 +32,7 @@ std::unique_ptr parseGeoJSON(const JSValue& value) } } -std::unique_ptr GeoJSONSource::parse(const std::string& id, - const JSValue& value) { - std::unique_ptr geojsonvt; - std::string url; - +std::unique_ptr GeoJSONSource::parse(const std::string& id, const JSValue& value) { // We should probably split this up to have URLs in the url property, and actual data // in the data property. Until then, we're going to detect the content based on the // object type. @@ -47,36 +43,24 @@ std::unique_ptr GeoJSONSource::parse(const std::string& id, const JSValue& dataVal = value["data"]; if (dataVal.IsString()) { - // We need to load an external GeoJSON file - url = { dataVal.GetString(), dataVal.GetStringLength() }; + return std::make_unique(id, std::string(dataVal.GetString(), dataVal.GetStringLength())); } else if (dataVal.IsObject()) { - // We need to parse dataVal as a GeoJSON object - geojsonvt = parseGeoJSON(dataVal); + return std::make_unique(id, parseGeoJSON(dataVal)); } else { Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object"); return nullptr; } - - return std::make_unique(id, url, std::move(geojsonvt)); } -GeoJSONSource::GeoJSONSource(std::string id_, - std::string url_, - std::unique_ptr&& geojsonvt_) +GeoJSONSource::GeoJSONSource(std::string id_, variant urlOrGeoJSON_) : Source(SourceType::GeoJSON, std::move(id_)), - url(std::move(url_)), - geojsonvt(std::move(geojsonvt_)) { + urlOrGeoJSON(std::move(urlOrGeoJSON_)) { } GeoJSONSource::~GeoJSONSource() = default; -Range GeoJSONSource::getZoomRange() { - return { 0, geojsonvt->options.maxZoom }; -} - void GeoJSONSource::load(FileSource& fileSource) { - if (url.empty()) { - // If the URL is empty, the GeoJSON was specified inline in the stylesheet. + if (urlOrGeoJSON.is()) { loaded = true; return; } @@ -85,6 +69,7 @@ void GeoJSONSource::load(FileSource& fileSource) { return; } + const std::string& url = urlOrGeoJSON.get(); req = fileSource.request(Resource::source(url), [this](Response res) { if (res.error) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); @@ -103,19 +88,25 @@ void GeoJSONSource::load(FileSource& fileSource) { return; } - geojsonvt = style::parseGeoJSON(d); - invalidateTiles(); + urlOrGeoJSON = parseGeoJSON(d); loaded = true; + observer->onSourceLoaded(*this); } }); } +Range GeoJSONSource::getZoomRange() { + assert(loaded); + return { 0, urlOrGeoJSON.get()->options.maxZoom }; +} + std::unique_ptr GeoJSONSource::createTile(const OverscaledTileID& tileID, const UpdateParameters& parameters) { - return std::make_unique(tileID, id, parameters, geojsonvt.get()); + assert(loaded); + return std::make_unique(tileID, id, parameters, *urlOrGeoJSON.get()); } } // namespace style diff --git a/src/mbgl/style/sources/geojson_source.hpp b/src/mbgl/style/sources/geojson_source.hpp index 00d245c87..490dae48b 100644 --- a/src/mbgl/style/sources/geojson_source.hpp +++ b/src/mbgl/style/sources/geojson_source.hpp @@ -1,8 +1,8 @@ #pragma once #include - #include +#include namespace mapbox { namespace geojsonvt { @@ -18,25 +18,29 @@ namespace style { class GeoJSONSource : public Source { public: - static std::unique_ptr parse(const std::string& id, - const JSValue&); + using GeoJSON = std::unique_ptr; - GeoJSONSource(std::string id, - std::string url, - std::unique_ptr&&); + static std::unique_ptr parse(const std::string& id, const JSValue&); + static GeoJSON parseGeoJSON(const JSValue&); + + GeoJSONSource(std::string id, variant urlOrGeoJSON); ~GeoJSONSource() final; void load(FileSource&) final; - const std::string& getURL() const { return url; } + uint16_t getTileSize() const final { + return util::tileSize; + } + + const variant& getURLOrGeoJSON() const { + return urlOrGeoJSON; + } private: - uint16_t getTileSize() const final { return util::tileSize; } Range getZoomRange() final; std::unique_ptr createTile(const OverscaledTileID&, const UpdateParameters&) final; - const std::string url; - std::unique_ptr geojsonvt; + variant urlOrGeoJSON; std::unique_ptr req; }; diff --git a/src/mbgl/tile/geojson_tile.cpp b/src/mbgl/tile/geojson_tile.cpp index f7bf969e7..9334cf2fe 100644 --- a/src/mbgl/tile/geojson_tile.cpp +++ b/src/mbgl/tile/geojson_tile.cpp @@ -109,11 +109,9 @@ std::unique_ptr convertTile(const mapbox::geojsonvt::Tile& tile GeoJSONTile::GeoJSONTile(const OverscaledTileID& overscaledTileID, std::string sourceID, const style::UpdateParameters& parameters, - mapbox::geojsonvt::GeoJSONVT* geojsonvt) + mapbox::geojsonvt::GeoJSONVT& geojsonvt) : GeometryTile(overscaledTileID, sourceID, parameters.style, parameters.mode) { - if (geojsonvt) { - setData(convertTile(geojsonvt->getTile(id.canonical.z, id.canonical.x, id.canonical.y))); - } + setData(convertTile(geojsonvt.getTile(id.canonical.z, id.canonical.x, id.canonical.y))); } void GeoJSONTile::setNecessity(Necessity) {} diff --git a/src/mbgl/tile/geojson_tile.hpp b/src/mbgl/tile/geojson_tile.hpp index 8cccda77b..09fdd1ec9 100644 --- a/src/mbgl/tile/geojson_tile.hpp +++ b/src/mbgl/tile/geojson_tile.hpp @@ -19,7 +19,7 @@ public: GeoJSONTile(const OverscaledTileID&, std::string sourceID, const style::UpdateParameters&, - mapbox::geojsonvt::GeoJSONVT*); + mapbox::geojsonvt::GeoJSONVT&); void setNecessity(Necessity) final; };