summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-03-05 14:12:37 -0800
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-03-09 13:11:23 -0800
commit12568f21a39dfec7788ebee2815c4c0b2ee964d8 (patch)
tree6184a8e741c3658c6ef672866f882b1a72c9c51b /platform/darwin
parent0d10d2df1c6d246004e7291511f3aab7a8781d59 (diff)
downloadqtlocation-mapboxgl-12568f21a39dfec7788ebee2815c4c0b2ee964d8.tar.gz
[macos, ios] query source features
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLShapeSource.h16
-rw-r--r--platform/darwin/src/MGLShapeSource.mm12
-rw-r--r--platform/darwin/src/MGLVectorSource.h26
-rw-r--r--platform/darwin/src/MGLVectorSource.mm23
-rw-r--r--platform/darwin/test/MGLSourceQueryTests.m73
-rw-r--r--platform/darwin/test/query-style.json98
6 files changed, 248 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLShapeSource.h b/platform/darwin/src/MGLShapeSource.h
index 24cdf82bea..07045490bd 100644
--- a/platform/darwin/src/MGLShapeSource.h
+++ b/platform/darwin/src/MGLShapeSource.h
@@ -210,6 +210,22 @@ MGL_EXPORT
*/
@property (nonatomic, copy, nullable) NSURL *URL;
+/**
+ Returns an array of map features for this source, filtered by the given predicate.
+
+ Each object in the returned array represents a feature for the current style
+ and provides access to attributes specified by the source
+
+ Features come from tiled GeoJSON data that is converted to tiles internally,
+ so feature geometries are clipped at tile boundaries and features
+ may appear duplicated across tiles.
+
+ @param predicate A predicate to filter the returned features.
+ @return An array of objects conforming to the `MGLFeature` protocol that
+ represent features in the sources used by the current style.
+ */
+- (NS_ARRAY_OF(id <MGLFeature>) *)featuresMatchingPredicate:(nullable NSPredicate *)predicate;
+
@end
NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm
index b37b01663f..7de2d69af3 100644
--- a/platform/darwin/src/MGLShapeSource.mm
+++ b/platform/darwin/src/MGLShapeSource.mm
@@ -5,6 +5,7 @@
#import "MGLFeature_Private.h"
#import "MGLShape_Private.h"
+#import "NSPredicate+MGLAdditions.h"
#import "NSURL+MGLAdditions.h"
#include <mbgl/map/map.hpp>
@@ -135,6 +136,17 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh
NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.shape];
}
+- (NS_ARRAY_OF(id <MGLFeature>) *)featuresMatchingPredicate:(nullable NSPredicate *)predicate {
+
+ mbgl::optional<mbgl::style::Filter> optionalFilter;
+ if (predicate) {
+ optionalFilter = predicate.mgl_filter;
+ }
+
+ std::vector<mbgl::Feature> features = self.rawSource->querySourceFeatures({ {}, optionalFilter });
+ return MGLFeaturesFromMBGLFeatures(features);
+}
+
@end
mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NS_DICTIONARY_OF(MGLShapeSourceOption, id) *options) {
diff --git a/platform/darwin/src/MGLVectorSource.h b/platform/darwin/src/MGLVectorSource.h
index 2322c62f29..8634316809 100644
--- a/platform/darwin/src/MGLVectorSource.h
+++ b/platform/darwin/src/MGLVectorSource.h
@@ -1,3 +1,4 @@
+#import "MGLFeature.h"
#import "MGLFoundation.h"
#import "MGLTileSource.h"
@@ -42,10 +43,35 @@ NS_ASSUME_NONNULL_BEGIN
MGL_EXPORT
@interface MGLVectorSource : MGLTileSource
+#pragma mark Initializing a Source
+
- (instancetype)initWithIdentifier:(NSString *)identifier configurationURL:(NSURL *)configurationURL NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithIdentifier:(NSString *)identifier tileURLTemplates:(NS_ARRAY_OF(NSString *) *)tileURLTemplates options:(nullable NS_DICTIONARY_OF(MGLTileSourceOption, id) *)options NS_DESIGNATED_INITIALIZER;
+#pragma mark Accessing a Source’s Content
+
+/**
+ Returns an array of map features loaded by this source, restricted to the
+ given source layers and filtered by the given predicate.
+
+ Each object in the returned array represents a feature for the
+ current style and provides access to attributes specified by the
+ <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources">source</a>.
+
+ Features come from tiled vector data that is converted to tiles internally,
+ so feature geometries are clipped at tile boundaries and features
+ may appear duplicated across tiles.
+
+ @param sourceLayerIdentifiers The source layers to include in the query. Only the
+ features contained in these source layers are included in the returned array. At
+ least one source layer is required.
+ @param predicate A predicate to filter the returned features.
+ @return An array of objects conforming to the `MGLFeature` protocol that
+ represent features in the sources used by the current style.
+ */
+- (NS_ARRAY_OF(id <MGLFeature>) *)featuresInSourceLayersWithIdentifiers:(NS_SET_OF(NSString *) *)sourceLayerIdentifiers predicate:(nullable NSPredicate *)predicate NS_SWIFT_NAME(features(sourceLayerIdentifiers:predicate:));
+
@end
NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLVectorSource.mm b/platform/darwin/src/MGLVectorSource.mm
index 94511900c1..aeec2e40ac 100644
--- a/platform/darwin/src/MGLVectorSource.mm
+++ b/platform/darwin/src/MGLVectorSource.mm
@@ -1,8 +1,10 @@
#import "MGLVectorSource_Private.h"
#import "MGLMapView_Private.h"
+#import "MGLFeature_Private.h"
#import "MGLSource_Private.h"
#import "MGLTileSource_Private.h"
+#import "NSPredicate+MGLAdditions.h"
#import "NSURL+MGLAdditions.h"
#include <mbgl/map/map.hpp>
@@ -91,4 +93,25 @@
return attribution ? @(attribution->c_str()) : nil;
}
+- (NS_ARRAY_OF(id <MGLFeature>) *)featuresInSourceLayersWithIdentifiers:(NS_SET_OF(NSString *) *)sourceLayerIdentifiers predicate:(nullable NSPredicate *)predicate {
+
+ mbgl::optional<std::vector<std::string>> optionalSourceLayerIDs;
+ if (sourceLayerIdentifiers) {
+ __block std::vector<std::string> layerIDs;
+ layerIDs.reserve(sourceLayerIdentifiers.count);
+ [sourceLayerIdentifiers enumerateObjectsUsingBlock:^(NSString * _Nonnull identifier, BOOL * _Nonnull stop) {
+ layerIDs.push_back(identifier.UTF8String);
+ }];
+ optionalSourceLayerIDs = layerIDs;
+ }
+
+ mbgl::optional<mbgl::style::Filter> optionalFilter;
+ if (predicate) {
+ optionalFilter = predicate.mgl_filter;
+ }
+
+ std::vector<mbgl::Feature> features = self.rawSource->querySourceFeatures({ optionalSourceLayerIDs, optionalFilter });
+ return MGLFeaturesFromMBGLFeatures(features);
+}
+
@end
diff --git a/platform/darwin/test/MGLSourceQueryTests.m b/platform/darwin/test/MGLSourceQueryTests.m
new file mode 100644
index 0000000000..28e968146b
--- /dev/null
+++ b/platform/darwin/test/MGLSourceQueryTests.m
@@ -0,0 +1,73 @@
+#import <Mapbox/Mapbox.h>
+
+#import "NSBundle+MGLAdditions.h"
+
+#import <XCTest/XCTest.h>
+#if TARGET_OS_IPHONE
+ #import <UIKit/UIKit.h>
+#else
+ #import <Cocoa/Cocoa.h>
+#endif
+
+@interface MGLSourceQueryTests : XCTestCase <MGLMapViewDelegate>
+
+@property (nonatomic) MGLMapView *mapView;
+@property (nonatomic) MGLStyle *style;
+
+@end
+
+@implementation MGLSourceQueryTests {
+ XCTestExpectation *_styleLoadingExpectation;
+}
+
+- (void)setUp {
+ [super setUp];
+
+ [MGLAccountManager setAccessToken:@"pk.feedcafedeadbeefbadebede"];
+ NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"query-style" withExtension:@"json"];
+ self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) styleURL:styleURL];
+ self.mapView.delegate = self;
+ if (!self.mapView.style) {
+ _styleLoadingExpectation = [self expectationWithDescription:@"Map view should finish loading style."];
+ [self waitForExpectationsWithTimeout:1 handler:nil];
+ }
+}
+
+- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
+ XCTAssertNotNil(mapView.style);
+ XCTAssertEqual(mapView.style, style);
+
+ [_styleLoadingExpectation fulfill];
+}
+
+- (void)tearDown {
+ _styleLoadingExpectation = nil;
+ self.mapView = nil;
+
+ [super tearDown];
+}
+
+- (MGLStyle *)style {
+ return self.mapView.style;
+}
+
+- (void) testQueryVectorSource {
+ MGLVectorSource *source = (MGLVectorSource *)[self.style sourceWithIdentifier:@"source5"];
+
+ NSSet *sourceLayers = [NSSet setWithObjects:@"buildings", @"water", nil];
+ NSArray* features = [source featuresInSourceLayersWithIdentifiers:sourceLayers predicate:nil];
+ // Source won't be loaded yet, so features is 0
+ XCTAssertEqual([features count], 0);
+}
+
+- (void) testQueryShapeSource {
+ MGLShapeSource *source = (MGLShapeSource *)[self.style sourceWithIdentifier:@"source4"];
+
+ NSPredicate *eqPredicate = [NSPredicate predicateWithFormat:@"key1 == 'value1'"];
+ NSArray* features = [source featuresMatchingPredicate:eqPredicate];
+ // Source won't be loaded yet, so features is 0
+ XCTAssertEqual([features count], 0);
+
+}
+
+@end
diff --git a/platform/darwin/test/query-style.json b/platform/darwin/test/query-style.json
new file mode 100644
index 0000000000..97f1d04432
--- /dev/null
+++ b/platform/darwin/test/query-style.json
@@ -0,0 +1,98 @@
+{
+ "version": 8,
+ "sources": {
+ "source1": {
+ "type": "geojson",
+ "data": {
+ "type": "Point",
+ "coordinates": [
+ 0,
+ 0
+ ]
+ }
+ },
+ "source2": {
+ "type": "geojson",
+ "data": {
+ "type": "Point",
+ "coordinates": [
+ 0,
+ 0
+ ]
+ }
+ },
+ "source3": {
+ "type": "geojson",
+ "data": {
+ "type": "Point",
+ "coordinates": [
+ 0,
+ 0
+ ]
+ }
+ },
+ "source4": {
+ "type": "geojson",
+ "data": {
+ "type": "Feature",
+ "id": "feature1",
+ "geometry": {
+ "type": "Point",
+ "coordinates": [
+ 0.0,
+ 0.0
+ ]
+ },
+ "properties": {
+ "key1": "value1",
+ "key2": 1.5,
+ "key3": false,
+ "key4": 0.5
+ }
+ }
+ },
+ "source5": {
+ "type": "vector",
+ "url": "mapbox://mapbox.mapbox-streets-v6"
+ },
+ "source6": {
+ "type": "raster",
+ "url": "mapbox://mapbox.satellite",
+ "tileSize": 256
+ }
+ },
+ "layers": [
+ {
+ "id": "layer1",
+ "type": "symbol",
+ "source": "source1",
+ "layout": {
+ "icon-image": "test-icon"
+ }
+ },
+ {
+ "id": "layer2",
+ "type": "symbol",
+ "source": "source2",
+ "layout": {
+ "icon-image": "test-icon"
+ }
+ },
+ {
+ "id": "layer3",
+ "type": "symbol",
+ "source": "source3",
+ "layout": {
+ "icon-image": "test-icon"
+ }
+ },
+ {
+ "id": "layer4",
+ "type": "symbol",
+ "source": "source4",
+ "layout": {
+ "icon-image": "test-icon"
+ }
+ }
+ ]
+}