1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#import "NSPredicate+MGLAdditions.h"
#import "MGLValueEvaluator.h"
#import "MGLStyleValue_Private.h"
class FilterEvaluator {
public:
NSArray *getPredicates(std::vector<mbgl::style::Filter> filters) {
NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:filters.size()];
for (auto filter : filters) {
[predicates addObject:mbgl::style::Filter::visit(filter, FilterEvaluator())];
}
return predicates;
}
template <typename MBGLType>
NSExpression *getValues(std::vector<MBGLType> values) {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:values.size()];
for (auto value : values) {
id constantValue = MBGLType::visit(value, ValueEvaluator());
[array addObject:[NSExpression expressionForConstantValue:constantValue]];
}
return [NSExpression expressionForAggregate:array];
}
NSString *getFeatureTypeString(mbgl::FeatureType type) {
switch (type) {
case mbgl::FeatureType::Point:
return @"Point";
case mbgl::FeatureType::LineString:
return @"LineString";
case mbgl::FeatureType::Polygon:
return @"Polygon";
default:
NSCAssert(NO, @"Unrecognized feature type %hhu", type);
return nil;
}
}
NSExpression *getFeatureTypeStrings(std::vector<mbgl::FeatureType> values) {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:values.size()];
for (auto value : values) {
id typeString = getFeatureTypeString(value);
[array addObject:[NSExpression expressionForConstantValue:typeString]];
}
return [NSExpression expressionForAggregate:array];
}
NSPredicate *operator()(mbgl::style::NullFilter filter) {
return nil;
}
NSPredicate *operator()(mbgl::style::EqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K == %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::NotEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K != %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::GreaterThanFilter filter) {
return [NSPredicate predicateWithFormat:@"%K > %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::GreaterThanEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K >= %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::LessThanFilter filter) {
return [NSPredicate predicateWithFormat:@"%K < %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::LessThanEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K <= %@", @(filter.key.c_str()), mbgl::Value::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::InFilter filter) {
return [NSPredicate predicateWithFormat:@"%K IN %@", @(filter.key.c_str()), getValues(filter.values)];
}
NSPredicate *operator()(mbgl::style::NotInFilter filter) {
return [NSPredicate predicateWithFormat:@"NOT %K IN %@", @(filter.key.c_str()), getValues(filter.values)];
}
NSPredicate *operator()(mbgl::style::TypeEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K == %@", @"$type", getFeatureTypeString(filter.value)];
}
NSPredicate *operator()(mbgl::style::TypeNotEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K != %@", @"$type", getFeatureTypeString(filter.value)];
}
NSPredicate *operator()(mbgl::style::TypeInFilter filter) {
return [NSPredicate predicateWithFormat:@"%K IN %@", @"$type", getFeatureTypeStrings(filter.values)];
}
NSPredicate *operator()(mbgl::style::TypeNotInFilter filter) {
return [NSPredicate predicateWithFormat:@"NOT %K IN %@", @"$type", getFeatureTypeStrings(filter.values)];
}
NSPredicate *operator()(mbgl::style::IdentifierEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K == %@", @"$id", mbgl::FeatureIdentifier::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::IdentifierNotEqualsFilter filter) {
return [NSPredicate predicateWithFormat:@"%K != %@", @"$id", mbgl::FeatureIdentifier::visit(filter.value, ValueEvaluator())];
}
NSPredicate *operator()(mbgl::style::IdentifierInFilter filter) {
return [NSPredicate predicateWithFormat:@"%K IN %@", @"$id", getValues(filter.values)];
}
NSPredicate *operator()(mbgl::style::IdentifierNotInFilter filter) {
return [NSPredicate predicateWithFormat:@"NOT %K IN %@", @"$id", getValues(filter.values)];
}
NSPredicate *operator()(mbgl::style::AnyFilter filter) {
NSArray *subpredicates = getPredicates(filter.filters);
if (subpredicates.count) {
return [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
}
return [NSPredicate predicateWithValue:NO];
}
NSPredicate *operator()(mbgl::style::AllFilter filter) {
// Convert [all, [>=, key, lower], [<=, key, upper]] to key BETWEEN {lower, upper}
if (filter.filters.size() == 2) {
auto leftFilter = filter.filters[0];
auto rightFilter = filter.filters[1];
std::string lowerKey;
std::string upperKey;
mbgl::Value lowerBound;
mbgl::Value upperBound;
if (leftFilter.is<mbgl::style::GreaterThanEqualsFilter>()) {
lowerKey = leftFilter.get<mbgl::style::GreaterThanEqualsFilter>().key;
lowerBound = leftFilter.get<mbgl::style::GreaterThanEqualsFilter>().value;
} else if (rightFilter.is<mbgl::style::GreaterThanEqualsFilter>()) {
lowerKey = rightFilter.get<mbgl::style::GreaterThanEqualsFilter>().key;
lowerBound = rightFilter.get<mbgl::style::GreaterThanEqualsFilter>().value;
}
if (leftFilter.is<mbgl::style::LessThanEqualsFilter>()) {
upperKey = leftFilter.get<mbgl::style::LessThanEqualsFilter>().key;
upperBound = leftFilter.get<mbgl::style::LessThanEqualsFilter>().value;
} else if (rightFilter.is<mbgl::style::LessThanEqualsFilter>()) {
upperKey = rightFilter.get<mbgl::style::LessThanEqualsFilter>().key;
upperBound = rightFilter.get<mbgl::style::LessThanEqualsFilter>().value;
}
if (!lowerBound.is<mbgl::NullValue>() && !upperBound.is<mbgl::NullValue>()
&& lowerKey == upperKey) {
return [NSPredicate predicateWithFormat:@"%K BETWEEN {%@, %@}",
@(lowerKey.c_str()),
mbgl::Value::visit(lowerBound, ValueEvaluator()),
mbgl::Value::visit(upperBound, ValueEvaluator())];
}
}
NSArray *subpredicates = getPredicates(filter.filters);
if (subpredicates.count) {
return [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
}
return [NSPredicate predicateWithValue:YES];
}
NSPredicate *operator()(mbgl::style::NoneFilter filter) {
NSArray *subpredicates = getPredicates(filter.filters);
if (subpredicates.count > 1) {
NSCompoundPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
return [NSCompoundPredicate notPredicateWithSubpredicate:predicate];
} else if (subpredicates.count) {
return [NSCompoundPredicate notPredicateWithSubpredicate:subpredicates.firstObject];
} else {
return [NSPredicate predicateWithValue:YES];
}
}
NSPredicate *operator()(mbgl::style::HasFilter filter) {
return [NSPredicate predicateWithFormat:@"%K != nil", @(filter.key.c_str())];
}
NSPredicate *operator()(mbgl::style::NotHasFilter filter) {
return [NSPredicate predicateWithFormat:@"%K == nil", @(filter.key.c_str())];
}
NSPredicate *operator()(mbgl::style::HasIdentifierFilter filter) {
return [NSPredicate predicateWithFormat:@"%K != nil", @"$id"];
}
NSPredicate *operator()(mbgl::style::NotHasIdentifierFilter filter) {
return [NSPredicate predicateWithFormat:@"%K == nil", @"$id"];
}
NSPredicate *operator()(mbgl::style::ExpressionFilter filter) {
id jsonObject = MGLJSONObjectFromMBGLExpression(*filter.expression);
return [NSPredicate mgl_predicateWithJSONObject:jsonObject];
}
};
@implementation NSPredicate (MGLAdditions)
- (mbgl::style::Filter)mgl_filter
{
if ([self isEqual:[NSPredicate predicateWithValue:YES]])
{
return mbgl::style::AllFilter();
}
if ([self isEqual:[NSPredicate predicateWithValue:NO]])
{
return mbgl::style::AnyFilter();
}
if ([self.predicateFormat hasPrefix:@"BLOCKPREDICATE("])
{
[NSException raise:NSInvalidArgumentException
format:@"Block-based predicates are not supported."];
}
[NSException raise:NSInvalidArgumentException
format:@"Unrecognized predicate type."];
return {};
}
+ (instancetype)mgl_predicateWithFilter:(mbgl::style::Filter)filter
{
FilterEvaluator evaluator;
return mbgl::style::Filter::visit(filter, evaluator);
}
@end
@implementation NSPredicate (MGLExpressionAdditions)
NSArray *MGLSubpredicatesWithJSONObjects(NSArray *objects) {
NSMutableArray *subpredicates = [NSMutableArray arrayWithCapacity:objects.count];
for (id object in objects) {
NSPredicate *predicate = [NSPredicate mgl_predicateWithJSONObject:object];
[subpredicates addObject:predicate];
}
return subpredicates;
}
+ (instancetype)mgl_predicateWithJSONObject:(id)object {
if ([object isEqual:@YES]) {
return [NSPredicate predicateWithValue:YES];
}
if ([object isEqual:@NO]) {
return [NSPredicate predicateWithValue:NO];
}
NSAssert([object isKindOfClass:[NSArray class]], @"Condition for case expression should be an expression.");
NSArray *objects = (NSArray *)object;
NSString *op = objects.firstObject;
if ([op isEqualToString:@"=="]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%@ == %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@"!="]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%@ != %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@"<"]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%@ < %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@"<="]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%@ <= %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@">"]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%@ > %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@">="]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSPredicate predicateWithFormat:@"%K >= %@" argumentArray:subexpressions];
}
if ([op isEqualToString:@"!"]) {
NSArray *subpredicates = MGLSubpredicatesWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
if (subpredicates.count > 1) {
NSCompoundPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
return [NSCompoundPredicate notPredicateWithSubpredicate:predicate];
}
if (subpredicates.count) {
return [NSCompoundPredicate notPredicateWithSubpredicate:subpredicates.firstObject];
}
return [NSPredicate predicateWithValue:YES];
}
if ([op isEqualToString:@"all"]) {
NSArray *subpredicates = MGLSubpredicatesWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
}
if ([op isEqualToString:@"any"]) {
NSArray *subpredicates = MGLSubpredicatesWithJSONObjects([objects subarrayWithRange:NSMakeRange(1, objects.count - 1)]);
return [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
}
NSAssert(NO, @"Unrecognized expression conditional operator %@.", op);
return nil;
}
- (id)mgl_jsonExpressionObject {
if ([self isEqual:[NSPredicate predicateWithValue:YES]]) {
return @YES;
}
if ([self isEqual:[NSPredicate predicateWithValue:NO]]) {
return @NO;
}
if ([self.predicateFormat hasPrefix:@"BLOCKPREDICATE("]) {
[NSException raise:NSInvalidArgumentException
format:@"Block-based predicates are not supported."];
}
[NSException raise:NSInvalidArgumentException
format:@"Unrecognized predicate type."];
return nil;
}
@end
|