Skip to content

Commit

Permalink
Update to use XmlJson 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
anconaesselmann committed Aug 26, 2021
1 parent 838238e commit cbf442d
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 85 deletions.
16 changes: 7 additions & 9 deletions Example/Example.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import CoreLocation
print("Instantiating GPX objects:")

let locations: [CLLocation] = [
CLLocation(lat: 38.1237270, lon: -119.4670500, alt: 2899.8, dateString: "2021-06-03T20:25:26Z"),
CLLocation(lat: 38.1237330, lon: -119.4670490, alt: 2899.8, dateString: "2021-06-03T20:25:27Z"),
CLLocation(lat: 38.1237360, lon: -119.4670510, alt: 2899.8, dateString: "2021-06-03T20:25:28Z"),
CLLocation(lat: 38.1237360, lon: -119.4670500, alt: 2899.8, dateString: "2021-06-03T20:25:29Z"),
CLLocation(lat: 38.1237360, lon: -119.4670500, alt: 2899.8, dateString: "2021-06-03T20:25:30Z"),
CLLocation(lat: 38.1237360, lon: -119.4670510, alt: 2899.8, dateString: "2021-06-03T20:25:31Z")
CLLocation(lat: 38.1237270, lon: -119.4670500, alt: 2899.8, timestamp: "2021-06-03T20:25:26Z"),
CLLocation(lat: 38.1237330, lon: -119.4670490, alt: 2899.8, timestamp: "2021-06-03T20:25:27Z"),
CLLocation(lat: 38.1237360, lon: -119.4670510, alt: 2899.8, timestamp: "2021-06-03T20:25:28Z"),
CLLocation(lat: 38.1237360, lon: -119.4670500, alt: 2899.8, timestamp: "2021-06-03T20:25:29Z"),
CLLocation(lat: 38.1237360, lon: -119.4670500, alt: 2899.8, timestamp: "2021-06-03T20:25:30Z"),
CLLocation(lat: 38.1237360, lon: -119.4670510, alt: 2899.8, timestamp: "2021-06-03T20:25:31Z")
].compactMap { $0 }

print()
print("With an array of CLLocation instances:")
let trackFromLocations = GPX(name: "My Track", locations: locations)
print(trackFromLocations)

let trackFromLocations = GPX(locations: locations)

let gpxString = """
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
Expand Down
2 changes: 1 addition & 1 deletion SwiftGpx.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SwiftGpx'
s.version = '1.0.0'
s.version = '1.0.1'
s.summary = 'SwiftGpx is a small library for reading amd writing GPX files'
s.description = <<-DESC
SwiftGpx is a small library for reading amd writing GPX files.
Expand Down
Empty file removed SwiftGpx/Classes/.gitkeep
Empty file.
35 changes: 0 additions & 35 deletions SwiftGpx/Classes/DateHelpers.swift

This file was deleted.

18 changes: 9 additions & 9 deletions SwiftGpx/Classes/Extensions/CoreLocation+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import CoreLocation

public extension CLLocationCoordinate2D {
init?(gpxJson: [String: Any]) {
init?(gpxJson: JsonDictionary) {
guard
let lat = gpxJson[Keys.lat] as? CLLocationDegrees,
let lon = gpxJson[Keys.lon] as? CLLocationDegrees
Expand All @@ -14,12 +14,12 @@ public extension CLLocationCoordinate2D {
}

public extension CLLocation {
convenience init?(gpxJson: [String: Any]) {
convenience init?(gpxJson: JsonDictionary) {
guard
let coordinate = CLLocationCoordinate2D(gpxJson: gpxJson),
let altitude = gpxJson[Keys.ele] as? CLLocationDistance,
let timeString = gpxJson[Keys.time] as? String,
let timestamp = timeString.iso8601Date
let timestamp = GPX.fromISO8601Timestamp(timeString)
else { return nil }
self.init(coordinate: coordinate, altitude: altitude, horizontalAccuracy: kCLLocationAccuracyBest, verticalAccuracy: kCLLocationAccuracyBest, timestamp: timestamp)
}
Expand All @@ -33,8 +33,8 @@ public extension CLLocation {
}

public extension CLLocation {
convenience init?(lat: Double, lon: Double, alt: Double, dateString: String) {
guard let date = dateString.iso8601Date else {
convenience init?(lat: Double, lon: Double, alt: Double, timestamp: String) {
guard let date = GPX.fromISO8601Timestamp(timestamp) else {
return nil
}
let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
Expand All @@ -43,7 +43,7 @@ public extension CLLocation {
}

public extension CLLocationCoordinate2D {
var gpxJson: [String: Any] {
var gpxJson: JsonDictionary {
return [
Keys.lat: latitude,
Keys.lon: longitude
Expand All @@ -52,16 +52,16 @@ public extension CLLocationCoordinate2D {
}

public extension CLLocation {
var gpxJson: [String: Any] {
var gpxJson: JsonDictionary {
var result = coordinate.gpxJson
result[Keys.ele] = altitude
result[Keys.time] = timestamp.iso8601DateString
result[Keys.time] = GPX.toISO8601Timestamp(timestamp)
return result
}
}

public extension Array where Element == CLLocation {
var gpxJson: [[String: Any]] {
var gpxJson: [JsonDictionary] {
map { $0.gpxJson }
}
}
13 changes: 13 additions & 0 deletions SwiftGpx/Classes/Extensions/JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Created by Axel Ancona Esselmann on 8/24/21.
//

import Foundation

public typealias JsonDictionary = [String: Any]

public extension Dictionary where Key == String {

subscript(json key: String) -> JsonDictionary? {
self[key] as? JsonDictionary
}
}
6 changes: 3 additions & 3 deletions SwiftGpx/Classes/GPX/GPX+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import Foundation

public extension LocationTrackSegment {
var gpxJson: [[String: Any]] {
var gpxJson: [JsonDictionary] {
return locations.gpxJson
}
}

public extension LocationTrack {
var gpxJson: [String: Any] {
var gpxJson: JsonDictionary {
return [
Keys.name: name,
Keys.trksegElements: trackSegments.map { $0.gpxJson }
Expand All @@ -19,7 +19,7 @@ public extension LocationTrack {
}

public extension GPX {
var gpxJson: [String: Any] {
var gpxJson: JsonDictionary {
return [
Keys.gpx: track.gpxJson
]
Expand Down
43 changes: 30 additions & 13 deletions SwiftGpx/Classes/GPX/GPX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ public struct GPX: Codable {

public var data: Data? { xmlString.data(using: .utf8) }

public init(name: String, locations: [CLLocation]) {
self.init(name: name, locations: [locations])
public init(name: String? = nil, locations: [[CLLocation]]) {
self.init(track: LocationTrack(
name: name,
trackSegments: locations.map { LocationTrackSegment(locations: $0) }
))
}

public init(name: String, locations: [[CLLocation]]) {
self.init(
track: LocationTrack(
name: name,
timestamp: locations.first?.first?.timestamp ?? Date(),
trackSegments: locations.map { LocationTrackSegment(locations: $0) }
)
)
public init(name: String? = nil, locations: [CLLocation]) {
self.init(name: name, locations: [locations])
}

public init(track: LocationTrack) {
self.track = track
}

public init?(gpxJson: [String: Any]) {
public init?(gpxJson: JsonDictionary) {
guard
let gpxDict = gpxJson[Keys.gpx] as? [String: Any],
let trackJson = gpxDict[Keys.trk] as? [String: Any],
let gpxDict = gpxJson[json: Keys.gpx],
let trackJson = gpxDict[json: Keys.trk],
let track = LocationTrack(gpxJson: trackJson)
else { return nil }
self.init(track: track)
Expand Down Expand Up @@ -62,6 +59,8 @@ public struct GPX: Codable {
return data.dataToFile(fileName: fileName)
}

// MARK: Codable

enum CodingKeys: CodingKey {
case name, trackSegments
}
Expand Down Expand Up @@ -97,5 +96,23 @@ public struct GPX: Codable {
try container.encode(trackSegments, forKey: .trackSegments)
try container.encode(track.name, forKey: .name)
}

// MARK: - Date conversion

private static let timestampFormat: String = "yyyy-MM-dd'T'HH:mm:ssZ"

private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = timestampFormat
return dateFormatter
}()

public static func toISO8601Timestamp(_ date: Date) -> String {
dateFormatter.string(from: date)
}

public static func fromISO8601Timestamp(_ timestamp: String) -> Date? {
dateFormatter.date(from: timestamp)
}
}

27 changes: 15 additions & 12 deletions SwiftGpx/Classes/GPX/LocationTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ public struct LocationTrack {
return trackSegments.flatMap { $0.locations }
}

public init(name: String, timestamp: Date, trackSegments: [LocationTrackSegment] = []) {
self.name = name
self.timestamp = timestamp
self.trackSegments = trackSegments
}

public init(name: String, trackSegments: [LocationTrackSegment] = []) {
self.name = name
self.timestamp = trackSegments.first?.locations.first?.timestamp ?? Date()
public init(name: String? = nil, timestamp: Date? = nil, trackSegments: [LocationTrackSegment] = []) {
let timestamp = timestamp ?? trackSegments.last.flatMap({ $0.locations })?.first?.timestamp
if let trackName = name {
self.name = trackName
} else {
if let timestamp = timestamp {
self.name = "Track from \(GPX.toISO8601Timestamp(timestamp))"
} else {
self.name = "Untitled Track"
}
}
self.timestamp = timestamp ?? Date()
self.trackSegments = trackSegments
}

public init?(gpxJson: [String: Any]) {
public init?(gpxJson: JsonDictionary) {
guard
let trksegElements = gpxJson[Keys.trksegElements] as? [[String: Any]],
let trksegElements = gpxJson[Keys.trksegElements] as? [JsonDictionary],
let name = gpxJson[Keys.name] as? String
else { return nil }
if
let timeString = gpxJson[Keys.time] as? String,
let timestamp = timeString.iso8601Date
let timestamp = GPX.fromISO8601Timestamp(timeString)
{
self.init(name: name, timestamp: timestamp, trackSegments: trksegElements.compactMap(LocationTrackSegment.init(gpxJson:)))
} else {
Expand Down
4 changes: 2 additions & 2 deletions SwiftGpx/Classes/GPX/LocationTrackSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public struct LocationTrackSegment {
self.locations = locations
}

public init?(gpxJson: [String: Any]) {
guard let trkptElements = gpxJson[Keys.trkptElements] as? [[String: Any]] else { return nil }
public init?(gpxJson: JsonDictionary) {
guard let trkptElements = gpxJson[Keys.trkptElements] as? [JsonDictionary] else { return nil }
self.init(locations: trkptElements.compactMap(CLLocation.init(gpxJson:)))
}
}
7 changes: 6 additions & 1 deletion SwiftGpx/Classes/XML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

import CoreLocation
import XmlJson
import Foundation

fileprivate extension String {
extension String {
static let dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
static let schemaDocumentationUrl = "http://www.topografix.com/GPX/1/1"
static let schemaUri = "/gpx.xsd"
Expand Down Expand Up @@ -125,4 +126,8 @@ public extension GPX {
var xmlString: String {
.xmlProlog + xmlTag.stringValue
}

var gpxString: String {
xmlString
}
}

0 comments on commit cbf442d

Please sign in to comment.