Skip to content

Commit b25f438

Browse files
author
Clément Le Provost
committed
Merge branch 'refact/insidePolygons'
2 parents da29b77 + c5cf557 commit b25f438

File tree

3 files changed

+88
-78
lines changed

3 files changed

+88
-78
lines changed

Source/Query.swift

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -676,35 +676,40 @@ open class Query : AbstractQuery {
676676
}
677677
}
678678

679-
/// Search entries inside a given area defined by a set of points (defined by a minimum of 3 points).
680-
/// You can pass several time the insidePolygon parameter to your query, the behavior will be a OR between all those polygons.
681-
@objc public var insidePolygon: [LatLng]? {
682-
// FIXME: Union cannot work with this implementation, as at most one occurrence per parameter is supported.
679+
/// Search entries inside a given area defined by a union of polygons.
680+
/// Each polygon must be defined by a minimum of 3 points.
681+
@objc public var insidePolygon: [[LatLng]]? {
683682
get {
684-
if let fields = self["insidePolygon"]?.components(separatedBy: ",") {
685-
if fields.count % 2 == 0 && fields.count / 2 >= 3 {
686-
var result = [LatLng]()
687-
for i in 0..<(fields.count / 2) {
688-
if let lat = Double(fields[2 * i + 0]), let lng = Double(fields[2 * i + 1]) {
689-
result.append(LatLng(lat: lat, lng: lng))
690-
}
683+
if let data = self["insidePolygon"]?.data(using: .utf8, allowLossyConversion: false),
684+
let srcPolygons = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [[Double]] {
685+
var dstPolygons = [[LatLng]]()
686+
for srcPolygon in srcPolygons {
687+
var dstPolygon = [LatLng]()
688+
if srcPolygon.count % 2 != 0 { continue }
689+
for i in 0 ..< srcPolygon.count / 2 {
690+
let point = LatLng(lat: srcPolygon[2 * i], lng: srcPolygon[2 * i + 1])
691+
dstPolygon.append(point)
691692
}
692-
return result
693+
dstPolygons.append(dstPolygon)
693694
}
695+
return dstPolygons
694696
}
695697
return nil
696698
}
697699
set {
698-
if newValue == nil {
699-
self["insidePolygon"] = nil
700-
} else {
701-
assert(newValue!.count >= 3)
702-
var components = [String]()
703-
for point in newValue! {
704-
components.append(String(point.lat))
705-
components.append(String(point.lng))
700+
if let srcPolygons = newValue {
701+
var dstPolygons = [[Double]]()
702+
for srcPolygon in srcPolygons {
703+
var dstPolygon = [Double]()
704+
for point in srcPolygon {
705+
dstPolygon.append(point.lat)
706+
dstPolygon.append(point.lng)
707+
}
708+
dstPolygons.append(dstPolygon)
706709
}
707-
self["insidePolygon"] = components.joined(separator: ",")
710+
self["insidePolygon"] = AbstractQuery.buildJSONArray(dstPolygons)
711+
} else {
712+
self["insidePolygon"] = nil
708713
}
709714
}
710715
}

0 commit comments

Comments
 (0)