diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-06-23 12:00:25 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-06-24 09:39:51 -0700 |
commit | 021d4199cb9ee754e9f0f5bc42f7f75285afd405 (patch) | |
tree | 9396f291348c0ab5f3a75e1a217a78fc4dbff4b2 /src/mbgl/style/sources | |
parent | 16c435b1517b15a5ea8654987979ef58800b838b (diff) | |
download | qtlocation-mapboxgl-021d4199cb9ee754e9f0f5bc42f7f75285afd405.tar.gz |
[core, node] Implement bindings for addSource
Diffstat (limited to 'src/mbgl/style/sources')
-rw-r--r-- | src/mbgl/style/sources/geojson_source.cpp | 13 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source_impl.cpp | 73 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source_impl.hpp | 18 | ||||
-rw-r--r-- | src/mbgl/style/sources/raster_source_impl.cpp | 21 | ||||
-rw-r--r-- | src/mbgl/style/sources/raster_source_impl.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/sources/vector_source_impl.cpp | 8 | ||||
-rw-r--r-- | src/mbgl/style/sources/vector_source_impl.hpp | 2 |
7 files changed, 49 insertions, 88 deletions
diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index b5932581dd..a3eec4f4ef 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -4,5 +4,18 @@ namespace mbgl { namespace style { +GeoJSONSource::GeoJSONSource(const std::string& id) + : Source(SourceType::GeoJSON, std::make_unique<GeoJSONSource::Impl>(std::move(id), *this)) + , impl(static_cast<Impl*>(baseImpl.get())) { +} + +void GeoJSONSource::setURL(const std::string& url) { + impl->setURL(url); +} + +void GeoJSONSource::setGeoJSON(GeoJSON&& geoJSON) { + impl->setGeoJSON(std::move(geoJSON)); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index f58e0fc62b..b9744d193a 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -1,9 +1,10 @@ #include <mbgl/style/sources/geojson_source_impl.hpp> #include <mbgl/style/source_observer.hpp> -#include <mbgl/style/parser.hpp> +#include <mbgl/style/conversion/geojson.hpp> #include <mbgl/tile/geojson_tile.hpp> #include <mbgl/storage/file_source.hpp> #include <mbgl/platform/log.hpp> +#include <mbgl/util/rapidjson.hpp> #include <mapbox/geojsonvt.hpp> #include <mapbox/geojsonvt/convert.hpp> @@ -12,56 +13,38 @@ #include <sstream> +using namespace mapbox::geojsonvt; + namespace mbgl { namespace style { - -std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> GeoJSONSource::Impl::parseGeoJSON(const JSValue& value) { - using namespace mapbox::geojsonvt; - +namespace conversion { +template <> +Result<GeoJSON> convertGeoJSON(const JSValue& value) { Options options; options.buffer = util::EXTENT / util::tileSize * 128; options.extent = util::EXTENT; try { - return std::make_unique<GeoJSONVT>(Convert::convert(value, 0), options); + return GeoJSON { std::make_unique<GeoJSONVT>(Convert::convert(value, 0), options) }; } catch (const std::exception& ex) { - Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", ex.what()); - // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for - // tiles to load. - return std::make_unique<GeoJSONVT>(std::vector<ProjectedFeature>{}, options); + return Error { ex.what() }; } } +} // namespace conversion -std::unique_ptr<GeoJSONSource> GeoJSONSource::Impl::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. - if (!value.HasMember("data")) { - Log::Error(Event::ParseStyle, "GeoJSON source must have a data value"); - return nullptr; - } - - const JSValue& dataVal = value["data"]; - if (dataVal.IsString()) { - return std::make_unique<GeoJSONSource>([&] (Source& base) { - return std::make_unique<Impl>(id, base, std::string(dataVal.GetString(), dataVal.GetStringLength())); - }); - } else if (dataVal.IsObject()) { - return std::make_unique<GeoJSONSource>([&] (Source& base) { - return std::make_unique<Impl>(id, base, parseGeoJSON(dataVal)); - }); - } else { - Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object"); - return nullptr; - } +GeoJSONSource::Impl::Impl(std::string id_, Source& base_) + : Source::Impl(SourceType::GeoJSON, std::move(id_), base_) { } -GeoJSONSource::Impl::Impl(std::string id_, Source& base_, variant<std::string, GeoJSON> urlOrGeoJSON_) - : Source::Impl(SourceType::GeoJSON, std::move(id_), base_), - urlOrGeoJSON(std::move(urlOrGeoJSON_)) { +GeoJSONSource::Impl::~Impl() = default; + +void GeoJSONSource::Impl::setURL(std::string url) { + urlOrGeoJSON = std::move(url); } -GeoJSONSource::Impl::~Impl() = default; +void GeoJSONSource::Impl::setGeoJSON(GeoJSON&& geoJSON) { + urlOrGeoJSON = std::move(geoJSON); +} void GeoJSONSource::Impl::load(FileSource& fileSource) { if (urlOrGeoJSON.is<GeoJSON>()) { @@ -94,9 +77,17 @@ void GeoJSONSource::Impl::load(FileSource& fileSource) { invalidateTiles(); - urlOrGeoJSON = parseGeoJSON(d); - loaded = true; + conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d); + if (!geoJSON) { + Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", geoJSON.error().message); + // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for + // tiles to load. + urlOrGeoJSON = GeoJSON { std::make_unique<GeoJSONVT>(std::vector<ProjectedFeature>()) }; + } else { + urlOrGeoJSON = std::move(*geoJSON); + } + loaded = true; observer->onSourceLoaded(base); } }); @@ -104,13 +95,13 @@ void GeoJSONSource::Impl::load(FileSource& fileSource) { Range<uint8_t> GeoJSONSource::Impl::getZoomRange() { assert(loaded); - return { 0, urlOrGeoJSON.get<GeoJSON>()->options.maxZoom }; + return { 0, urlOrGeoJSON.get<GeoJSON>().impl->options.maxZoom }; } std::unique_ptr<Tile> GeoJSONSource::Impl::createTile(const OverscaledTileID& tileID, - const UpdateParameters& parameters) { + const UpdateParameters& parameters) { assert(loaded); - return std::make_unique<GeoJSONTile>(tileID, base.getID(), parameters, *urlOrGeoJSON.get<GeoJSON>()); + return std::make_unique<GeoJSONTile>(tileID, base.getID(), parameters, *urlOrGeoJSON.get<GeoJSON>().impl); } } // namespace style diff --git a/src/mbgl/style/sources/geojson_source_impl.hpp b/src/mbgl/style/sources/geojson_source_impl.hpp index 350045b27c..e6e01c06e9 100644 --- a/src/mbgl/style/sources/geojson_source_impl.hpp +++ b/src/mbgl/style/sources/geojson_source_impl.hpp @@ -2,15 +2,8 @@ #include <mbgl/style/sources/geojson_source.hpp> #include <mbgl/style/source_impl.hpp> -#include <mbgl/util/rapidjson.hpp> #include <mbgl/util/variant.hpp> -namespace mapbox { -namespace geojsonvt { -class GeoJSONVT; -} // namespace geojsonvt -} // namespace mapbox - namespace mbgl { class AsyncRequest; @@ -19,15 +12,12 @@ namespace style { class GeoJSONSource::Impl : public Source::Impl { public: - using GeoJSON = std::unique_ptr<mapbox::geojsonvt::GeoJSONVT>; - - static std::unique_ptr<GeoJSONSource> parse(const std::string& id, const JSValue&); - static GeoJSON parseGeoJSON(const JSValue&); - - Impl(std::string id, Source&, - variant<std::string, GeoJSON> urlOrGeoJSON); + Impl(std::string id, Source&); ~Impl() final; + void setURL(std::string); + void setGeoJSON(GeoJSON&&); + void load(FileSource&) final; uint16_t getTileSize() const final { diff --git a/src/mbgl/style/sources/raster_source_impl.cpp b/src/mbgl/style/sources/raster_source_impl.cpp index a6e19b4757..b727651260 100644 --- a/src/mbgl/style/sources/raster_source_impl.cpp +++ b/src/mbgl/style/sources/raster_source_impl.cpp @@ -1,30 +1,9 @@ #include <mbgl/style/sources/raster_source_impl.hpp> #include <mbgl/tile/raster_tile.hpp> -#include <mbgl/platform/log.hpp> namespace mbgl { namespace style { -std::unique_ptr<RasterSource> RasterSource::Impl::parse(std::string id, const JSValue& value) { - optional<variant<std::string, Tileset>> urlOrTileset = TileSourceImpl::parseURLOrTileset(value); - if (!urlOrTileset) { - return nullptr; - } - - uint16_t tileSize = util::tileSize; - if (value.HasMember("tileSize")) { - const JSValue& tileSizeVal = value["tileSize"]; - if (tileSizeVal.IsNumber() && tileSizeVal.GetUint64() <= std::numeric_limits<uint16_t>::max()) { - tileSize = tileSizeVal.GetUint64(); - } else { - Log::Error(Event::ParseStyle, "invalid tileSize"); - return nullptr; - } - } - - return std::make_unique<RasterSource>(std::move(id), std::move(*urlOrTileset), tileSize); -} - RasterSource::Impl::Impl(std::string id_, Source& base_, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize_) diff --git a/src/mbgl/style/sources/raster_source_impl.hpp b/src/mbgl/style/sources/raster_source_impl.hpp index 2222b13082..6f34a050bb 100644 --- a/src/mbgl/style/sources/raster_source_impl.hpp +++ b/src/mbgl/style/sources/raster_source_impl.hpp @@ -8,8 +8,6 @@ namespace style { class RasterSource::Impl : public TileSourceImpl { public: - static std::unique_ptr<RasterSource> parse(std::string id, const JSValue&); - Impl(std::string id, Source&, variant<std::string, Tileset>, uint16_t tileSize); private: diff --git a/src/mbgl/style/sources/vector_source_impl.cpp b/src/mbgl/style/sources/vector_source_impl.cpp index 28e14f3e16..efe8afbbea 100644 --- a/src/mbgl/style/sources/vector_source_impl.cpp +++ b/src/mbgl/style/sources/vector_source_impl.cpp @@ -4,14 +4,6 @@ namespace mbgl { namespace style { -std::unique_ptr<VectorSource> VectorSource::Impl::parse(std::string id, const JSValue& value) { - optional<variant<std::string, Tileset>> urlOrTileset = TileSourceImpl::parseURLOrTileset(value); - if (!urlOrTileset) { - return nullptr; - } - return std::make_unique<VectorSource>(std::move(id), std::move(*urlOrTileset)); -} - VectorSource::Impl::Impl(std::string id_, Source& base_, variant<std::string, Tileset> urlOrTileset_) : TileSourceImpl(SourceType::Vector, std::move(id_), base_, std::move(urlOrTileset_), util::tileSize) { } diff --git a/src/mbgl/style/sources/vector_source_impl.hpp b/src/mbgl/style/sources/vector_source_impl.hpp index 4a6703e5c0..6726fa6955 100644 --- a/src/mbgl/style/sources/vector_source_impl.hpp +++ b/src/mbgl/style/sources/vector_source_impl.hpp @@ -8,8 +8,6 @@ namespace style { class VectorSource::Impl : public TileSourceImpl { public: - static std::unique_ptr<VectorSource> parse(std::string id, const JSValue&); - Impl(std::string id, Source&, variant<std::string, Tileset>); private: |