From 3f0c89d633a5056006557ad5f4b9e446807d00ee Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 20 Apr 2017 17:11:50 -0700 Subject: [core] Refactor Source::*Impls into RenderSources and TilePyramid --- src/mbgl/style/sources/geojson_source_impl.cpp | 89 +++++++++++++------------- 1 file changed, 45 insertions(+), 44 deletions(-) (limited to 'src/mbgl/style/sources/geojson_source_impl.cpp') diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index 6431d5faa4..08ed95ea3f 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -1,10 +1,12 @@ -#include -#include +#include #include #include -#include -#include +#include +#include +#include #include +#include +#include #include #include @@ -31,6 +33,34 @@ optional convertGeoJSON(const JSValue& value, Error& error) { } } // namespace conversion +class GeoJSONVTData : public GeoJSONData { +public: + GeoJSONVTData(const GeoJSON& geoJSON, + const mapbox::geojsonvt::Options& options) + : impl(geoJSON, options) {} + + mapbox::geometry::feature_collection getTile(const CanonicalTileID& tileID) final { + return impl.getTile(tileID.z, tileID.x, tileID.y).features; + } + +private: + mapbox::geojsonvt::GeoJSONVT impl; +}; + +class SuperclusterData : public GeoJSONData { +public: + SuperclusterData(const mapbox::geometry::feature_collection& features, + const mapbox::supercluster::Options& options) + : impl(features, options) {} + + mapbox::geometry::feature_collection getTile(const CanonicalTileID& tileID) final { + return impl.getTile(tileID.z, tileID.x, tileID.y); + } + +private: + mapbox::supercluster::Supercluster impl; +}; + GeoJSONSource::Impl::Impl(std::string id_, Source& base_, const GeoJSONOptions options_) : Source::Impl(SourceType::GeoJSON, std::move(id_), base_), options(options_) { } @@ -52,18 +82,14 @@ optional GeoJSONSource::Impl::getURL() const { return url; } - void GeoJSONSource::Impl::setGeoJSON(const GeoJSON& geoJSON) { req.reset(); _setGeoJSON(geoJSON); } -// Private implementation void GeoJSONSource::Impl::_setGeoJSON(const GeoJSON& geoJSON) { double scale = util::EXTENT / util::tileSize; - cache.clear(); - if (options.cluster && geoJSON.is>() && !geoJSON.get>().empty()) { @@ -71,35 +97,15 @@ void GeoJSONSource::Impl::_setGeoJSON(const GeoJSON& geoJSON) { clusterOptions.maxZoom = options.clusterMaxZoom; clusterOptions.extent = util::EXTENT; clusterOptions.radius = std::round(scale * options.clusterRadius); - - const auto& features = geoJSON.get>(); - geoJSONOrSupercluster = - std::make_unique(features, clusterOptions); + data = std::make_unique( + geoJSON.get>(), clusterOptions); } else { mapbox::geojsonvt::Options vtOptions; vtOptions.maxZoom = options.maxzoom; vtOptions.extent = util::EXTENT; vtOptions.buffer = std::round(scale * options.buffer); vtOptions.tolerance = scale * options.tolerance; - geoJSONOrSupercluster = std::make_unique(geoJSON, vtOptions); - } - - for (auto const &item : tiles) { - GeoJSONTile* geoJSONTile = static_cast(item.second.get()); - setTileData(*geoJSONTile, geoJSONTile->id); - } -} - -void GeoJSONSource::Impl::setTileData(GeoJSONTile& tile, const OverscaledTileID& tileID) { - if (geoJSONOrSupercluster.is()) { - tile.updateData(geoJSONOrSupercluster.get()->getTile(tileID.canonical.z, - tileID.canonical.x, - tileID.canonical.y).features); - } else { - assert(geoJSONOrSupercluster.is()); - tile.updateData(geoJSONOrSupercluster.get()->getTile(tileID.canonical.z, - tileID.canonical.x, - tileID.canonical.y)); + data = std::make_unique(geoJSON, vtOptions); } } @@ -135,8 +141,6 @@ void GeoJSONSource::Impl::loadDescription(FileSource& fileSource) { return; } - invalidateTiles(); - conversion::Error error; optional geoJSON = conversion::convertGeoJSON(d, error); if (!geoJSON) { @@ -155,19 +159,16 @@ void GeoJSONSource::Impl::loadDescription(FileSource& fileSource) { }); } -optional> GeoJSONSource::Impl::getZoomRange() const { - if (loaded) { - return { { 0, options.maxzoom }}; - } - return {}; +std::unique_ptr GeoJSONSource::Impl::createRenderSource() const { + return std::make_unique(*this); +} + +Range GeoJSONSource::Impl::getZoomRange() const { + return { 0, options.maxzoom }; } -std::unique_ptr GeoJSONSource::Impl::createTile(const OverscaledTileID& tileID, - const UpdateParameters& parameters) { - assert(loaded); - auto tilePointer = std::make_unique(tileID, base.getID(), parameters); - setTileData(*tilePointer.get(), tileID); - return std::move(tilePointer); +GeoJSONData* GeoJSONSource::Impl::getData() const { + return data.get(); } } // namespace style -- cgit v1.2.1