diff --git a/Sources/GPXKit/DistanceCalculation.swift b/Sources/GPXKit/DistanceCalculation.swift index c2bee05..6b4548c 100644 --- a/Sources/GPXKit/DistanceCalculation.swift +++ b/Sources/GPXKit/DistanceCalculation.swift @@ -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) + } } diff --git a/Tests/GPXKitTests/GeoCoordinateExtensionsTests.swift b/Tests/GPXKitTests/GeoCoordinateExtensionsTests.swift new file mode 100644 index 0000000..04557eb --- /dev/null +++ b/Tests/GPXKitTests/GeoCoordinateExtensionsTests.swift @@ -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)" + ) + } + } +}