Skip to content

Commit

Permalink
🚧 WIP Refactor GeoModels thanks to Swift 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiBardon committed Sep 26, 2022
1 parent 179f785 commit 9d86ba4
Show file tree
Hide file tree
Showing 34 changed files with 647 additions and 767 deletions.
50 changes: 40 additions & 10 deletions Sources/GeoCoordinates/Coordinate2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,55 @@

import Foundation

public struct Coordinate2D: Hashable {
public protocol Coordinate2D<X, Y>: Coordinates {

associatedtype X: Coordinate
associatedtype Y: Coordinate

var x: X { get set }
var y: Y { get set }

init(x: X, y: Y)

func offsetBy(dx: X, dy: Y) -> Self

}

public extension Coordinate2D {

func offsetBy(dx: X = .zero, dy: Y = .zero) -> Self {
self.offsetBy(dx: dx, dy: dy)
}

}

public struct WGS84Coordinate2D: Coordinate2D {

public typealias X = Longitude
public typealias Y = Latitude

public static var zero: Coordinate2D {
public static var zero: Self {
Self.init(latitude: .zero, longitude: .zero)
}

public var latitude: Latitude
public var longitude: Longitude

public var x: X { longitude }
public var y: Y { latitude }

public var x: X {
get { longitude }
set { longitude = newValue }
}
public var y: Y {
get { latitude }
set { latitude = newValue }
}

public var west: X { longitude }
public var east: X { longitude }
public var south: Y { latitude }
public var north: Y { latitude }

public var withPositiveLongitude: Coordinate2D {
public var withPositiveLongitude: Self {
Self.init(latitude: latitude, longitude: longitude.positive)
}

Expand All @@ -41,18 +69,19 @@ public struct Coordinate2D: Hashable {
self.init(latitude: y, longitude: x)
}

public func offsetBy(dLat: Latitude = 0, dLong: Longitude = 0) -> Coordinate2D {
public func offsetBy(dLat: Latitude = 0, dLong: Longitude = 0) -> Self {
Self.init(latitude: latitude + dLat, longitude: longitude + dLong)
}
public func offsetBy(dx: X = .zero, dy: Y = .zero) -> Coordinate2D {
public func offsetBy(dx: X, dy: Y) -> Self {
Self.init(x: x + dx, y: y + dy)
}

}

// MARK: - Operators

extension Coordinate2D: AdditiveArithmetic {
/// ``Foundation/AdditiveArithmetic``
extension Coordinate2D {

public static func + (lhs: Self, rhs: Self) -> Self {
return lhs.offsetBy(dx: rhs.x, dy: rhs.y)
Expand All @@ -72,7 +101,8 @@ extension Coordinate2D {

}

extension Coordinate2D: Coordinates {
/// ``GeoCoordinates/Coordinates``
extension Coordinate2D {

public init<N: BinaryFloatingPoint>(repeating number: N) {
self.init(x: Self.X(number), y: Self.Y(number))
Expand Down
74 changes: 55 additions & 19 deletions Sources/GeoCoordinates/Coordinate3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,59 @@
// Copyright © 2022 Rémi Bardon. All rights reserved.
//

public struct Coordinate3D: Hashable {
public protocol Coordinate3D<X, Y, Z>: Coordinates, CompoundDimension {

associatedtype X: Coordinate
associatedtype Y: Coordinate
associatedtype Z: Coordinate

var x: X { get set }
var y: Y { get set }
var z: Z { get set }

init(x: X, y: Y, z: Z)

func offsetBy(dx: X, dy: Y, dz: Z) -> Self

}

public extension Coordinate3D {

func offsetBy(dx: X = .zero, dy: Y = .zero, dz: Z = .zero) -> Self {
self.offsetBy(dx: dx, dy: dy, dz: dz)
}

}

public struct WGS84Coordinate3D: Coordinate3D {

public typealias X = Longitude
public typealias Y = Latitude
public typealias Z = Altitude

public static var zero: Coordinate3D {
Coordinate3D(latitude: .zero, longitude: .zero, altitude: .zero)
public static var zero: Self {
Self.init(latitude: .zero, longitude: .zero, altitude: .zero)
}

public var latitude: Latitude
public var longitude: Longitude
public var altitude: Altitude

public var x: X { longitude }
public var y: Y { latitude }
public var z: Z { altitude }

public var withPositiveLongitude: Coordinate3D {
Coordinate3D(latitude: latitude, longitude: longitude.positive, altitude: altitude)

public var x: X {
get { longitude }
set { longitude = newValue }
}
public var y: Y {
get { latitude }
set { latitude = newValue }
}
public var z: Z {
get { altitude }
set { altitude = newValue }
}

public var withPositiveLongitude: Self {
Self.init(latitude: latitude, longitude: longitude.positive, altitude: altitude)
}

public init(
Expand All @@ -42,26 +75,27 @@ public struct Coordinate3D: Hashable {
self.init(latitude: y, longitude: x, altitude: z)
}

public init(_ coordinate2d: Coordinate2D, altitude: Altitude = .zero) {
public init(_ coordinate2d: WGS84Coordinate2D, altitude: Altitude = .zero) {
self.init(latitude: coordinate2d.latitude, longitude: coordinate2d.longitude, altitude: altitude)
}

public func offsetBy(dLat: Latitude = .zero, dLong: Longitude = .zero, dAlt: Altitude = .zero) -> Coordinate3D {
Coordinate3D(
public func offsetBy(dLat: Latitude = .zero, dLong: Longitude = .zero, dAlt: Altitude = .zero) -> Self {
Self.init(
latitude: latitude + dLat,
longitude: longitude + dLong,
altitude: altitude + dAlt
)
}
public func offsetBy(dx: X = .zero, dy: Y = .zero, dz: Z = .zero) -> Coordinate3D {
Coordinate3D(x: x + dx, y: y + dy, z: z + dz)
public func offsetBy(dx: X, dy: Y, dz: Z) -> Self {
Self.init(x: x + dx, y: y + dy, z: z + dz)
}

}

// MARK: - Operators

extension Coordinate3D: AdditiveArithmetic {
/// ``Foundation/AdditiveArithmetic``
extension Coordinate3D {

public static func + (lhs: Self, rhs: Self) -> Self {
return lhs.offsetBy(dx: rhs.x, dy: rhs.y, dz: rhs.z)
Expand All @@ -87,17 +121,19 @@ extension Coordinate3D {

// MARK: - Protocol conformances

extension Coordinate3D: CompoundDimension {
/// ``GeoCoordinates/CompoundDimension``
extension WGS84Coordinate3D {

public typealias LowerDimension = Coordinate2D
public typealias LowerDimension = WGS84Coordinate2D

public var lowerDimension: Self.LowerDimension {
LowerDimension(latitude: latitude, longitude: longitude)
}

}

extension Coordinate3D: Coordinates {
/// ``GeoCoordinates/Coordinates``
extension Coordinate3D {

public init<N: BinaryFloatingPoint>(repeating number: N) {
self.init(x: Self.X(number), y: Self.Y(number), z: Self.Z(number))
Expand Down
2 changes: 1 addition & 1 deletion Sources/GeoCoordinates/Display/Coordinate+Display.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension AngularCoordinate {

}

extension Coordinate2D: GeographicNotation {
extension WGS84Coordinate2D: GeographicNotation {

public func ddNotation(maxDigits: UInt8) -> String {
let lat = self.latitude.ddNotation(maxDigits: maxDigits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#if canImport(CoreLocation)
import CoreLocation

extension Coordinate2D {
extension WGS84Coordinate2D {

public var clLocation: CLLocation {
CLLocation(latitude: latitude.decimalDegrees, longitude: longitude.decimalDegrees)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#if canImport(MapKit)
import MapKit

extension Coordinate2D {
extension WGS84Coordinate2D {

public var mkMapPoint: MKMapPoint {
MKMapPoint(clLocationCoordinate2D)
Expand Down
2 changes: 1 addition & 1 deletion Sources/GeoCoordinates/Protocols/Coordinate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public protocol Coordinate: BinaryFloatingPoint, CustomStringConvertible, CustomDebugStringConvertible {
public protocol Coordinate: BinaryFloatingPoint, CustomStringConvertible, CustomDebugStringConvertible, Zeroable {

var value: Double { get set }

Expand Down
Loading

0 comments on commit 9d86ba4

Please sign in to comment.