Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions UTMConversion/CLLocation+UTMCoordinate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public extension CLLocation {
/**
Calculates the UTM coordinate of the receiver

- Parameter datum: The datum to use, defaults to WGS84 which should be fine for most applications
- Parameter datum: The datum to use, defaults to WGS84 which should be fine for most applications.

Throws a ``UTMConversionError.invalidCoordinate`` if the locations coordinate is invalid.
*/
func utmCoordinate(datum: UTMDatum = UTMDatum.wgs84) -> UTMCoordinate {
func utmCoordinate(datum: UTMDatum = UTMDatum.wgs84) throws -> UTMCoordinate {
let coordinate = self.coordinate
guard CLLocationCoordinate2DIsValid(coordinate) else { throw UTMConversionError.invalidCoordinate }
let zone = coordinate.zone
return TMCoordinate(coordinate: coordinate, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: coordinate.hemisphere)
}
Expand Down
6 changes: 4 additions & 2 deletions UTMConversion/CLLocationCoordinate2D+UTMCoordinate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ public extension CLLocationCoordinate2D {
/**
Calculates the UTM coordinate of the receiver

- Parameter datum: The datum to use, defaults to WGS84 which should be fine for most applications
- Parameter datum: The datum to use, defaults to WGS84 which should be fine for most applications.

Throws a ``UTMConversionError.invalidCoordinate`` if the coordinate is invalid.
*/
func utmCoordinate(datum: UTMDatum = UTMDatum.wgs84) -> UTMCoordinate {
func utmCoordinate(datum: UTMDatum = UTMDatum.wgs84) throws -> UTMCoordinate {
guard CLLocationCoordinate2DIsValid(self) else { throw UTMConversionError.invalidCoordinate }
let zone = self.zone
return TMCoordinate(coordinate: self, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: hemisphere)
}
Expand Down
3 changes: 3 additions & 0 deletions UTMConversion/UTMConversionError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum UTMConversionError: Error {
case invalidCoordinate
}
48 changes: 38 additions & 10 deletions UTMConversionTests/UTMConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ import XCTest

class UTMConversionTests: XCTestCase {

func testCLLocationCoordinate2D_utmCoordinate() {
let osloUTM = oslo.utmCoordinate()
func testCLLocationCoordinate2D_utmCoordinate() throws {
let osloUTM = try oslo.utmCoordinate()
XCTAssertEqual(osloUTM.northing, 6643010.0, accuracy: 0.00001);
XCTAssertEqual(osloUTM.easting, 598430.0, accuracy: 0.00001);
XCTAssertEqual(osloUTM.zone, 32)
XCTAssertEqual(osloUTM.hemisphere, .northern)

let trondheimUTM = trondheim.utmCoordinate()
let trondheimUTM = try trondheim.utmCoordinate()
XCTAssertEqual(trondheimUTM.northing, 7034313, accuracy: 0.00001)
XCTAssertEqual(trondheimUTM.easting, 569612, accuracy: 0.00001)
XCTAssertEqual(trondheimUTM.zone, 32)
XCTAssertEqual(trondheimUTM.hemisphere, .northern)

let johannesburgUTM = johannesburg.utmCoordinate()
let johannesburgUTM = try johannesburg.utmCoordinate()
XCTAssertEqual(johannesburgUTM.northing, 7100115, accuracy: 0.00001)
XCTAssertEqual(johannesburgUTM.easting, 603914, accuracy: 0.00001)
XCTAssertEqual(johannesburgUTM.zone, 35)
XCTAssertEqual(johannesburgUTM.hemisphere, .southern)

let buninyongUTM = buninyong.utmCoordinate()
let buninyongUTM = try buninyong.utmCoordinate()
XCTAssertEqual(buninyongUTM.northing, 5828674.33994, accuracy: 0.00001)
XCTAssertEqual(buninyongUTM.easting, 758173.79835, accuracy: 0.00001)
XCTAssertEqual(buninyongUTM.zone, 54)
XCTAssertEqual(buninyongUTM.hemisphere, .southern)
}

func testCLLocation_utmCoordinate() {
let osloUTM = osloLocation.utmCoordinate()
func testCLLocation_utmCoordinate() throws {
let osloUTM = try osloLocation.utmCoordinate()
XCTAssertEqual(osloUTM.northing, 6643010.0, accuracy: 0.00001);
XCTAssertEqual(osloUTM.easting, 598430.0, accuracy: 0.00001);
XCTAssertEqual(osloUTM.zone, 32)
XCTAssertEqual(osloUTM.hemisphere, .northern)

let trondheimUTM = trondheimLocation.utmCoordinate()
let trondheimUTM = try trondheimLocation.utmCoordinate()
XCTAssertEqual(trondheimUTM.northing, 7034313, accuracy: 0.00001)
XCTAssertEqual(trondheimUTM.easting, 569612, accuracy: 0.00001)
XCTAssertEqual(trondheimUTM.zone, 32)
XCTAssertEqual(trondheimUTM.hemisphere, .northern)

let johannesburgUTM = johannesburgLocation.utmCoordinate()
let johannesburgUTM = try johannesburgLocation.utmCoordinate()
XCTAssertEqual(johannesburgUTM.northing, 7100115, accuracy: 0.00001)
XCTAssertEqual(johannesburgUTM.easting, 603914, accuracy: 0.00001)
XCTAssertEqual(johannesburgUTM.zone, 35)
XCTAssertEqual(johannesburgUTM.hemisphere, .southern)

let buninyongUTM = buninyongLocation.utmCoordinate()
let buninyongUTM = try buninyongLocation.utmCoordinate()
XCTAssertEqual(buninyongUTM.northing, 5828674.33994, accuracy: 0.00001)
XCTAssertEqual(buninyongUTM.easting, 758173.79835, accuracy: 0.00001)
XCTAssertEqual(buninyongUTM.zone, 54)
Expand Down Expand Up @@ -98,6 +98,34 @@ class UTMConversionTests: XCTestCase {
XCTAssertEqual(oslo.coordinate.latitude, 59.912814611065265)
XCTAssertEqual(oslo.coordinate.longitude, 10.760192985178369)
}

func testInvalidCoordinateLatitude() {
let coordinate = CLLocationCoordinate2D(latitude: 90.1, longitude: -180)
XCTAssertThrowsError(try coordinate.utmCoordinate()) { error in
XCTAssertEqual(error as? UTMConversionError, UTMConversionError.invalidCoordinate)
}
}

func testInvalidCoordinateLongitude() {
let coordinate = CLLocationCoordinate2D(latitude: 90, longitude: -180.1)
XCTAssertThrowsError(try coordinate.utmCoordinate()) { error in
XCTAssertEqual(error as? UTMConversionError, UTMConversionError.invalidCoordinate)
}
}

func testInvalidLocationLatitude() {
let location = CLLocation(latitude: -90.1, longitude: 180)
XCTAssertThrowsError(try location.utmCoordinate()) { error in
XCTAssertEqual(error as? UTMConversionError, UTMConversionError.invalidCoordinate)
}
}

func testInvalidLocationLongitude() {
let location = CLLocation(latitude: -90, longitude: 180.1)
XCTAssertThrowsError(try location.utmCoordinate()) { error in
XCTAssertEqual(error as? UTMConversionError, UTMConversionError.invalidCoordinate)
}
}
}


Expand Down