-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathValidatable.swift
More file actions
197 lines (144 loc) · 5.21 KB
/
Validatable.swift
File metadata and controls
197 lines (144 loc) · 5.21 KB
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
#if canImport(CoreLocation)
import CoreLocation
#endif
import Foundation
// (Partly) ported from https://github.com/Turfjs/turf/blob/master/packages/turf-boolean-valid
/// Objects that can be validated, i.e. check that they are non-empty.
public protocol ValidatableGeoJson {
/// Check if the geometry is valid, i.e. it has enough coordinates to make sense.
///
/// TODO: Would this be a null geometry?
var isValid: Bool { get }
}
// MARK: - Geometries etc.
extension Feature {
/// Check if the Feature's geometry is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
geometry.isValid
}
/// Check if the GeoJson is a valid Feature.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .feature)
}
}
extension FeatureCollection {
/// Check if the FeatureCollection's Feature is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
features.isEmpty || features.allSatisfy({ $0.isValid })
}
/// Check if the GeoJson is a valid FeatureCollection.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .featureCollection)
}
}
extension GeometryCollection {
/// Check if the geometries are valid, i.e. they have enough coordinates to make sense.
public var isValid: Bool {
geometries.isEmpty || geometries.allSatisfy({ $0.isValid })
}
/// Check if the GeoJson is a valid GeometryCollection.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .geometryCollection)
}
}
extension LineString {
/// Check if the LineString is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
coordinates.count >= 2
}
/// Check if the GeoJson has a valid LineString geometry.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .lineString)
}
}
extension MultiLineString {
/// Check if the MultiLineString is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
!coordinates.isEmpty
&& coordinates[0].count >= 2
}
/// Check if the GeoJson has a valid MultiLineString geometry.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .multiLineString)
}
}
extension Point {
/// Check if the Point is valid. Always **true**.
public var isValid: Bool {
true
}
/// Check if the GeoJson is a valid Point.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .point)
}
}
extension MultiPoint {
/// Check if the MultiPoint is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
!coordinates.isEmpty
}
/// Check if the GeoJson has a valid MultiPoint geometry.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .multiPoint)
}
}
extension Polygon {
/// Check if the Polygon is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
!coordinates.isEmpty
&& coordinates[0].count >= 3
}
/// Check if the GeoJson has a valid Polygon geometry.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .polygon)
}
}
extension MultiPolygon {
/// Check if the MultiPolygon is valid, i.e. it has enough coordinates to make sense.
public var isValid: Bool {
!coordinates.isEmpty
&& !coordinates[0].isEmpty
&& coordinates[0][0].count >= 3
}
/// Check if the GeoJson has a valid MultiPolygon geometry.
public static func isValid(geoJson: [String: Any]) -> Bool {
checkIsValid(geoJson: geoJson, ofType: .multiPolygon)
}
}
// MARK: - GeoJSON
extension GeoJson {
// Sanity checks
// TODO:
/// Check if the GeoJson has a valid geometry, i.e. it has enough coordinates to make sense.
public static func checkIsValid(
geoJson: [String: Any],
ofType expectedType: GeoJsonType? = nil
) -> Bool {
guard !geoJson.isEmpty,
let geometryType = geoJson["type"] as? String,
let type = GeoJsonType(rawValue: geometryType)
else { return false }
if let expectedType = expectedType,
expectedType != type
{
return false
}
switch type {
case .point:
guard let coordinates = geoJson["coordinates"] as? [Any],
!coordinates.isEmpty
else { return false }
return true
case .lineString, .multiLineString, .multiPoint, .multiPolygon, .polygon:
return geoJson["coordinates"] != nil
case .geometryCollection:
return geoJson["geometries"] != nil
case .feature:
return geoJson["geometry"] != nil
case .featureCollection:
return geoJson["features"] != nil
case .invalid:
return false
}
}
}