From 8c498cd36c07b1019e5ddb60d5f9f8872f036e25 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Wed, 30 Oct 2019 11:30:54 +0200 Subject: [core] Introduce and apply GeoJSONData::create() API --- src/mbgl/style/sources/geojson_source.cpp | 20 +++++++------- src/mbgl/style/sources/geojson_source_impl.cpp | 37 ++++++++++++++------------ src/mbgl/style/sources/geojson_source_impl.hpp | 16 ++--------- 3 files changed, 33 insertions(+), 40 deletions(-) (limited to 'src/mbgl/style') diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index 5171c7c8d9..79e7c23459 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -32,8 +32,12 @@ void GeoJSONSource::setURL(const std::string& url_) { } void GeoJSONSource::setGeoJSON(const mapbox::geojson::geojson& geoJSON) { + setGeoJSONData(GeoJSONData::create(geoJSON, impl().getOptions())); +} + +void GeoJSONSource::setGeoJSONData(std::shared_ptr geoJSONData) { req.reset(); - baseImpl = makeMutable(impl(), geoJSON); + baseImpl = makeMutable(impl(), std::move(geoJSONData)); observer->onSourceChanged(*this); } @@ -62,17 +66,15 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { *this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); } else { conversion::Error error; - optional geoJSON = conversion::convertJSON(*res.data, error); - if (!geoJSON) { + std::shared_ptr geoJSONData; + if (optional geoJSON = conversion::convertJSON(*res.data, error)) { + geoJSONData = GeoJSONData::create(*geoJSON, impl().getOptions()); + } else { + // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for tiles to load. Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", error.message.c_str()); - // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for - // tiles to load. - baseImpl = makeMutable(impl(), GeoJSON{ FeatureCollection{} }); - } else { - baseImpl = makeMutable(impl(), *geoJSON); } - + baseImpl = makeMutable(impl(), std::move(geoJSONData)); loaded = true; observer->onSourceLoaded(*this); } diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index 8067b1ab1d..468deb6134 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -82,13 +82,8 @@ T evaluateFeature(const mapbox::feature::feature& f, return T(); } -GeoJSONSource::Impl::Impl(std::string id_, optional options_) - : Source::Impl(SourceType::GeoJSON, std::move(id_)) { - options = options_ ? std::move(*options_) : GeoJSONOptions{}; -} - -GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) - : Source::Impl(other), options(other.options) { +// static +std::shared_ptr GeoJSONData::create(const GeoJSON& geoJSON, const GeoJSONOptions& options) { constexpr double scale = util::EXTENT / util::tileSize; if (options.cluster && geoJSON.is>() && !geoJSON.get>().empty()) { @@ -116,19 +111,27 @@ GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) toReturn[p.first] = evaluateFeature(feature, p.second.second, accumulated); } }; - data = std::make_shared( - geoJSON.get>(), clusterOptions); - } else { - mapbox::geojsonvt::Options vtOptions; - vtOptions.maxZoom = options.maxzoom; - vtOptions.extent = util::EXTENT; - vtOptions.buffer = ::round(scale * options.buffer); - vtOptions.tolerance = scale * options.tolerance; - vtOptions.lineMetrics = options.lineMetrics; - data = std::make_shared(geoJSON, vtOptions); + return std::make_shared(geoJSON.get>(), + clusterOptions); } + + mapbox::geojsonvt::Options vtOptions; + vtOptions.maxZoom = options.maxzoom; + vtOptions.extent = util::EXTENT; + vtOptions.buffer = ::round(scale * options.buffer); + vtOptions.tolerance = scale * options.tolerance; + vtOptions.lineMetrics = options.lineMetrics; + return std::make_shared(geoJSON, vtOptions); } +GeoJSONSource::Impl::Impl(std::string id_, optional options_) + : Source::Impl(SourceType::GeoJSON, std::move(id_)) { + options = options_ ? std::move(*options_) : GeoJSONOptions{}; +} + +GeoJSONSource::Impl::Impl(const GeoJSONSource::Impl& other, std::shared_ptr data_) + : Source::Impl(other), options(other.options), data(std::move(data_)) {} + GeoJSONSource::Impl::~Impl() = default; Range GeoJSONSource::Impl::getZoomRange() const { diff --git a/src/mbgl/style/sources/geojson_source_impl.hpp b/src/mbgl/style/sources/geojson_source_impl.hpp index 26b9d95a39..da2673a38c 100644 --- a/src/mbgl/style/sources/geojson_source_impl.hpp +++ b/src/mbgl/style/sources/geojson_source_impl.hpp @@ -11,27 +11,15 @@ class CanonicalTileID; namespace style { -class GeoJSONData { -public: - virtual ~GeoJSONData() = default; - virtual mapbox::feature::feature_collection getTile(const CanonicalTileID&) = 0; - - // SuperclusterData - virtual mapbox::feature::feature_collection getChildren(const std::uint32_t) = 0; - virtual mapbox::feature::feature_collection getLeaves(const std::uint32_t, - const std::uint32_t limit = 10u, - const std::uint32_t offset = 0u) = 0; - virtual std::uint8_t getClusterExpansionZoom(std::uint32_t) = 0; -}; - class GeoJSONSource::Impl : public Source::Impl { public: Impl(std::string id, optional); - Impl(const GeoJSONSource::Impl&, const GeoJSON&); + Impl(const GeoJSONSource::Impl&, std::shared_ptr); ~Impl() final; Range getZoomRange() const; std::weak_ptr getData() const; + const GeoJSONOptions& getOptions() const { return options; } optional getAttribution() const final; -- cgit v1.2.1