Skip to content

Commit

Permalink
Adds radiusInMeters(latitudeDelta:) to GeoCoordinate
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Mueller <mmlr@gmx.net>
  • Loading branch information
mmllr committed Jan 23, 2021
1 parent 38221b6 commit 0f893ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/GPXKit/DistanceCalculation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ extension GeoCoordinate {
let d = R * c
return d
}

// one degree of latitude is always approximately 111 kilometers (69 miles)
func radiusInMeters(latitudeDelta: Double) -> Double {
let topCentralLat: Double = latitude - latitudeDelta / 2
let topCentralLocation = Coordinate(latitude: topCentralLat, longitude: longitude, elevation: 0)
return distance(to: topCentralLocation)
}
}
20 changes: 20 additions & 0 deletions Tests/GPXKitTests/GeoCoordinateExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation
@testable import GPXKit
import XCTest

final class GeoCoordinateExtensionsTests: XCTestCase {
func testRadiusForDelta() {
let location = Coordinate(latitude: 51.323331, longitude: 12.368279, elevation: 110)

for degree in stride(from: 1.0, to: 180.0, by: 1) {
let expected = degree * 111.045 / 2.0 * 1000
// one degree of latitude is always approximately 111 kilometers (69 miles)
XCTAssertEqual(
expected,
location.radiusInMeters(latitudeDelta: degree),
accuracy: 139 * degree,
"Radius \(location.radiusInMeters(latitudeDelta: degree)) for \(degree) not in expected range \(expected)"
)
}
}
}

0 comments on commit 0f893ef

Please sign in to comment.