[ios, macos] Added distance expression function

Installed an mgl_distanceFrom: aftermarket expression function that corresponds to the “distance” expression operator in style JSON.
This commit is contained in:
Minh Nguyễn
2020-05-01 11:59:04 -07:00
committed by Minh Nguyễn
parent 93141ac0f0
commit 27eee6adf5
8 changed files with 45 additions and 3 deletions

View File

@@ -389,6 +389,7 @@ In style specification | Method, function, or predicate type | Format string syn
`atan` | `mgl_atan:` | `mgl_atan(20)`
`ceil` | `ceiling:` | `ceiling(0.99999)`
`cos` | `mgl_cos:` | `mgl_cos(0)`
`distance` | | `mgl_distanceFrom(%@)` where `%@` is an `MGLShape`
`e` | | `%@` representing `NSNumber` containing `M_E`
`floor` | `floor:` | `floor(-0.99999)`
`ln` | `ln:` | `ln(2)`

View File

@@ -540,6 +540,21 @@ This function corresponds to the
[`tan`](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-tan)
operator in the Mapbox Style Specification.
### `mgl_distanceFrom:`
<dl>
<dt>Selector:</dt>
<dd><code>mgl_distanceFrom:</code></dd>
<dt>Format string syntax:</dt>
<dd><code>mgl_distanceFrom(%@)</code> with an <code>MGLShape</code></dd>
</dl>
Returns the straight-line distance from the evaluated object to the given shape.
This function corresponds to the
[`distance`](https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#distance)
operator in the Mapbox Style Specification.
### `mgl_coalesce:`
<dl>

View File

@@ -77,6 +77,7 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier =
INSTALL_METHOD(mgl_atan:);
INSTALL_METHOD(mgl_tan:);
INSTALL_METHOD(mgl_log2:);
INSTALL_METHOD(mgl_distanceFrom:);
INSTALL_METHOD(mgl_attributed:);
// Install functions that resemble control structures, taking arbitrary
@@ -162,6 +163,15 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier =
return @(log2(number.doubleValue));
}
/**
Returns a straight-line distance from the given shape to the evaluated feature.
*/
- (NSNumber *)mgl_distanceFrom:(id)object {
[NSException raise:NSInvalidArgumentException
format:@"Shape distance expressions lack underlying Objective-C implementations."];
return nil;
}
/**
A placeholder for a method that evaluates an interpolation expression.
*/
@@ -656,6 +666,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
@"floor": @"floor:",
@"ceil": @"ceiling:",
@"^": @"raise:toPower:",
@"distance": @"mgl_distanceFrom:",
@"upcase": @"uppercase:",
@"downcase": @"lowercase:",
@"let": @"MGL_LET",
@@ -970,6 +981,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
@"mgl_atan:" : @"atan",
@"mgl_tan:" : @"tan",
@"mgl_log2:" : @"log2",
@"mgl_distanceFrom:": @"distance",
// Vararg aftermarket expressions need to be declared with an explicit and implicit first argument.
@"MGL_LET": @"let",
@"MGL_LET:": @"let",

View File

@@ -667,6 +667,18 @@ using namespace std::string_literals;
}
}
- (void)testShapeDistanceExpressionObject {
{
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(1, -1);
NSExpression *expression = [NSExpression expressionForFunction:@"mgl_distanceFrom:" arguments:@[MGLConstantExpression(point)]];
NSArray *jsonExpression = @[@"distance", @{@"type": @"Point", @"coordinates": @[@-1, @1]}];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertThrowsSpecificNamed([expression expressionValueWithObject:nil context:nil], NSException, NSInvalidArgumentException);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
}
- (void)testStringFormattingExpressionObject {
NSArray *arguments = @[MGLConstantExpression(@"MacDonald")];
{