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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ let utmCoordinate = UTMCoordinate(northing: 7034313, easting: 569612, zone: 32,
let datum = UTMDatum(equitorialRadius: 6378137, polarRadius: 6356752.3142)
let coordinate = utmCoordinate.coordinate(datum: datum)
```

### UTM Zone

It is possible to specify the UTM zone for conversion, e.g. if you want consistent UTM coordinates at a border of two zones.

```swift
import CoreLocation
import UTMConversion

let location = CLLocation(latitude: 51.357402, longitude: 12.002263)
let utmCoordinateInZone33 = utmCoordinate.coordinate() // Coordinate is in 33, but at the border to 32.
let utmCoordinateInZone32 = utmCoordinate.coordinate(zone: 32)
```
14 changes: 13 additions & 1 deletion Sources/UTMConversion/CLLocation+UTMCoordinate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@ public extension CLLocation {
let zone = coordinate.zone
return TMCoordinate(coordinate: coordinate, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: coordinate.hemisphere)
}


/**
Calculates the UTM coordinate of the receiver

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

*/
func utmCoordinate(zone: UTMGridZone, datum: UTMDatum = UTMDatum.wgs84) -> UTMCoordinate {
let coordinate = self.coordinate
return TMCoordinate(coordinate: coordinate, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: coordinate.hemisphere)
}

}
14 changes: 12 additions & 2 deletions Sources/UTMConversion/CLLocationCoordinate2D+UTMCoordinate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ public extension CLLocationCoordinate2D {
let zone = self.zone
return TMCoordinate(coordinate: self, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: hemisphere)
}



/**
Calculates the UTM coordinate of the receiver

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

*/
func utmCoordinate(zone: UTMGridZone, datum: UTMDatum = UTMDatum.wgs84) -> UTMCoordinate {
TMCoordinate(coordinate: self, centralMeridian: zone.centralMeridian, datum: datum).utmCoordinate(zone: zone, hemisphere: hemisphere)
}

/**
The UTM grid zone
*/
Expand Down
24 changes: 24 additions & 0 deletions Tests/UTMConversionTests/UTMConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ class UTMConversionTests: XCTestCase {
let oslo = osloUTM.location(datum: UTMDatum(equitorialRadius: 6378137, polarRadius: 6356752.3142))
XCTAssertEqual(oslo.coordinate.latitude, 59.912814611065265)
XCTAssertEqual(oslo.coordinate.longitude, 10.760192985178369)

}

func testCustomZone() {
// Coordinate located in UTM zone 33
let lat = 51.357402
let lon = 12.002263

// echo "12.002263 51.357402" | proj -f "%.5f" +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs
let northing32 = 5693849.66049
let easting32 = 709023.99157
let zone: UTMGridZone = 32

let neumarkt2D = CLLocationCoordinate2D(latitude: lat, longitude: lon).utmCoordinate(zone: zone)
XCTAssertEqual(neumarkt2D.northing, northing32, accuracy: 0.0001)
XCTAssertEqual(neumarkt2D.easting, easting32, accuracy: 0.0001)
XCTAssertEqual(neumarkt2D.zone, zone)
XCTAssertEqual(neumarkt2D.hemisphere, .northern)

let neumarkt = CLLocation(latitude: lat, longitude: lon).utmCoordinate(zone: zone)
XCTAssertEqual(neumarkt.northing, northing32, accuracy: 0.0001)
XCTAssertEqual(neumarkt.easting, easting32, accuracy: 0.0001)
XCTAssertEqual(neumarkt.zone, zone)
XCTAssertEqual(neumarkt.hemisphere, .northern)
}
}

Expand Down