From c2b00378b78b55d50968a9b11ed75bb4edf62ec9 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 25 Apr 2017 12:56:59 -0700 Subject: [PATCH] [all] Push querySourceFeatures back out to Map Once Source and RendererSource are split, Source will no longer have access to tiles. --- cmake/core-files.cmake | 1 - include/mbgl/map/map.hpp | 1 + include/mbgl/map/query.hpp | 11 +++++++++ include/mbgl/style/query.hpp | 24 ------------------- include/mbgl/style/source.hpp | 3 --- .../src/style/sources/geojson_source.cpp | 6 +++-- .../src/style/sources/vector_source.cpp | 7 +++--- platform/darwin/src/MGLShapeSource.mm | 5 +++- platform/darwin/src/MGLSource.mm | 4 ++++ platform/darwin/src/MGLSource_Private.h | 8 +++++++ platform/darwin/src/MGLVectorSource.mm | 7 +++++- src/mbgl/map/map.cpp | 11 ++++++++- src/mbgl/style/source.cpp | 4 ---- src/mbgl/style/source_impl.cpp | 1 - src/mbgl/style/source_impl.hpp | 2 +- src/mbgl/tile/geojson_tile.cpp | 4 ++-- src/mbgl/tile/geojson_tile.hpp | 2 +- src/mbgl/tile/geometry_tile.cpp | 3 +-- src/mbgl/tile/geometry_tile.hpp | 4 ++-- src/mbgl/tile/tile.cpp | 3 +-- src/mbgl/tile/tile.hpp | 4 ++-- test/api/query.test.cpp | 14 +++++------ test/tile/vector_tile.test.cpp | 2 +- 23 files changed, 70 insertions(+), 61 deletions(-) delete mode 100644 include/mbgl/style/query.hpp diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index d28187210..d5f7ee0cc 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -286,7 +286,6 @@ set(MBGL_CORE_FILES include/mbgl/style/light.hpp include/mbgl/style/position.hpp include/mbgl/style/property_value.hpp - include/mbgl/style/query.hpp include/mbgl/style/source.hpp include/mbgl/style/transition_options.hpp include/mbgl/style/types.hpp diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 84ea3104d..1c0c2c544 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -194,6 +194,7 @@ public: // Feature queries std::vector queryRenderedFeatures(const ScreenCoordinate&, const RenderedQueryOptions& options = {}); std::vector queryRenderedFeatures(const ScreenBox&, const RenderedQueryOptions& options = {}); + std::vector querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options = {}); AnnotationIDs queryPointAnnotations(const ScreenBox&); diff --git a/include/mbgl/map/query.hpp b/include/mbgl/map/query.hpp index 827424d21..9fac60d71 100644 --- a/include/mbgl/map/query.hpp +++ b/include/mbgl/map/query.hpp @@ -19,4 +19,15 @@ public: optional filter; }; +/** + * Options for query source features + */ +class SourceQueryOptions { +public: + // Required for VectorSource, ignored for GeoJSONSource + optional> sourceLayers; + + optional filter; +}; + } diff --git a/include/mbgl/style/query.hpp b/include/mbgl/style/query.hpp deleted file mode 100644 index 8cb2545ab..000000000 --- a/include/mbgl/style/query.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -namespace mbgl { -namespace style { - -/** - * Options for query source features - */ -class SourceQueryOptions { -public: - // Required for VectorSource, ignored for GeoJSONSource - optional> sourceLayers; - - optional filter; -}; - -} // namespace style -} // namespace mbgl diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index f2dfb6a89..e49f1fc27 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -59,8 +58,6 @@ public: optional getAttribution() const; optional> getZoomRange() const; - std::vector querySourceFeatures(const SourceQueryOptions& options = {}); - // Private implementation class Impl; const std::unique_ptr baseImpl; diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp index c201cdade..9b56446e3 100644 --- a/platform/android/src/style/sources/geojson_source.cpp +++ b/platform/android/src/style/sources/geojson_source.cpp @@ -102,8 +102,10 @@ namespace android { using namespace mbgl::android::conversion; using namespace mbgl::android::geojson; - auto filter = toFilter(env, jfilter); - auto features = source.querySourceFeatures({ {}, filter }); + std::vector features; + if (map) { + features = map->querySourceFeatures(source.getID(), { {}, toFilter(env, jfilter) }); + } return *convert>, std::vector>(env, features); } diff --git a/platform/android/src/style/sources/vector_source.cpp b/platform/android/src/style/sources/vector_source.cpp index 53aa14445..a8f74df14 100644 --- a/platform/android/src/style/sources/vector_source.cpp +++ b/platform/android/src/style/sources/vector_source.cpp @@ -40,9 +40,10 @@ namespace android { using namespace mbgl::android::conversion; using namespace mbgl::android::geojson; - mbgl::optional> sourceLayerIds = { toVector(env, jSourceLayerIds) }; - auto filter = toFilter(env, jfilter); - auto features = source.querySourceFeatures({ sourceLayerIds, filter }); + std::vector features; + if (map) { + features = map->querySourceFeatures(source.getID(), { toVector(env, jSourceLayerIds), toFilter(env, jfilter) }); + } return *convert>, std::vector>(env, features); } diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index 15437ebed..023a81bba 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -95,7 +95,10 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh optionalFilter = predicate.mgl_filter; } - std::vector features = self.rawSource->querySourceFeatures({ {}, optionalFilter }); + std::vector features; + if (self.mapView) { + features = self.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { {}, optionalFilter }); + } return MGLFeaturesFromMBGLFeatures(features); } diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm index 59c2ae13e..eb859ba2c 100644 --- a/platform/darwin/src/MGLSource.mm +++ b/platform/darwin/src/MGLSource.mm @@ -10,6 +10,8 @@ // special internal source types like mbgl::AnnotationSource. @property (nonatomic, readonly) mbgl::style::Source *rawSource; +@property (nonatomic, readonly, weak) MGLMapView *mapView; + @end @implementation MGLSource { @@ -48,12 +50,14 @@ "to the style more than once is invalid.", self, mapView.style]; } + _mapView = mapView; mapView.mbglMap->addSource(std::move(_pendingSource)); } - (void)removeFromMapView:(MGLMapView *)mapView { if (self.rawSource == mapView.mbglMap->getSource(self.identifier.UTF8String)) { _pendingSource = mapView.mbglMap->removeSource(self.identifier.UTF8String); + _mapView = nil; } } diff --git a/platform/darwin/src/MGLSource_Private.h b/platform/darwin/src/MGLSource_Private.h index 6f86e4800..91bfac639 100644 --- a/platform/darwin/src/MGLSource_Private.h +++ b/platform/darwin/src/MGLSource_Private.h @@ -43,6 +43,14 @@ struct SourceWrapper { */ @property (nonatomic, readonly) mbgl::style::Source *rawSource; +/** + The map view whose style currently contains the source. + + If the source is not currently part of any map view’s style, this property is + set to `nil`. + */ +@property (nonatomic, readonly, weak) MGLMapView *mapView; + /** Adds the mbgl source that this object represents to the mbgl map. diff --git a/platform/darwin/src/MGLVectorSource.mm b/platform/darwin/src/MGLVectorSource.mm index afce3bad4..5e9f4f4a6 100644 --- a/platform/darwin/src/MGLVectorSource.mm +++ b/platform/darwin/src/MGLVectorSource.mm @@ -3,9 +3,11 @@ #import "MGLFeature_Private.h" #import "MGLSource_Private.h" #import "MGLTileSource_Private.h" +#import "MGLMapView_Private.h" #import "NSPredicate+MGLAdditions.h" #import "NSURL+MGLAdditions.h" +#include #include @interface MGLVectorSource () @@ -59,7 +61,10 @@ optionalFilter = predicate.mgl_filter; } - std::vector features = self.rawSource->querySourceFeatures({ optionalSourceLayerIDs, optionalFilter }); + std::vector features; + if (self.mapView) { + features = self.mapView.mbglMap->querySourceFeatures(self.rawSource->getID(), { optionalSourceLayerIDs, optionalFilter }); + } return MGLFeaturesFromMBGLFeatures(features); } diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index b5a1af172..696dca187 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -868,6 +868,15 @@ std::vector Map::queryRenderedFeatures(const ScreenBox& box, const Rend ); } +std::vector Map::querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options) { + if (!impl->style) return {}; + + const style::Source* source = impl->style->getSource(sourceID); + if (!source) return {}; + + return source->baseImpl->querySourceFeatures(options); +} + AnnotationIDs Map::queryPointAnnotations(const ScreenBox& box) { RenderedQueryOptions options; options.layerIDs = {{ AnnotationManager::PointLayerID }}; diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index f10fc6916..51863f8eb 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -22,9 +22,5 @@ optional> Source::getZoomRange() const { return baseImpl->getZoomRange(); } -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 7a65dd1de..19138bd78 100644 --- a/src/mbgl/style/source_impl.cpp +++ b/src/mbgl/style/source_impl.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include diff --git a/src/mbgl/style/source_impl.hpp b/src/mbgl/style/source_impl.hpp index 387428ec5..132d1b97f 100644 --- a/src/mbgl/style/source_impl.hpp +++ b/src/mbgl/style/source_impl.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/mbgl/tile/geojson_tile.cpp b/src/mbgl/tile/geojson_tile.cpp index 00e10d148..f989bc2ce 100644 --- a/src/mbgl/tile/geojson_tile.cpp +++ b/src/mbgl/tile/geojson_tile.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -98,7 +98,7 @@ void GeoJSONTile::setNecessity(Necessity) {} void GeoJSONTile::querySourceFeatures( std::vector& result, - const style::SourceQueryOptions& options) { + const SourceQueryOptions& options) { // Ignore the sourceLayer, there is only one auto layer = getData()->getLayer({}); diff --git a/src/mbgl/tile/geojson_tile.hpp b/src/mbgl/tile/geojson_tile.hpp index e1d269a9a..f8cde7510 100644 --- a/src/mbgl/tile/geojson_tile.hpp +++ b/src/mbgl/tile/geojson_tile.hpp @@ -21,7 +21,7 @@ public: void querySourceFeatures( std::vector& result, - const style::SourceQueryOptions&) override; + const SourceQueryOptions&) override; }; } // namespace mbgl diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index 6b768d9d5..6ac4a2e3a 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include @@ -191,7 +190,7 @@ void GeometryTile::queryRenderedFeatures( void GeometryTile::querySourceFeatures( std::vector& result, - const style::SourceQueryOptions& options) { + const SourceQueryOptions& options) { // Data not yet available if (!data) { diff --git a/src/mbgl/tile/geometry_tile.hpp b/src/mbgl/tile/geometry_tile.hpp index 8aedd6706..227e774e9 100644 --- a/src/mbgl/tile/geometry_tile.hpp +++ b/src/mbgl/tile/geometry_tile.hpp @@ -19,11 +19,11 @@ class GeometryTileData; class FeatureIndex; class CollisionTile; class RenderLayer; +class SourceQueryOptions; namespace style { class Style; class UpdateParameters; -class SourceQueryOptions; } // namespace style class GeometryTile : public Tile, public GlyphRequestor, IconRequestor { @@ -58,7 +58,7 @@ public: void querySourceFeatures( std::vector& result, - const style::SourceQueryOptions&) override; + const SourceQueryOptions&) override; void cancel() override; diff --git a/src/mbgl/tile/tile.cpp b/src/mbgl/tile/tile.cpp index 4fb8331ae..0adc151a6 100644 --- a/src/mbgl/tile/tile.cpp +++ b/src/mbgl/tile/tile.cpp @@ -4,7 +4,6 @@ #include #include #include -#include namespace mbgl { @@ -38,6 +37,6 @@ void Tile::queryRenderedFeatures( void Tile::querySourceFeatures( std::vector&, - const style::SourceQueryOptions&) {} + const SourceQueryOptions&) {} } // namespace mbgl diff --git a/src/mbgl/tile/tile.hpp b/src/mbgl/tile/tile.hpp index 6c2c78967..795fd6214 100644 --- a/src/mbgl/tile/tile.hpp +++ b/src/mbgl/tile/tile.hpp @@ -22,10 +22,10 @@ class TransformState; class TileObserver; class PlacementConfig; class RenderedQueryOptions; +class SourceQueryOptions; class RenderLayer; namespace style { -class SourceQueryOptions; } // namespace style class Tile : private util::noncopyable { @@ -60,7 +60,7 @@ public: virtual void querySourceFeatures( std::vector& result, - const style::SourceQueryOptions&); + const SourceQueryOptions&); void setTriedOptional(); diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp index 77dc09548..c509753d2 100644 --- a/test/api/query.test.cpp +++ b/test/api/query.test.cpp @@ -86,7 +86,7 @@ TEST(Query, QueryRenderedFeaturesFilter) { TEST(Query, QuerySourceFeatures) { QueryTest test; - auto features1 = test.map.getSource("source3")->querySourceFeatures(); + auto features1 = test.map.querySourceFeatures("source3"); EXPECT_EQ(features1.size(), 1u); } @@ -94,15 +94,15 @@ TEST(Query, QuerySourceFeaturesOptionValidation) { QueryTest test; // GeoJSONSource, doesn't require a layer id - auto features = test.map.getSource("source3")->querySourceFeatures(); + auto features = test.map.querySourceFeatures("source3"); ASSERT_EQ(features.size(), 1u); // VectorSource, requires a layer id - features = test.map.getSource("source5")->querySourceFeatures(); + features = test.map.querySourceFeatures("source5"); ASSERT_EQ(features.size(), 0u); // RasterSource, not supported - features = test.map.getSource("source6")->querySourceFeatures(); + features = test.map.querySourceFeatures("source6"); ASSERT_EQ(features.size(), 0u); } @@ -110,15 +110,15 @@ TEST(Query, QuerySourceFeaturesFilter) { QueryTest test; const EqualsFilter eqFilter = { "key1", std::string("value1") }; - auto features1 = test.map.getSource("source4")->querySourceFeatures({{}, { eqFilter }}); + auto features1 = test.map.querySourceFeatures("source4", {{}, { eqFilter }}); EXPECT_EQ(features1.size(), 1u); const IdentifierNotEqualsFilter idNotEqFilter = { std::string("feature1") }; - auto features2 = test.map.getSource("source4")->querySourceFeatures({{}, { idNotEqFilter }}); + auto features2 = test.map.querySourceFeatures("source4", {{}, { idNotEqFilter }}); EXPECT_EQ(features2.size(), 0u); const GreaterThanFilter gtFilter = { "key2", 1.0 }; - auto features3 = test.map.getSource("source4")->querySourceFeatures({{}, { gtFilter }}); + auto features3 = test.map.querySourceFeatures("source4", {{}, { gtFilter }}); EXPECT_EQ(features3.size(), 1u); } diff --git a/test/tile/vector_tile.test.cpp b/test/tile/vector_tile.test.cpp index 26c5dac8c..15d437c36 100644 --- a/test/tile/vector_tile.test.cpp +++ b/test/tile/vector_tile.test.cpp @@ -6,8 +6,8 @@ #include #include #include +#include #include -#include #include #include #include