summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsheem Mamoowala <asheem.mamoowala@mapbox.com>2017-09-25 11:38:04 -0700
committerAsheem Mamoowala <asheem.mamoowala@mapbox.com>2017-10-10 16:51:48 -0700
commit1624a523105b709259e7222cf645598c342e0a9e (patch)
treef46fe9e5e9620d1e588a92b6ffa69675938e1239
parent08df6ebca0adbea5589545d96d2a9e94feeb148f (diff)
downloadqtlocation-mapboxgl-1624a523105b709259e7222cf645598c342e0a9e.tar.gz
Custom Tile
-rw-r--r--cmake/core-files.cmake2
-rw-r--r--src/mbgl/tile/custom_tile.cpp73
-rw-r--r--src/mbgl/tile/custom_tile.hpp33
3 files changed, 108 insertions, 0 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake
index 18d07cd20e..629a9a554a 100644
--- a/cmake/core-files.cmake
+++ b/cmake/core-files.cmake
@@ -495,6 +495,8 @@ set(MBGL_CORE_FILES
# tile
include/mbgl/tile/tile_id.hpp
+ src/mbgl/tile/custom_tile.cpp
+ src/mbgl/tile/custom_tile.hpp
src/mbgl/tile/geojson_tile.cpp
src/mbgl/tile/geojson_tile.hpp
src/mbgl/tile/geojson_tile_data.hpp
diff --git a/src/mbgl/tile/custom_tile.cpp b/src/mbgl/tile/custom_tile.cpp
new file mode 100644
index 0000000000..b7057d447e
--- /dev/null
+++ b/src/mbgl/tile/custom_tile.cpp
@@ -0,0 +1,73 @@
+#include <mbgl/tile/custom_tile.hpp>
+#include <mbgl/tile/geometry_tile_data.hpp>
+#include <mbgl/renderer/query.hpp>
+#include <mbgl/renderer/tile_parameters.hpp>
+#include <mbgl/style/filter_evaluator.hpp>
+#include <mbgl/util/logging.hpp>
+#include <mbgl/actor/scheduler.hpp>
+
+#include <mapbox/geojsonvt.hpp>
+
+namespace mbgl {
+
+CustomTile::CustomTile(const OverscaledTileID& overscaledTileID,
+ std::string sourceID_,
+ const TileParameters& parameters,
+ const style::GeoJSONOptions options_)
+ : GeometryTile(overscaledTileID, sourceID_, parameters),
+ options(options_),
+ actor(*Scheduler::GetCurrent(), std::bind(&CustomTile::setTileData, this, std::placeholders::_1, std::placeholders::_2)) {
+
+}
+
+void CustomTile::setTileData(const CanonicalTileID&, const style::FetchTileResult& result) {
+ if (result.is<style::Error>()) {
+ Log::Error(Event::Render, "FetchTile (%d, %d, %d) error: %s", id.canonical.z, id.canonical.x, id.canonical.y, result.get<style::Error>().message.c_str());
+ return;
+ }
+
+ auto geoJSON = result.get<mapbox::geojson::geojson>();
+ auto data = mapbox::geometry::feature_collection<int16_t>();
+ if (geoJSON.is<FeatureCollection>() && !geoJSON.get<FeatureCollection>().empty()) {
+ const double scale = util::EXTENT / options.tileSize;
+
+ mapbox::geojsonvt::Options vtOptions;
+ vtOptions.maxZoom = options.maxzoom;
+ vtOptions.extent = util::EXTENT;
+ vtOptions.buffer = std::round(scale * options.buffer);
+ vtOptions.tolerance = scale * options.tolerance;
+ auto geojsonVt = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(geoJSON, vtOptions);
+ data = geojsonVt->getTile(id.canonical.z, id.canonical.x, id.canonical.y).features;
+ }
+ setData(std::make_unique<GeoJSONTileData>(std::move(data)));
+}
+
+void CustomTile::setNecessity(Necessity) {}
+
+void CustomTile::querySourceFeatures(
+ std::vector<Feature>& result,
+ const SourceQueryOptions& queryOptions) {
+
+ // Ignore the sourceLayer, there is only one
+ auto layer = getData()->getLayer({});
+
+ if (layer) {
+ auto featureCount = layer->featureCount();
+ for (std::size_t i = 0; i < featureCount; i++) {
+ auto feature = layer->getFeature(i);
+
+ // Apply filter, if any
+ if (queryOptions.filter && !(*queryOptions.filter)(*feature)) {
+ continue;
+ }
+
+ result.push_back(convertFeature(*feature, id.canonical));
+ }
+ }
+}
+
+ActorRef<style::FetchTileCallback> CustomTile::fetchTileCallback() {
+ return actor.self();
+}
+
+} // namespace mbgl
diff --git a/src/mbgl/tile/custom_tile.hpp b/src/mbgl/tile/custom_tile.hpp
new file mode 100644
index 0000000000..66606d2224
--- /dev/null
+++ b/src/mbgl/tile/custom_tile.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <mbgl/tile/geometry_tile.hpp>
+#include <mbgl/util/feature.hpp>
+#include <mbgl/style/sources/custom_vector_source.hpp>
+#include <mbgl/style/sources/geojson_source.hpp>
+
+namespace mbgl {
+
+class TileParameters;
+
+class CustomTile: public GeometryTile {
+public:
+ CustomTile(const OverscaledTileID&,
+ std::string sourceID,
+ const TileParameters&,
+ const style::GeoJSONOptions);
+
+ void setTileData(const CanonicalTileID& tileID, const style::FetchTileResult& result);
+
+ void setNecessity(Necessity) final;
+
+ void querySourceFeatures(
+ std::vector<Feature>& result,
+ const SourceQueryOptions&) override;
+
+ ActorRef<style::FetchTileCallback> fetchTileCallback();
+private:
+ const style::GeoJSONOptions options;
+ Actor<style::FetchTileCallback> actor;
+};
+
+} // namespace mbgl