summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-10-30 11:30:54 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-10-31 10:24:55 +0200
commit8c498cd36c07b1019e5ddb60d5f9f8872f036e25 (patch)
treeea5c5578ae0433c9c3fff9da42c221d99c72ddfc
parent6900ac9837c6981b2a3f8d389ac09c0fca56b749 (diff)
downloadqtlocation-mapboxgl-8c498cd36c07b1019e5ddb60d5f9f8872f036e25.tar.gz
[core] Introduce and apply GeoJSONData::create() API
-rw-r--r--include/mbgl/style/sources/geojson_source.hpp16
-rw-r--r--src/mbgl/style/sources/geojson_source.cpp20
-rw-r--r--src/mbgl/style/sources/geojson_source_impl.cpp37
-rw-r--r--src/mbgl/style/sources/geojson_source_impl.hpp16
4 files changed, 49 insertions, 40 deletions
diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp
index a256ad6f15..c8dc65b912 100644
--- a/include/mbgl/style/sources/geojson_source.hpp
+++ b/include/mbgl/style/sources/geojson_source.hpp
@@ -2,6 +2,7 @@
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/source.hpp>
+#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/geojson.hpp>
#include <mbgl/util/optional.hpp>
@@ -34,6 +35,20 @@ struct GeoJSONOptions {
using ClusterProperties = std::unordered_map<std::string, ClusterExpression>;
ClusterProperties clusterProperties;
};
+class GeoJSONData {
+public:
+ static std::shared_ptr<GeoJSONData> create(const GeoJSON&, const GeoJSONOptions&);
+
+ virtual ~GeoJSONData() = default;
+ virtual mapbox::feature::feature_collection<int16_t> getTile(const CanonicalTileID&) = 0;
+
+ // SuperclusterData
+ virtual mapbox::feature::feature_collection<double> getChildren(const std::uint32_t) = 0;
+ virtual mapbox::feature::feature_collection<double> 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 final : public Source {
public:
@@ -42,6 +57,7 @@ public:
void setURL(const std::string& url);
void setGeoJSON(const GeoJSON&);
+ void setGeoJSONData(std::shared_ptr<GeoJSONData>);
optional<std::string> getURL() const;
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> geoJSONData) {
req.reset();
- baseImpl = makeMutable<Impl>(impl(), geoJSON);
+ baseImpl = makeMutable<Impl>(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> geoJSON = conversion::convertJSON<GeoJSON>(*res.data, error);
- if (!geoJSON) {
+ std::shared_ptr<GeoJSONData> geoJSONData;
+ if (optional<GeoJSON> geoJSON = conversion::convertJSON<GeoJSON>(*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>(impl(), GeoJSON{ FeatureCollection{} });
- } else {
- baseImpl = makeMutable<Impl>(impl(), *geoJSON);
}
-
+ baseImpl = makeMutable<Impl>(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<double>& f,
return T();
}
-GeoJSONSource::Impl::Impl(std::string id_, optional<GeoJSONOptions> 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> GeoJSONData::create(const GeoJSON& geoJSON, const GeoJSONOptions& options) {
constexpr double scale = util::EXTENT / util::tileSize;
if (options.cluster && geoJSON.is<mapbox::feature::feature_collection<double>>() &&
!geoJSON.get<mapbox::feature::feature_collection<double>>().empty()) {
@@ -116,19 +111,27 @@ GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON)
toReturn[p.first] = evaluateFeature<Value>(feature, p.second.second, accumulated);
}
};
- data = std::make_shared<SuperclusterData>(
- geoJSON.get<mapbox::feature::feature_collection<double>>(), 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<GeoJSONVTData>(geoJSON, vtOptions);
+ return std::make_shared<SuperclusterData>(geoJSON.get<mapbox::feature::feature_collection<double>>(),
+ 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<GeoJSONVTData>(geoJSON, vtOptions);
}
+GeoJSONSource::Impl::Impl(std::string id_, optional<GeoJSONOptions> options_)
+ : Source::Impl(SourceType::GeoJSON, std::move(id_)) {
+ options = options_ ? std::move(*options_) : GeoJSONOptions{};
+}
+
+GeoJSONSource::Impl::Impl(const GeoJSONSource::Impl& other, std::shared_ptr<GeoJSONData> data_)
+ : Source::Impl(other), options(other.options), data(std::move(data_)) {}
+
GeoJSONSource::Impl::~Impl() = default;
Range<uint8_t> 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<int16_t> getTile(const CanonicalTileID&) = 0;
-
- // SuperclusterData
- virtual mapbox::feature::feature_collection<double> getChildren(const std::uint32_t) = 0;
- virtual mapbox::feature::feature_collection<double> 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<GeoJSONOptions>);
- Impl(const GeoJSONSource::Impl&, const GeoJSON&);
+ Impl(const GeoJSONSource::Impl&, std::shared_ptr<GeoJSONData>);
~Impl() final;
Range<uint8_t> getZoomRange() const;
std::weak_ptr<GeoJSONData> getData() const;
+ const GeoJSONOptions& getOptions() const { return options; }
optional<std::string> getAttribution() const final;