我们的一个 Mapbox tilesets 中的一个表有一个 ids 的字符串化 JSON 数组属性:
"string_ids":"[\"a\",\"b\"]"
我想使用此属性过滤功能,但似乎无法在 Predicates and Expressions documentation 中找到执行此操作的方法.因此,例如,我想过滤功能,以便仅显示“string_id”为“a”的功能。
我相信这会在准备就绪时帮助解决我的问题 ( https://github.com/mapbox/mapbox-gl-js/issues/4113 ),但只是想知道此时是否有其他解决方案?
更新
我尝试了几种不同的方法:
-
NSPredicate(format: "'a' IN CAST(string_ids, 'NSArray<NSString>')")
错误输出:“尚未实现将表达式转换为 NSArray。” -
NSPredicate(format: "string_ids contains[c] %@", "a")
不会出错,但没有特征匹配过滤器。 -
NSPredicate(format: "string_ids LIKE 'a'")
错误:“不支持 NSPredicateOperatorType:7。”
最佳答案
NSExpression/NSPredicate 实现在支持哪些运算符方面有所不同。 (如果您查看 Apple 的 NSPredicate 文档,就会发现某些 Core Data 后端也缺乏对某些运算符的支持。)
iOS map SDK 不支持字符串的IN
、CONTAINS
和LIKE
,among other operators .在底层,SDK 将 NSExpressions 和 NSPredicates 翻译成 the expression syntax described in the Mapbox Style Specification。 .
如您所述,样式规范缺少 searching for substrings 的运算符.它还缺少 splitting a string 的运算符或将字符串转换为 JSON 对象(目前还没有问题,但可以随意打开一个)。
一种解决方法可能是使用 -[MGLVectorTileSource featuresInSourceLayersWithIdentifiers:predicate:]
查询源中的特征,手动评估属性值是否应显示,并将修改后的特征添加到新的 MGLShapeSource。
您可能遇到的另一个限制是特征查询结果 can’t have feature properties set to nested objects and arrays .
关于swift - 按字符串化 JSON 数组属性中包含的值过滤图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56613977/