问题
One of the tables in one of our Mapbox tilesets has a stringified JSON array property of ids:
"string_ids":"[\"a\",\"b\"]"
I would like to filter features using this property, but can't seem to find a way to do it in the Predicates and Expressions documentation. So, for instance, I'd like to filter features so only those that have a "string_id" of "a" display.
I believe this will help with my problem, when ready (https://github.com/mapbox/mapbox-gl-js/issues/4113), but just wondering if there's another solution in place at this time?
UPDATE
I've tried several different approaches:
NSPredicate(format: "'a' IN CAST(string_ids, 'NSArray<NSString>')")
errors out with: "Casting expression to NSArray not yet implemented."NSPredicate(format: "string_ids contains[c] %@", "a")
does not error out, but no features match the filter.NSPredicate(format: "string_ids LIKE 'a'")
errors out with: "NSPredicateOperatorType:7 is not supported."
回答1:
NSExpression/NSPredicate implementations vary in terms of which operators are supported. (If you look through Apple’s NSPredicate documentation, there are caveats about certain Core Data backends lacking support for certain operators too.)
The iOS map SDK doesn’t support IN
, CONTAINS
, and LIKE
for strings, among other operators. Under the hood, the SDK translates NSExpressions and NSPredicates to the expression syntax described in the Mapbox Style Specification.
As you noted, the style specification lacks an operator for searching for substrings. It also lacks an operator for splitting a string or for converting a string to a JSON object (no issue yet, but feel free to open one).
One workaround might be to query for features in the source using -[MGLVectorTileSource featuresInSourceLayersWithIdentifiers:predicate:]
, manually evaluate the property values to whether they should be shown, and add the modified features to a new MGLShapeSource.
Another limitation you may run into is that feature querying results can’t have feature properties set to nested objects and arrays.
来源:https://stackoverflow.com/questions/56613977/filter-a-layer-by-a-value-contained-in-a-stringified-json-array-property