@@ -65,6 +65,24 @@ extension Longitude: Codable {
65
65
66
66
}
67
67
68
+ extension Altitude : Codable {
69
+
70
+ public init ( from decoder: Decoder ) throws {
71
+ let container = try decoder. singleValueContainer ( )
72
+
73
+ let meters = try container. decode ( Double . self)
74
+
75
+ self . init ( meters: meters)
76
+ }
77
+
78
+ public func encode( to encoder: Encoder ) throws {
79
+ var container = encoder. singleValueContainer ( )
80
+
81
+ try container. encode ( self . meters. roundedToPlaces ( 3 ) )
82
+ }
83
+
84
+ }
85
+
68
86
extension Position2D : Codable {
69
87
70
88
public init ( from decoder: Decoder ) throws {
@@ -85,6 +103,28 @@ extension Position2D: Codable {
85
103
86
104
}
87
105
106
+ extension Position3D : Codable {
107
+
108
+ public init ( from decoder: Decoder ) throws {
109
+ var container = try decoder. unkeyedContainer ( )
110
+
111
+ let longitude = try container. decode ( Longitude . self)
112
+ let latitude = try container. decode ( Latitude . self)
113
+ let altitude = try container. decode ( Altitude . self)
114
+
115
+ self . init ( latitude: latitude, longitude: longitude, altitude: altitude)
116
+ }
117
+
118
+ public func encode( to encoder: Encoder ) throws {
119
+ var container = encoder. unkeyedContainer ( )
120
+
121
+ try container. encode ( self . longitude)
122
+ try container. encode ( self . latitude)
123
+ try container. encode ( self . altitude)
124
+ }
125
+
126
+ }
127
+
88
128
extension LinearRingCoordinates {
89
129
90
130
public init ( from decoder: Decoder ) throws {
@@ -147,34 +187,59 @@ extension AnyGeometry {
147
187
148
188
let container = try decoder. singleValueContainer ( )
149
189
150
- // FIXME : Fix 2D/3D performance by checking the number of values in `bbox`
190
+ // TODO : Fix 2D/3D performance by checking the number of values in `bbox`
151
191
switch type {
152
192
case . geometryCollection:
153
193
let geometryCollection = try container. decode ( GeometryCollection . self)
154
194
self = . geometryCollection( geometryCollection)
155
195
case . point:
156
- // do {
157
- // let point3D = try container.decode(Point3D.self)
158
- // self = .point3D(point3D)
159
- // } catch {
196
+ do {
197
+ let point3D = try container. decode ( Point3D . self)
198
+ self = . point3D( point3D)
199
+ } catch {
160
200
let point2D = try container. decode ( Point2D . self)
161
201
self = . point2D( point2D)
162
- // }
202
+ }
163
203
case . multiPoint:
164
- let multiPoint2D = try container. decode ( MultiPoint2D . self)
165
- self = . multiPoint2D( multiPoint2D)
204
+ do {
205
+ let multiPoint3D = try container. decode ( MultiPoint3D . self)
206
+ self = . multiPoint3D( multiPoint3D)
207
+ } catch {
208
+ let multiPoint2D = try container. decode ( MultiPoint2D . self)
209
+ self = . multiPoint2D( multiPoint2D)
210
+ }
166
211
case . lineString:
167
- let lineString2D = try container. decode ( LineString2D . self)
168
- self = . lineString2D( lineString2D)
212
+ do {
213
+ let lineString3D = try container. decode ( LineString3D . self)
214
+ self = . lineString3D( lineString3D)
215
+ } catch {
216
+ let lineString2D = try container. decode ( LineString2D . self)
217
+ self = . lineString2D( lineString2D)
218
+ }
169
219
case . multiLineString:
170
- let multiLineString2D = try container. decode ( MultiLineString2D . self)
171
- self = . multiLineString2D( multiLineString2D)
220
+ do {
221
+ let multiLineString3D = try container. decode ( MultiLineString3D . self)
222
+ self = . multiLineString3D( multiLineString3D)
223
+ } catch {
224
+ let multiLineString2D = try container. decode ( MultiLineString2D . self)
225
+ self = . multiLineString2D( multiLineString2D)
226
+ }
172
227
case . polygon:
173
- let polygon2D = try container. decode ( Polygon2D . self)
174
- self = . polygon2D( polygon2D)
228
+ do {
229
+ let polygon3D = try container. decode ( Polygon3D . self)
230
+ self = . polygon3D( polygon3D)
231
+ } catch {
232
+ let polygon2D = try container. decode ( Polygon2D . self)
233
+ self = . polygon2D( polygon2D)
234
+ }
175
235
case . multiPolygon:
176
- let multiPolygon2D = try container. decode ( MultiPolygon2D . self)
177
- self = . multiPolygon2D( multiPolygon2D)
236
+ do {
237
+ let multiPolygon3D = try container. decode ( MultiPolygon3D . self)
238
+ self = . multiPolygon3D( multiPolygon3D)
239
+ } catch {
240
+ let multiPolygon2D = try container. decode ( MultiPolygon2D . self)
241
+ self = . multiPolygon2D( multiPolygon2D)
242
+ }
178
243
}
179
244
}
180
245
@@ -184,6 +249,7 @@ extension AnyGeometry {
184
249
switch self {
185
250
case . geometryCollection( let geometryCollection) :
186
251
try container. encode ( geometryCollection)
252
+
187
253
case . point2D( let point2D) :
188
254
try container. encode ( point2D)
189
255
case . multiPoint2D( let multiPoint2D) :
@@ -196,6 +262,19 @@ extension AnyGeometry {
196
262
try container. encode ( polygon2D)
197
263
case . multiPolygon2D( let multiPolygon2D) :
198
264
try container. encode ( multiPolygon2D)
265
+
266
+ case . point3D( let point3D) :
267
+ try container. encode ( point3D)
268
+ case . multiPoint3D( let multiPoint3D) :
269
+ try container. encode ( multiPoint3D)
270
+ case . lineString3D( let lineString3D) :
271
+ try container. encode ( lineString3D)
272
+ case . multiLineString3D( let multiLineString3D) :
273
+ try container. encode ( multiLineString3D)
274
+ case . polygon3D( let polygon3D) :
275
+ try container. encode ( polygon3D)
276
+ case . multiPolygon3D( let multiPolygon3D) :
277
+ try container. encode ( multiPolygon3D)
199
278
}
200
279
}
201
280
@@ -228,13 +307,57 @@ extension BoundingBox2D {
228
307
229
308
}
230
309
310
+ extension BoundingBox3D {
311
+
312
+ public init ( from decoder: Decoder ) throws {
313
+ var container = try decoder. unkeyedContainer ( )
314
+
315
+ let westLongitude = try container. decode ( Longitude . self)
316
+ let southLatitude = try container. decode ( Latitude . self)
317
+ let lowAltitude = try container. decode ( Altitude . self)
318
+ let eastLongitude = try container. decode ( Longitude . self)
319
+ let northLatitude = try container. decode ( Latitude . self)
320
+ let highAltitude = try container. decode ( Altitude . self)
321
+
322
+ self . init (
323
+ southWestLow: Coordinate3D (
324
+ latitude: southLatitude,
325
+ longitude: westLongitude,
326
+ altitude: lowAltitude
327
+ ) ,
328
+ northEastHigh: Coordinate3D (
329
+ latitude: northLatitude,
330
+ longitude: eastLongitude,
331
+ altitude: highAltitude
332
+ )
333
+ )
334
+ }
335
+
336
+ public func encode( to encoder: Encoder ) throws {
337
+ var container = encoder. unkeyedContainer ( )
338
+
339
+ try container. encode ( self . twoDimensions. westLongitude)
340
+ try container. encode ( self . twoDimensions. southLatitude)
341
+ try container. encode ( self . lowAltitude)
342
+ try container. encode ( self . twoDimensions. eastLongitude)
343
+ try container. encode ( self . twoDimensions. northLatitude)
344
+ try container. encode ( self . highAltitude)
345
+ }
346
+
347
+ }
348
+
231
349
extension AnyBoundingBox {
232
350
233
351
public init ( from decoder: Decoder ) throws {
234
352
let container = try decoder. singleValueContainer ( )
235
353
236
- let boundingBox2D = try container. decode ( BoundingBox2D . self)
237
- self = . twoDimensions( boundingBox2D)
354
+ do {
355
+ let boundingBox3D = try container. decode ( BoundingBox3D . self)
356
+ self = . threeDimensions( boundingBox3D)
357
+ } catch {
358
+ let boundingBox2D = try container. decode ( BoundingBox2D . self)
359
+ self = . twoDimensions( boundingBox2D)
360
+ }
238
361
}
239
362
240
363
public func encode( to encoder: Encoder ) throws {
@@ -243,6 +366,8 @@ extension AnyBoundingBox {
243
366
switch self {
244
367
case . twoDimensions( let boundingBox2D) :
245
368
try container. encode ( boundingBox2D)
369
+ case . threeDimensions( let boundingBox3D) :
370
+ try container. encode ( boundingBox3D)
246
371
}
247
372
}
248
373
0 commit comments