-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move OSMTileDataJson out to GeoJsonFeatureCollection
This improves consistency with naming of GeoJsonGeometry and GeoJsonFeature, as well as reducing clutter.
- Loading branch information
Showing
5 changed files
with
60 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// GeoJsonFeatureCollection.swift | ||
// Soundscape | ||
// | ||
// Created by Kai on 11/10/23. | ||
// Copyright © 2023 Soundscape community. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class FailableDecode<T: Decodable>: Decodable { | ||
var result: Result<T, Error> | ||
|
||
public init(from decoder: Decoder) throws { | ||
result = Result(catching: { try T(from: decoder) }) | ||
} | ||
} | ||
|
||
/// Represents the parsed json response from the OSM tiles service | ||
final class GeoJsonFeatureCollection: Decodable { | ||
var features: [GeoJsonFeature] | ||
|
||
private enum CodingKeys: CodingKey { | ||
/// Contains an array of ``GeoJsonFeature``s | ||
case features | ||
/// Should always be `"FeatureCollection"` | ||
case type | ||
} | ||
|
||
enum GeoJsonFeatureCollectionParseError: Error { | ||
/// The `type` property of an ``GeoJsonFeatureCollection`` should always be `"FeatureCollection"` | ||
case incorrectTypeField | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let type = try container.decode(String.self, forKey: .type) | ||
guard type == "FeatureCollection" else { | ||
throw GeoJsonFeatureCollectionParseError.incorrectTypeField | ||
} | ||
|
||
/// Some parsed features may error, since our ``GeoJsonFeature`` implementation requires a name | ||
/// As a result, we simply filter out the failing ones | ||
let parsed_features = try container.decode([FailableDecode<GeoJsonFeature>].self, forKey: .features) | ||
features = parsed_features.compactMap({ | ||
switch $0.result { | ||
case .success(let feature): return feature | ||
case .failure(_): return nil | ||
} | ||
}) | ||
|
||
} | ||
} |