From 1624a523105b709259e7222cf645598c342e0a9e Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Mon, 25 Sep 2017 11:38:04 -0700 Subject: Custom Tile --- cmake/core-files.cmake | 2 ++ src/mbgl/tile/custom_tile.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++ src/mbgl/tile/custom_tile.hpp | 33 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/mbgl/tile/custom_tile.cpp create mode 100644 src/mbgl/tile/custom_tile.hpp 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 +#include +#include +#include +#include +#include +#include + +#include + +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()) { + Log::Error(Event::Render, "FetchTile (%d, %d, %d) error: %s", id.canonical.z, id.canonical.x, id.canonical.y, result.get().message.c_str()); + return; + } + + auto geoJSON = result.get(); + auto data = mapbox::geometry::feature_collection(); + if (geoJSON.is() && !geoJSON.get().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(geoJSON, vtOptions); + data = geojsonVt->getTile(id.canonical.z, id.canonical.x, id.canonical.y).features; + } + setData(std::make_unique(std::move(data))); +} + +void CustomTile::setNecessity(Necessity) {} + +void CustomTile::querySourceFeatures( + std::vector& 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 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 +#include +#include +#include + +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& result, + const SourceQueryOptions&) override; + + ActorRef fetchTileCallback(); +private: + const style::GeoJSONOptions options; + Actor actor; +}; + +} // namespace mbgl -- cgit v1.2.1