Add geo utils
This commit is contained in:
parent
d6a4b0d1f1
commit
6da30adc0d
|
@ -0,0 +1,28 @@
|
||||||
|
//
|
||||||
|
// CLLocationCoordinate2D+Valid.swift
|
||||||
|
// SwiftKit
|
||||||
|
//
|
||||||
|
// Created by Daniel Saidi on 2015-09-18.
|
||||||
|
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import CoreLocation
|
||||||
|
|
||||||
|
public extension CLLocationCoordinate2D {
|
||||||
|
|
||||||
|
/**
|
||||||
|
Check if the coordinate is valid. This is a best effort
|
||||||
|
that checks so that not both the latitude and longitude
|
||||||
|
are not or any extremes.
|
||||||
|
*/
|
||||||
|
var isValid: Bool {
|
||||||
|
isValid(latitude) && isValid(longitude)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension CLLocationCoordinate2D {
|
||||||
|
|
||||||
|
func isValid(_ degrees: CLLocationDegrees) -> Bool {
|
||||||
|
degrees != 0 && degrees != 180 && degrees != -180
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
//
|
||||||
|
// WorldCoordinate.swift
|
||||||
|
// SwiftKit
|
||||||
|
//
|
||||||
|
// Created by Daniel Saidi on 2015-10-04.
|
||||||
|
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import CoreLocation
|
||||||
|
|
||||||
|
/**
|
||||||
|
This struct can be used to represent world coordinates, but
|
||||||
|
without bloating `CLLocationCoordinate2D` with static props.
|
||||||
|
|
||||||
|
The reason to why this is a struct and not an enum, is that
|
||||||
|
it simplifies extending it with new coordinates in any apps
|
||||||
|
that use custom coordinates.
|
||||||
|
*/
|
||||||
|
public struct WorldCoordinate {
|
||||||
|
|
||||||
|
public let coordinate: CLLocationCoordinate2D
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension WorldCoordinate {
|
||||||
|
|
||||||
|
static var manhattan: WorldCoordinate { .init(coordinate: CLLocationCoordinate2D(latitude: 40.7590615, longitude: -73.969231)) }
|
||||||
|
static var newYork: WorldCoordinate { .init(coordinate: CLLocationCoordinate2D(latitude: 40.7033127, longitude: -73.979681)) }
|
||||||
|
static var sanFransisco: WorldCoordinate { .init(coordinate: CLLocationCoordinate2D(latitude: 37.7796828, longitude: -122.4000062)) }
|
||||||
|
static var tokyo: WorldCoordinate { .init(coordinate: CLLocationCoordinate2D(latitude: 35.673, longitude: 139.710)) }
|
||||||
|
}
|
|
@ -36,8 +36,8 @@ class DateComparingTests: QuickSpec {
|
||||||
let date1 = Date(timeIntervalSince1970: 0)
|
let date1 = Date(timeIntervalSince1970: 0)
|
||||||
let date2 = Date(timeIntervalSince1970: 1)
|
let date2 = Date(timeIntervalSince1970: 1)
|
||||||
let date3 = Date(timeIntervalSince1970: 0)
|
let date3 = Date(timeIntervalSince1970: 0)
|
||||||
expect(date1.isSameAs(date2)).to(beFalse())
|
expect(date1.isSame(as: date2)).to(beFalse())
|
||||||
expect(date1.isSameAs(date3)).to(beTrue())
|
expect(date1.isSame(as: date3)).to(beTrue())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
//
|
||||||
|
// CLLocationCoordinate2D+ValidTests.swift
|
||||||
|
// SwiftKitTests
|
||||||
|
//
|
||||||
|
// Created by Daniel Saidi on 2018-10-19.
|
||||||
|
// Copyright © 2020 Daniel Saidi. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Quick
|
||||||
|
import Nimble
|
||||||
|
import SwiftKit
|
||||||
|
import CoreLocation
|
||||||
|
|
||||||
|
class CLLocationCoordinate2D_ValidTests: QuickSpec {
|
||||||
|
|
||||||
|
override func spec() {
|
||||||
|
|
||||||
|
describe("Coordinate validation") {
|
||||||
|
|
||||||
|
func result(for lat: CLLocationDegrees, _ long: CLLocationDegrees) -> Bool {
|
||||||
|
CLLocationCoordinate2D(latitude: lat, longitude: long).isValid
|
||||||
|
}
|
||||||
|
|
||||||
|
it("is invalid if latitude is invalid") {
|
||||||
|
expect(result(for: 0, 120)).to(beFalse())
|
||||||
|
expect(result(for: 180, 120)).to(beFalse())
|
||||||
|
expect(result(for: -180, 120)).to(beFalse())
|
||||||
|
}
|
||||||
|
|
||||||
|
it("is invalid if longitude is invalid") {
|
||||||
|
expect(result(for: 120, 0)).to(beFalse())
|
||||||
|
expect(result(for: 120, 180)).to(beFalse())
|
||||||
|
expect(result(for: 120, -180)).to(beFalse())
|
||||||
|
}
|
||||||
|
|
||||||
|
it("is valid if both components are valid") {
|
||||||
|
expect(result(for: 120, 120)).to(beTrue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue