Skip to content

Commit 7ab2232

Browse files
authored
Make pruning optional as a speed-up (default to enabled) (#7)
(Delete accidentally committed file, too.)
1 parent 0a2beff commit 7ab2232

3 files changed

Lines changed: 41 additions & 47 deletions

File tree

Sources/GeoJSONKit/GeoJSON+Codable.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension GeoJSON.GeometryObject: Codable {
1616
}
1717

1818
public enum CodingError: Error {
19-
case featuresNotSupported
19+
case notAGeometry
2020
case unsupportedCoordinateCount
2121
}
2222

@@ -26,7 +26,7 @@ extension GeoJSON.GeometryObject: Codable {
2626

2727
switch type {
2828
case .feature, .featureCollection:
29-
throw CodingError.featuresNotSupported
29+
throw CodingError.notAGeometry
3030

3131
case .geometryCollection:
3232
let geometries = try container.decode([GeoJSON.GeometryObject].self, forKey: .geometries)
@@ -98,10 +98,10 @@ extension GeoJSON.GeometryObject: Codable {
9898
try container.encode(geometries, forKey: .geometries)
9999

100100
case .single(let geometry):
101-
try addCoordinates(geometry.coordinatesJSON())
101+
try addCoordinates(geometry.coordinatesJSON(prune: false))
102102

103103
case .multi(let geometries):
104-
try addCoordinates(geometries.map { $0.coordinatesJSON() })
104+
try addCoordinates(geometries.map { $0.coordinatesJSON(prune: false) })
105105
}
106106
}
107107
}

Sources/GeoJSONKit/GeoJSON+Serialization.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ public struct GeoJSON: Hashable {
110110
}
111111
}
112112

113-
fileprivate func toJSON() -> [String: Any] {
113+
fileprivate func toJSON(prune: Bool) -> [String: Any] {
114114
var json: [String: Any] = [
115115
"type": type.rawValue
116116
]
117117

118118
switch self {
119119
case .single(let geometry):
120-
json["coordinates"] = geometry.coordinatesJSON()
120+
json["coordinates"] = geometry.coordinatesJSON(prune: prune)
121121
case .multi(let geometries):
122-
json["coordinates"] = geometries.map { $0.coordinatesJSON() }
122+
json["coordinates"] = geometries.map { $0.coordinatesJSON(prune: prune) }
123123
case .collection(let geometries):
124-
json["geometries"] = geometries.map { $0.toJSON() }
124+
json["geometries"] = geometries.map { $0.toJSON(prune: prune) }
125125
}
126126

127127
return json
@@ -262,14 +262,25 @@ public struct GeoJSON: Hashable {
262262
}
263263
}
264264

265-
func coordinatesJSON() -> [Any] {
266-
switch self {
267-
case .point(let position):
268-
return position.toJSON().prune
269-
case .lineString(let lineString):
270-
return lineString.positions.map { $0.toJSON().prune }
271-
case .polygon(let polygon):
272-
return polygon.positionsArray.map { $0.map { $0.toJSON().prune } }
265+
func coordinatesJSON(prune: Bool) -> [Any] {
266+
if prune {
267+
switch self {
268+
case .point(let position):
269+
return position.toJSON().prune
270+
case .lineString(let lineString):
271+
return lineString.positions.map { $0.toJSON().prune }
272+
case .polygon(let polygon):
273+
return polygon.positionsArray.map { $0.map { $0.toJSON().prune } }
274+
}
275+
} else {
276+
switch self {
277+
case .point(let position):
278+
return position.toJSON()
279+
case .lineString(let lineString):
280+
return lineString.positions.map { $0.toJSON() }
281+
case .polygon(let polygon):
282+
return polygon.positionsArray.map { $0.map { $0.toJSON() } }
283+
}
273284
}
274285
}
275286
}
@@ -296,12 +307,12 @@ public struct GeoJSON: Hashable {
296307
id = dict["id"] as? AnyHashable
297308
}
298309

299-
public func toJSON() -> [String: Any] {
310+
public func toJSON(prune: Bool = true) -> [String: Any] {
300311
var json: [String: Any] = [
301312
"type": "Feature",
302-
"geometry": geometry.toJSON()
313+
"geometry": geometry.toJSON(prune: prune)
303314
]
304-
json["properties"] = properties?.prune
315+
json["properties"] = prune ? properties?.prune : properties
305316
json["id"] = id
306317
return json
307318
}
@@ -471,28 +482,31 @@ public struct GeoJSON: Hashable {
471482
}
472483

473484

474-
public func toData(options: JSONSerialization.WritingOptions = []) throws -> Data {
475-
return try JSONSerialization.data(withJSONObject: toJSON(), options: options)
485+
public func toData(options: JSONSerialization.WritingOptions = [], prune: Bool = true) throws -> Data {
486+
return try JSONSerialization.data(withJSONObject: toJSON(prune: prune), options: options)
476487
}
477488

478-
public func toJSON() -> [String: Any] {
489+
public func toJSON(prune: Bool = true) -> [String: Any] {
479490
var json = [String: Any]()
480491

481492
json["type"] = type.rawValue
482-
json["bbox"] = boundingBox?.toJSON().prune
493+
494+
let bbJson = boundingBox?.toJSON()
495+
json["bbox"] = prune ? bbJson?.prune : bbJson
483496

484497
let objectJson: [String: Any]
485498
switch object {
486499
case .feature(let feature):
487-
objectJson = feature.toJSON()
500+
objectJson = feature.toJSON(prune: prune)
488501
case .featureCollection(let features):
489-
objectJson = ["features": features.map { $0.toJSON() }]
502+
objectJson = ["features": features.map { $0.toJSON(prune: prune) }]
490503
case .geometry(let geometry):
491-
objectJson = geometry.toJSON()
504+
objectJson = geometry.toJSON(prune: prune)
492505
}
493506
json.merge(objectJson) { a, _ in a }
494507

495-
json.merge(additionalFields.prune) { a, _ in a }
508+
let additional = prune ? additionalFields.prune : additionalFields
509+
json.merge(additional) { a, _ in a }
496510

497511
return json
498512
}

0 commit comments

Comments
 (0)