From 517659b1b44a4e31e1035d4da9c444380454ad58 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Sat, 4 Mar 2017 18:44:33 -0800 Subject: [core] query source features --- src/mbgl/style/source.cpp | 4 ++++ src/mbgl/style/source_impl.cpp | 18 ++++++++++++++++++ src/mbgl/style/source_impl.hpp | 3 +++ src/mbgl/tile/geojson_tile.cpp | 23 +++++++++++++++++++++++ src/mbgl/tile/geojson_tile.hpp | 4 ++++ src/mbgl/tile/geometry_tile.cpp | 36 +++++++++++++++++++++++++++++++++++- src/mbgl/tile/geometry_tile.hpp | 10 ++++++++++ src/mbgl/tile/tile.cpp | 5 +++++ src/mbgl/tile/tile.hpp | 5 +++++ 9 files changed, 107 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index cfb268006b..25c06024d6 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -17,6 +17,10 @@ const std::string& Source::getID() const { optional Source::getAttribution() const { return baseImpl->getAttribution(); } + +std::vector Source::querySourceFeatures(const SourceQueryOptions& options) { + return baseImpl->querySourceFeatures(options); +} } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/source_impl.cpp b/src/mbgl/style/source_impl.cpp index f83579d0d0..1f8301629f 100644 --- a/src/mbgl/style/source_impl.cpp +++ b/src/mbgl/style/source_impl.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -258,6 +259,23 @@ std::unordered_map> Source::Impl::queryRendere return result; } +std::vector Source::Impl::querySourceFeatures(const SourceQueryOptions& options) { + + // Only VectorSource and GeoJSON source supported + if (type != SourceType::GeoJSON && type != SourceType::Vector) { + Log::Warning(Event::General, "Source type not supported"); + return {}; + } + + std::vector result; + + for (const auto& pair : tiles) { + pair.second->querySourceFeatures(result, options); + } + + return result; +} + void Source::Impl::setCacheSize(size_t size) { cache.setSize(size); } diff --git a/src/mbgl/style/source_impl.hpp b/src/mbgl/style/source_impl.hpp index 6e16a31e5a..b9707edaa7 100644 --- a/src/mbgl/style/source_impl.hpp +++ b/src/mbgl/style/source_impl.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -70,6 +71,8 @@ public: const TransformState& transformState, const RenderedQueryOptions& options) const; + std::vector querySourceFeatures(const SourceQueryOptions&); + void setCacheSize(size_t); void onLowMemory(); diff --git a/src/mbgl/tile/geojson_tile.cpp b/src/mbgl/tile/geojson_tile.cpp index 85d8b75619..5a2a72f50b 100644 --- a/src/mbgl/tile/geojson_tile.cpp +++ b/src/mbgl/tile/geojson_tile.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -90,5 +91,27 @@ void GeoJSONTile::updateData(const mapbox::geometry::feature_collection } void GeoJSONTile::setNecessity(Necessity) {} + +void GeoJSONTile::querySourceFeatures( + std::vector& result, + const style::SourceQueryOptions& options) { + + // 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 (options.filter && !(*options.filter)(*feature)) { + continue; + } + + result.push_back(convertFeature(*feature, id.canonical)); + } + } +} } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile.hpp b/src/mbgl/tile/geojson_tile.hpp index 6ddc6ea482..e1d269a9a7 100644 --- a/src/mbgl/tile/geojson_tile.hpp +++ b/src/mbgl/tile/geojson_tile.hpp @@ -18,6 +18,10 @@ public: void updateData(const mapbox::geometry::feature_collection&); void setNecessity(Necessity) final; + + void querySourceFeatures( + std::vector& result, + const style::SourceQueryOptions&) override; }; } // namespace mbgl diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index 01f054bdf4..37787df9ea 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -12,8 +12,11 @@ #include #include #include -#include #include +#include +#include +#include +#include namespace mbgl { @@ -158,4 +161,35 @@ void GeometryTile::queryRenderedFeatures( collisionTile.get()); } +void GeometryTile::querySourceFeatures( + std::vector& result, + const style::SourceQueryOptions& options) { + + // No source layers, specified, nothing to do + if (!options.sourceLayers) { + Log::Warning(Event::General, "At least one sourceLayer required"); + return; + } + + for (auto sourceLayer : *options.sourceLayers) { + // Go throught all sourceLayers, if any + // to gather all the features + auto layer = data->getLayer(sourceLayer); + + 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 (options.filter && !(*options.filter)(*feature)) { + continue; + } + + result.push_back(convertFeature(*feature, id.canonical)); + } + } + } +} + } // namespace mbgl diff --git a/src/mbgl/tile/geometry_tile.hpp b/src/mbgl/tile/geometry_tile.hpp index 538ea4fbd2..cabe193467 100644 --- a/src/mbgl/tile/geometry_tile.hpp +++ b/src/mbgl/tile/geometry_tile.hpp @@ -21,6 +21,7 @@ namespace style { class Style; class Layer; class UpdateParameters; +class SourceQueryOptions; } // namespace style class GeometryTile : public Tile { @@ -46,6 +47,10 @@ public: const TransformState&, const RenderedQueryOptions& options) override; + void querySourceFeatures( + std::vector& result, + const style::SourceQueryOptions&) override; + void cancel() override; class LayoutResult { @@ -66,6 +71,11 @@ public: void onPlacement(PlacementResult); void onError(std::exception_ptr); + +protected: + const GeometryTileData* getData() { + return data.get(); + } private: const std::string sourceID; diff --git a/src/mbgl/tile/tile.cpp b/src/mbgl/tile/tile.cpp index 2f347fb950..4fb8331aeb 100644 --- a/src/mbgl/tile/tile.cpp +++ b/src/mbgl/tile/tile.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace mbgl { @@ -35,4 +36,8 @@ void Tile::queryRenderedFeatures( const TransformState&, const RenderedQueryOptions&) {} +void Tile::querySourceFeatures( + std::vector&, + const style::SourceQueryOptions&) {} + } // namespace mbgl diff --git a/src/mbgl/tile/tile.hpp b/src/mbgl/tile/tile.hpp index b335d1a5d5..613b15f36c 100644 --- a/src/mbgl/tile/tile.hpp +++ b/src/mbgl/tile/tile.hpp @@ -25,6 +25,7 @@ class RenderedQueryOptions; namespace style { class Layer; +class SourceQueryOptions; } // namespace style class Tile : private util::noncopyable { @@ -58,6 +59,10 @@ public: const TransformState&, const RenderedQueryOptions& options); + virtual void querySourceFeatures( + std::vector& result, + const style::SourceQueryOptions&); + void setTriedOptional(); // Returns true when the tile source has received a first response, regardless of whether a load -- cgit v1.2.1