-
Notifications
You must be signed in to change notification settings - Fork 122
Allow predicates to test whether a feature lies within a given shape #184
Changes from all commits
957296b
93f5290
5196e37
7b92e2a
e27b889
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#import "MGLFoundation_Private.h" | ||
#import "MGLGeometry_Private.h" | ||
#import "MGLShape_Private.h" | ||
#import "NSExpression+MGLPrivateAdditions.h" | ||
|
||
#import "MGLTypes.h" | ||
|
@@ -539,6 +540,14 @@ - (id)mgl_has:(id)element { | |
|
||
@end | ||
|
||
@implementation MGLShape (MGLExpressionAdditions) | ||
|
||
- (id)mgl_jsonExpressionObject { | ||
return self.geoJSONDictionary; | ||
} | ||
|
||
@end | ||
|
||
@implementation NSExpression (MGLAdditions) | ||
|
||
+ (NSExpression *)zoomLevelVariableExpression { | ||
|
@@ -659,11 +668,24 @@ + (instancetype)expressionWithMGLJSONObject:(id)object { | |
if ([object isKindOfClass:[NSString class]] || | ||
[object isKindOfClass:[NSNumber class]] || | ||
[object isKindOfClass:[NSValue class]] || | ||
[object isKindOfClass:[MGLColor class]]) { | ||
[object isKindOfClass:[MGLColor class]] || | ||
[object isKindOfClass:[MGLShape class]]) { | ||
return [NSExpression expressionForConstantValue:object]; | ||
} | ||
|
||
if ([object isKindOfClass:[NSDictionary class]]) { | ||
if (object[@"type"]) { | ||
NSError *error; | ||
NSData *shapeData = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; | ||
MGLShape *shape; | ||
if (shapeData && !error) { | ||
shape = [MGLShape shapeWithData:shapeData encoding:NSUTF8StringEncoding error:&error]; | ||
Comment on lines
+679
to
+682
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but there may be a more efficient way to convert this JSON-like NSDictionary structure into an MGLShape. Perhaps we could first convert the NSDictionary structure into a C++ representation of the GeoJSON object, then convert to an MGLShape. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any performance considerations for bridging into an C++ object then back to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current implementation, This conversion process happens once when setting a layer’s predicate or paint/layout property (which can happen automatically e.g. when localizing the style). mbgl will evaluate the already-converted expression on each feature, but no SDK-side code will run as part of the rendering loop. Ideally, mbgl would preserve the
but I’m inclined to treat either optimization as tail work for when we know more about how this expression operator will be used. The performance characteristics could differ depending on the complexity of the passed-in GeoJSON feature. |
||
} | ||
if (shape && !error) { | ||
return [NSExpression expressionForConstantValue:shape]; | ||
} | ||
} | ||
|
||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:[object count]]; | ||
[object enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { | ||
dictionary[key] = [NSExpression expressionWithMGLJSONObject:obj]; | ||
|
@@ -1031,6 +1053,10 @@ - (id)mgl_jsonExpressionObject { | |
} | ||
return @[jsonObject, attributedDictionary]; | ||
} | ||
if ([constantValue isKindOfClass:[MGLShape class]]) { | ||
MGLShape *shape = (MGLShape *)constantValue; | ||
return shape.geoJSONDictionary; | ||
} | ||
return self.constantValue; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MGLFeature support was added in mapbox/mapbox-gl-native#16232, which we haven’t pulled into this repository yet.