-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLineOverlap.swift
257 lines (216 loc) · 8.43 KB
/
LineOverlap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#if !os(Linux)
import CoreLocation
#endif
import Foundation
// Ported from https://github.com/Turfjs/turf/blob/master/packages/turf-line-overlap
extension LineSegment {
/// Indicates how one segment compares to another segment.
public enum LineSegmentComparisonResult: Sendable {
case equal
case notEqual
case thisOnOther
case otherOnThis
}
/// Checks how the receiver and the other *LineSegment* lie in relation to each other.
///
/// - Parameters:
/// - other: The other *LineSegment*
/// - tolerance: The tolerance, in meters
public func compare(
other: LineSegment,
tolerance: CLLocationDistance = 0.0)
-> LineSegmentComparisonResult
{
let other = other.projected(to: projection)
let tolerance = abs(tolerance)
if (first == other.first && second == other.second)
|| (first == other.second && second == other.first)
{
return .equal
}
else if tolerance == 0.0 {
if other.checkIsOnSegment(first), other.checkIsOnSegment(second) {
return .thisOnOther
}
else if checkIsOnSegment(other.first), checkIsOnSegment(other.second) {
return .otherOnThis
}
}
else if tolerance > 0.0 {
if other.nearestCoordinateOnSegment(from: first).distance <= tolerance,
other.nearestCoordinateOnSegment(from: second).distance <= tolerance
{
return .thisOnOther
}
else if nearestCoordinateOnSegment(from: other.first).distance <= tolerance,
nearestCoordinateOnSegment(from: other.second).distance <= tolerance
{
return .otherOnThis
}
}
return .notEqual
}
}
extension GeoJson {
/// Returns the overlapping segments between the receiver and the other geometry.
///
/// - Parameters:
/// - other: The other geometry
/// - tolerance: The tolerance, in meters
public func overlappingSegments(
with other: GeoJson,
tolerance: CLLocationDistance = 0.0)
-> [LineSegment]
{
let other = other.projected(to: projection)
let tolerance = abs(tolerance)
var result: [LineSegment] = []
let tree: RTree<LineSegment> = RTree(lineSegments)
for segment in other.lineSegments {
guard let boundingBox = segment.boundingBox ?? segment.calculateBoundingBox() else { continue }
for match in tree.search(inBoundingBox: boundingBox) {
let comparison = segment.compare(other: match, tolerance: tolerance)
if comparison == .equal {
result.append(segment)
break
}
else if comparison == .thisOnOther {
result.append(segment)
break
}
else if comparison == .otherOnThis {
result.append(match)
}
}
}
return result
}
/// Returns the overlapping segments with the receiver itself.
///
/// This implementation has been optimized for finding self-overlaps.
///
/// - Note: Altitude values will be ignored.
///
/// - Parameters:
/// - tolerance: The tolerance, in meters. Using `0.0` will only return segments that *exactly* overlap.
/// - segmentLength: This value adds intermediate points to the geometry for improved matching, in meters. Choosing this too small might lead to memory explosion.
///
/// - Returns: All segments that at least overlap with one other segment. Each segment will
/// appear in the result only once.
public func overlappingSegments(
tolerance: CLLocationDistance,
segmentLength: Double? = nil
) -> MultiLineString? {
let tolerance = abs(tolerance)
let distanceFunction = FrechetDistanceFunction.haversine
guard let line = if let segmentLength, segmentLength > 0.0 {
LineString(lineSegments)?.evenlyDivided(segmentLength: segmentLength)
}
else {
LineString(lineSegments)
}
else {
return nil
}
let p = line.allCoordinates
var ca: [OrderedIndexPair: Double] = [:]
func index(_ pI: Int, _ qI: Int) -> OrderedIndexPair {
.init(pI, qI)
}
// Distances between each coordinate pair
for i in 0 ..< p.count {
for j in i + 1 ..< p.count {
let distance = distanceFunction.distance(between: p[i], and: p[j])
if distance > tolerance { continue }
ca[index(i, j)] = distance
}
}
// Find coordinate pairs within the tolerance
var pairs: Set<OrderedIndexPair> = []
var i = 0
outer: while i < p.count - 1 {
defer { i += 1 }
var j = i + 2
while ca[index(i, j), default: Double.greatestFiniteMagnitude] <= tolerance {
j += 1
if j == p.count { break outer }
}
while j < p.count {
defer { j += 1 }
if ca[index(i, j), default: Double.greatestFiniteMagnitude] <= tolerance {
pairs.insert(index(i, j))
}
}
}
// Find overlapping segments
var scratchList = pairs.sorted()
var result: Set<OrderedIndexPair> = []
while scratchList.isNotEmpty {
let candidate = scratchList.removeFirst()
if candidate.first > 0,
candidate.second > 0,
pairs.contains(index(candidate.first - 1, candidate.second - 1))
{
result.insert(index(candidate.first, candidate.first - 1))
result.insert(index(candidate.second, candidate.second - 1))
continue
}
if candidate.first > 0,
candidate.second < p.count - 1,
pairs.contains(index(candidate.first - 1, candidate.second + 1))
{
result.insert(index(candidate.first, candidate.first - 1))
result.insert(index(candidate.second, candidate.second + 1))
continue
}
if candidate.first < p.count - 1,
candidate.second > 0,
pairs.contains(index(candidate.first + 1, candidate.second - 1))
{
result.insert(index(candidate.first, candidate.first + 1))
result.insert(index(candidate.second, candidate.second - 1))
continue
}
if candidate.first < p.count - 1,
candidate.second < p.count - 1,
pairs.contains(index(candidate.first + 1, candidate.second + 1))
{
result.insert(index(candidate.first, candidate.first + 1))
result.insert(index(candidate.second, candidate.second + 1))
continue
}
}
return MultiLineString(result.map({ LineString(unchecked: [p[$0.first], p[$0.second]]) }))
}
/// An estimate of how much the receiver overlaps with itself.
///
/// - Parameters:
/// - tolerance: The tolerance, in meters. Using `0.0` will only count segments that *exactly* overlap.
/// - segmentLength: This value adds intermediate points to the geometry for improved matching, in meters. Choosing this too small might lead to memory explosion.
///
/// - Returns: The length of all segments that overlap within `tolerance`.
public func estimatedOverlap(
tolerance: CLLocationDistance,
segmentLength: Double? = nil
) -> Double {
guard let result = overlappingSegments(tolerance: tolerance, segmentLength: segmentLength) else { return 0.0 }
return result.length
}
}
// MARK: - Private
private struct OrderedIndexPair: Hashable, Comparable, CustomStringConvertible {
let first: Int
let second: Int
init(_ first: Int, _ second: Int) {
self.first = min(first, second)
self.second = max(first, second)
}
var description: String {
"(\(first)-\(second))"
}
static func < (lhs: OrderedIndexPair, rhs: OrderedIndexPair) -> Bool {
if lhs.first < rhs.first { return true }
if lhs.first > rhs.first { return false }
return lhs.second < rhs.second
}
}