-
Notifications
You must be signed in to change notification settings - Fork 6
/
Polygon.swift
199 lines (153 loc) · 5.7 KB
/
Polygon.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
#if !os(Linux)
import CoreLocation
#endif
import Foundation
/// A GeoJSON `Polygon` object.
public struct Polygon:
PolygonGeometry,
EmptyCreatable
{
public var type: GeoJsonType {
return .polygon
}
public var projection: Projection {
coordinates.first?.first?.projection ?? .noSRID
}
/// The receiver's coordinates.
public let coordinates: [[Coordinate3D]]
public var allCoordinates: [Coordinate3D] {
coordinates.flatMap({ $0 })
}
public var boundingBox: BoundingBox?
public var foreignMembers: [String: Sendable] = [:]
public var polygons: [Polygon] {
return [self]
}
/// The receiver's outer ring.
public var outerRing: Ring? {
guard !coordinates.isEmpty else { return nil }
return Ring(coordinates[0])
}
/// All of the receiver's inner rings.
public var innerRings: [Ring]? {
guard coordinates.count > 1 else { return nil }
return Array(coordinates.suffix(from: 1)).compactMap { Ring($0) }
}
/// All of the receiver's rings (outer + inner).
public var rings: [Ring] {
return coordinates.compactMap { Ring($0) }
}
public init() {
self.coordinates = []
}
/// Try to initialize a Polygon with some coordinates.
public init?(_ coordinates: [[Coordinate3D]], calculateBoundingBox: Bool = false) {
guard !coordinates.isEmpty,
coordinates[0].count >= 3
else { return nil }
self.init(unchecked: coordinates, calculateBoundingBox: calculateBoundingBox)
}
/// Try to initialize a Polygon with some coordinates, don't check the coordinates for validity.
public init(unchecked coordinates: [[Coordinate3D]], calculateBoundingBox: Bool = false) {
self.coordinates = coordinates
if calculateBoundingBox {
self.boundingBox = self.calculateBoundingBox()
}
}
/// Try to initialize a Polygon with some Rings.
public init?(_ rings: [Ring], calculateBoundingBox: Bool = false) {
guard !rings.isEmpty else { return nil }
self.init(unchecked: rings, calculateBoundingBox: calculateBoundingBox)
}
/// Try to initialize a Polygon with some Rings, don't check the coordinates for validity.
public init(unchecked rings: [Ring], calculateBoundingBox: Bool = false) {
self.coordinates = rings.map { $0.coordinates }
if calculateBoundingBox {
self.boundingBox = self.calculateBoundingBox()
}
}
public init?(json: Any?) {
self.init(json: json, calculateBoundingBox: false)
}
public init?(json: Any?, calculateBoundingBox: Bool = false) {
guard let geoJson = json as? [String: Sendable],
Polygon.isValid(geoJson: geoJson),
let coordinates: [[Coordinate3D]] = Polygon.tryCreate(json: geoJson["coordinates"])
else { return nil }
self.coordinates = coordinates
self.boundingBox = Polygon.tryCreate(json: geoJson["bbox"])
if calculateBoundingBox && self.boundingBox == nil {
self.boundingBox = self.calculateBoundingBox()
}
if geoJson.count > 2 {
var foreignMembers = geoJson
foreignMembers.removeValue(forKey: "type")
foreignMembers.removeValue(forKey: "coordinates")
foreignMembers.removeValue(forKey: "bbox")
self.foreignMembers = foreignMembers
}
}
public var asJson: [String: Any] {
var result: [String: Any] = [
"type": GeoJsonType.polygon.rawValue,
"coordinates": coordinates.map { $0.map { $0.asJson } }
]
if let boundingBox = boundingBox {
result["bbox"] = boundingBox.asJson
}
result.merge(foreignMembers) { (current, new) in
return current
}
return result
}
}
// MARK: - Projection
extension Polygon {
public func projected(to newProjection: Projection) -> Polygon {
guard newProjection != projection else { return self }
var polygon = Polygon(
unchecked: coordinates.map({ $0.map({ $0.projected(to: newProjection) }) }),
calculateBoundingBox: (boundingBox != nil))
polygon.foreignMembers = foreignMembers
return polygon
}
}
// MARK: - CoreLocation compatibility
#if !os(Linux)
extension Polygon {
/// Try to initialize a Polygon with some coordinates.
public init?(_ coordinates: [[CLLocationCoordinate2D]], calculateBoundingBox: Bool = false) {
self.init(coordinates.map({ $0.map({ Coordinate3D($0) }) }), calculateBoundingBox: calculateBoundingBox)
}
/// Try to initialize a Polygon with some locations.
public init?(_ coordinates: [[CLLocation]], calculateBoundingBox: Bool = false) {
self.init(coordinates.map({ $0.map({ Coordinate3D($0) }) }), calculateBoundingBox: calculateBoundingBox)
}
}
#endif
// MARK: - BoundingBox
extension Polygon {
public func calculateBoundingBox() -> BoundingBox? {
guard let coordinates = outerRing?.coordinates else { return nil }
return BoundingBox(coordinates: coordinates)
}
public func intersects(_ otherBoundingBox: BoundingBox) -> Bool {
if let boundingBox = boundingBox ?? calculateBoundingBox(),
!boundingBox.intersects(otherBoundingBox)
{
return false
}
return outerRing?.intersects(otherBoundingBox) ?? false
}
}
extension Polygon: Equatable {
public static func ==(
lhs: Polygon,
rhs: Polygon)
-> Bool
{
// TODO: The coordinats might be shifted (like [1, 2, 3] => [3, 1, 2])
return lhs.projection == rhs.projection
&& lhs.coordinates == rhs.coordinates
}
}