From c8c664fbf376b615b3f1e4b2a4d7f1c99b6097be Mon Sep 17 00:00:00 2001 From: Julian Rex Date: Mon, 14 Jan 2019 15:17:13 -0500 Subject: [ios, macos] Support getLeaves (and related) clustering methods (#12952) following feature extension API (#13382) --- platform/darwin/src/MGLShapeSource.mm | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'platform/darwin/src/MGLShapeSource.mm') diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index c960f2a4a7..fc526f9850 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -1,10 +1,13 @@ +#import "MGLFoundation_Private.h" #import "MGLShapeSource_Private.h" +#import "MGLLoggingConfiguration_Private.h" #import "MGLStyle_Private.h" #import "MGLMapView_Private.h" #import "MGLSource_Private.h" #import "MGLFeature_Private.h" #import "MGLShape_Private.h" +#import "MGLCluster.h" #import "NSPredicate+MGLPrivateAdditions.h" #import "NSURL+MGLAdditions.h" @@ -184,4 +187,106 @@ mbgl::style::GeoJSONOptions MGLGeoJSONOptionsFromDictionary(NSDictionary)featureExtensionValueOfCluster:(MGLShape *)cluster extension:(std::string)extension options:(const std::map)options { + mbgl::optional extensionValue; + + // Check parameters + if (!self.rawSource || !self.mapView || !cluster) { + return extensionValue; + } + + auto geoJSON = [cluster geoJSONObject]; + + if (!geoJSON.is()) { + MGLAssert(0, @"cluster geoJSON object is not a feature."); + return extensionValue; + } + + auto clusterFeature = geoJSON.get(); + + extensionValue = self.mapView.renderer->queryFeatureExtensions(self.rawSource->getID(), + clusterFeature, + "supercluster", + extension, + options); + return extensionValue; +} + +- (NSArray> *)leavesOfCluster:(MGLPointFeatureCluster *)cluster offset:(NSUInteger)offset limit:(NSUInteger)limit { + const std::map options = { + { "limit", static_cast(limit) }, + { "offset", static_cast(offset) } + }; + + auto featureExtension = [self featureExtensionValueOfCluster:cluster extension:"leaves" options:options]; + + if (!featureExtension) { + return @[]; + } + + if (!featureExtension->is()) { + return @[]; + } + + std::vector leaves = featureExtension->get(); + return MGLFeaturesFromMBGLFeatures(leaves); +} + +- (NSArray> *)childrenOfCluster:(MGLPointFeatureCluster *)cluster { + auto featureExtension = [self featureExtensionValueOfCluster:cluster extension:"children" options:{}]; + + if (!featureExtension) { + return @[]; + } + + if (!featureExtension->is()) { + return @[]; + } + + std::vector leaves = featureExtension->get(); + return MGLFeaturesFromMBGLFeatures(leaves); +} + +- (double)zoomLevelForExpandingCluster:(MGLPointFeatureCluster *)cluster { + auto featureExtension = [self featureExtensionValueOfCluster:cluster extension:"expansion-zoom" options:{}]; + + if (!featureExtension) { + return -1.0; + } + + if (!featureExtension->is()) { + return -1.0; + } + + auto value = featureExtension->get(); + if (value.is()) { + auto zoom = value.get(); + return static_cast(zoom); + } + + return -1.0; +} + +- (void)debugRecursiveLogForFeature:(id )feature indent:(NSUInteger)indent { + NSString *description = feature.description; + + // Want our recursive log on a single line + NSString *log = [description stringByReplacingOccurrencesOfString:@"\\s+" + withString:@" " + options:NSRegularExpressionSearch + range:NSMakeRange(0, description.length)]; + + printf("%*s%s\n", (int)indent, "", log.UTF8String); + + MGLPointFeatureCluster *cluster = MGL_OBJC_DYNAMIC_CAST(feature, MGLPointFeatureCluster); + + if (cluster) { + for (id child in [self childrenOfCluster:cluster]) { + [self debugRecursiveLogForFeature:child indent:indent + 4]; + } + } +} + @end -- cgit v1.2.1