1import Foundation
2
3/// How the user's own places are ordered for a location, and how directory
4/// places are matched to them.
5enum PlaceRanking {
6 /// Distance bands, in metres. Places are ranked by band, then by use and
7 /// recency within a band: two places a few hundred metres apart are both
8 /// within the fix's error and the centre of past answers' spread, so the
9 /// one used more is the better guess.
10 static let bands: [Double] = [150, 400, 1_000, 5_000, 25_000]
11 /// Without a search, only places within this distance, in metres, are
12 /// offered: the user is probably at one of them. Places further away
13 /// are found by searching.
14 static let nearDistance: Double = 500
15 /// A place covers this distance around its coordinate, in metres. Typing
16 /// a place's name further away than this adds a new place with that
17 /// name, as Reporter's saved places each had one location. It matches
18 /// `nearDistance`, so a place listed as near is the one its name gives;
19 /// in the archive, over 99% of reports were within this distance of the
20 /// saved place they answered.
21 static let regionRadius: Double = nearDistance
22 /// A directory place this close to one of the user's places with the same
23 /// name, in metres, is taken to be that place.
24 static let matchDistance: Double = 200
25 /// A place is chosen for the user when it's the only one of their places
26 /// within this distance, in metres, of a fix at least this accurate.
27 static let prefillDistance: Double = 50
28
29 /// The index of the band `distance` falls in. Places with no known
30 /// distance come last.
31 static func band(for distance: Double?) -> Int {
32 guard let distance else { return bands.count + 1 }
33 return bands.firstIndex { distance <= $0 } ?? bands.count
34 }
35
36 /// `entries` ranked by distance from `origin`, each with its distance.
37 /// Without an origin they keep their order, which is by use and recency.
38 static func rank(
39 _ entries: [VocabularyEntry],
40 from origin: Coordinate?
41 ) -> [(entry: VocabularyEntry, distance: Double?)] {
42 let measured = entries.map { entry in
43 (entry: entry, distance: origin.flatMap { origin in entry.coordinate?.distance(to: origin) })
44 }
45 guard origin != nil else { return measured }
46 return measured.sorted { lhs, rhs in
47 let lhsBand = band(for: lhs.distance)
48 let rhsBand = band(for: rhs.distance)
49 if lhsBand != rhsBand { return lhsBand < rhsBand }
50 return Vocabulary.ranksBefore(lhs.entry, rhs.entry)
51 }
52 }
53
54 /// The place to choose for the user at `location`, if there's no doubt
55 /// which it is: the fix is accurate to `prefillDistance` and exactly
56 /// one of `entries` is that close. Two places that close, such as home
57 /// and the café next door, can't be told apart, so neither is chosen.
58 static func prefill(_ entries: [VocabularyEntry], at location: LocationContext) -> VocabularyEntry? {
59 guard let accuracy = location.horizontalAccuracy, accuracy >= 0, accuracy <= prefillDistance
60 else { return nil }
61 let close = entries.filter { entry in
62 guard let coordinate = entry.coordinate else { return false }
63 return coordinate.distance(to: location.coordinate) <= prefillDistance
64 }
65 return close.count == 1 ? close[0] : nil
66 }
67
68 /// The user's place that `place` is, if any: one with the same MapKit
69 /// ID, or a place from before MapKit (with no MapKit ID) with the same
70 /// name nearby.
71 static func match(_ place: DirectoryPlace, in entries: [VocabularyEntry]) -> VocabularyEntry? {
72 if let entry = entries.first(where: {
73 $0.externalID == ExternalPlaceID(source: .mapKit, value: place.id)
74 }) {
75 return entry
76 }
77 return entries.first { entry in
78 guard entry.externalID?.source != .mapKit, let coordinate = entry.coordinate else {
79 return false
80 }
81 return entry.text.compare(place.name, options: [.caseInsensitive, .diacriticInsensitive])
82 == .orderedSame
83 && coordinate.distance(to: place.coordinate) <= matchDistance
84 }
85 }
86}