0

I'd like to filter features from a source based on (some property) AND (distance within a radius).

I could not find a way to convert the point to the filter expression.

There is not much document about SourceQueryOptions in iOS SDK, and there is no example using that neither. I've been struggling to make it work for the last few days. I've checked many iOS and GL-JS documents and stack overflow, none of them worked for me.

The code is pretty much like the one below

let coord = CLLocationCoordinate2D(xxx)
let point = Turf.Feature(geometry: Point(coord))
var sqo = SourceQueryOptions(sourceLayerIds: [xxx], 
                        filter: ["all", ["==", ["get", "SUMLEV"], 750], ["<", ["distance", GeoJSONObject.feature(point)], 100]])
mapboxMap.querySourceFeatures(for: layerAllPopStats.sourceData.sourceStrId, options: sqo) { xxxx }

However, the app would crash and the Xcode error log reads:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Value Turf.GeoJSONObject.feature(feature id: 
empty_features: ) of type __SwiftValue must be either NSNumber, NSString, NSArray, NSDictionary, NSNull'

I tried to replace GeoJSONObject.feature(point) to Turf.Point(coord) and many other values, none would work.

Standard Swift/iOS expression does not work for filter.

I also noticed the standard Swift/iOS way to define expression does not work for the filter case. For example, if I use as simple as

let filter = Exp(.eq) {
      Exp(.get) { "SUMLEV"}
         750
      }
var sqo = SourceQueryOptions(sourceLayerIds: [xxx],  filter: filter)

It would not work, the Xcode would crash with error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Value [==, [get, SUMLEV], 750.0] of type __SwiftValue must be either NSNumber, NSString, NSArray, NSDictionary, NSNull'

But if I use

let filter: [Any] = ["==", "SUMLEV", 750]
var sqo = SourceQueryOptions(sourceLayerIds: [xxx],  filter: filter)

It would work.

The 'all' expression seems not working correctly

For example:

let filter: [Any] = ["==", "SUMLEV", 750]

will return me ~5k features; But when I also used

let filter: [Any] = ["all", ["==", ["get", "SUMLEV"], 750], ["<", ["distance", "userPosition", "meters"], 100]]

It will return me >7k features and the result seems not correct. It returns many features have SUMLEV != 750. So it looks like the "all" AND condition is not respected.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Valar Morghulis
  • 647
  • 5
  • 16

0 Answers0