|
| 1 | +# Swift implementation of the geodesic routines in GeographicLib |
| 2 | + |
| 3 | +This is a Swift library to solve geodesic problems on an ellipsoid model of |
| 4 | +the earth. |
| 5 | + |
| 6 | +This is a Swift wrapper around the C implementation of the geodesic routines |
| 7 | +from [GeographicLib](https://geographiclib.sourceforge.io). |
| 8 | + |
| 9 | +Licensed under the MIT/X11 License; see |
| 10 | +[LICENSE.txt](https://geographiclib.sourceforge.io/LICENSE.txt). |
| 11 | + |
| 12 | +The algorithms are documented in |
| 13 | + |
| 14 | +* C. F. F. Karney, |
| 15 | + [Algorithms for geodesics](https://doi.org/10.1007/s00190-012-0578-z), |
| 16 | + J. Geodesy **87**(1), 43–55 (2013); |
| 17 | + [Addenda](https://geographiclib.sourceforge.io/geod-addenda.html). |
| 18 | + |
| 19 | +## Other links: |
| 20 | + |
| 21 | +* Library documentation: (coming soon) |
| 22 | +* GeographicLib: https://geographiclib.sourceforge.io |
| 23 | +* C implementation: https://github.com/geographiclib/geographiclib-c |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +### Swift Package Manager |
| 28 | + |
| 29 | +Add the following to your `Package.swift` file: |
| 30 | + |
| 31 | +```swift |
| 32 | +dependencies: [ |
| 33 | + .package(url: "https://github.com/scottrhoyt/geographiclib-swift.git", from: "1.0.0") |
| 34 | +] |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Basic Usage |
| 40 | + |
| 41 | +```swift |
| 42 | +import GeographicLib |
| 43 | + |
| 44 | +// Create a geodesic calculator with WGS-84 ellipsoid (default) |
| 45 | +let geodesic = Geodesic() |
| 46 | + |
| 47 | +// Solve the direct problem: given a starting point, azimuth, and distance |
| 48 | +let direct = geodesic.direct( |
| 49 | + latitude: 40.64, // JFK Airport |
| 50 | + longitude: -73.78, |
| 51 | + azimuth: 45.0, // northeast |
| 52 | + distance: 10_000_000 // 10,000 km |
| 53 | +) |
| 54 | +print("Destination: \(direct.latitude)°, \(direct.longitude)°") |
| 55 | + |
| 56 | +// Solve the inverse problem: given two points, find distance and azimuths |
| 57 | +let inverse = geodesic.inverse( |
| 58 | + latitude1: 40.64, // JFK Airport |
| 59 | + longitude1: -73.78, |
| 60 | + latitude2: 1.36, // Singapore Changi Airport |
| 61 | + longitude2: 103.99 |
| 62 | +) |
| 63 | +print("Distance: \(inverse.distance) meters") |
| 64 | +print("Initial azimuth: \(inverse.azimuth1)°") |
| 65 | +``` |
| 66 | + |
| 67 | +### Using Different Ellipsoids |
| 68 | + |
| 69 | +```swift |
| 70 | +// Use GRS-80 ellipsoid |
| 71 | +let grs80 = Geodesic(.grs80) |
| 72 | + |
| 73 | +// Use a custom ellipsoid |
| 74 | +let customEllipsoid = Ellipsoid(equatorialRadius: 6378000.0, flattening: 1.0/300.0) |
| 75 | +let customGeodesic = Geodesic(customEllipsoid) |
| 76 | + |
| 77 | +// Use a sphere for simplified calculations |
| 78 | +let sphere = Geodesic(.sphere) |
| 79 | +``` |
| 80 | + |
| 81 | +### Geodesic Lines |
| 82 | + |
| 83 | +For efficient calculations of multiple points along a geodesic: |
| 84 | + |
| 85 | +```swift |
| 86 | +// Create a geodesic line |
| 87 | +let line = geodesic.inverseLine( |
| 88 | + latitude1: 40.64, longitude1: -73.78, // JFK |
| 89 | + latitude2: 1.36, longitude2: 103.99 // Singapore |
| 90 | +) |
| 91 | + |
| 92 | +// Calculate waypoints |
| 93 | +for i in 0...10 { |
| 94 | + let fraction = Double(i) / 10.0 |
| 95 | + let position = line.position(distance: line.distance * fraction) |
| 96 | + print("Waypoint \(i): \(position.latitude)°, \(position.longitude)°") |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Polygon Areas |
| 101 | + |
| 102 | +Calculate areas and perimeters of geodesic polygons: |
| 103 | + |
| 104 | +```swift |
| 105 | +// Simple polygon area calculation |
| 106 | +let antarctica = [ |
| 107 | + (-72.9, -74), (-71.9, -102), (-74.9, -102), (-74.3, -131), |
| 108 | + (-77.5, -163), (-77.4, 163), (-71.7, 172), (-65.9, 140), |
| 109 | + (-65.7, 113), (-66.6, 88), (-66.9, 59), (-69.8, 25), |
| 110 | + (-70.0, -4), (-71.0, -14), (-77.3, -33), (-77.9, -46), (-74.7, -61) |
| 111 | +] |
| 112 | + |
| 113 | +let (area, perimeter) = geodesic.polygonArea( |
| 114 | + latitudes: antarctica.map { $0.0 }, |
| 115 | + longitudes: antarctica.map { $0.1 } |
| 116 | +) |
| 117 | +print("Antarctica area: \(area) m²") |
| 118 | +print("Antarctica perimeter: \(perimeter) m") |
| 119 | + |
| 120 | +// Using the Polygon type for more control |
| 121 | +var polygon = Polygon() |
| 122 | +polygon.addPoint(latitude: 0, longitude: 0) |
| 123 | +polygon.addPoint(latitude: 0, longitude: 90) |
| 124 | +polygon.addPoint(latitude: 90, longitude: 0) |
| 125 | + |
| 126 | +let result = polygon.compute() |
| 127 | +print("Triangle area: \(result.area!) m²") |
| 128 | +``` |
| 129 | + |
| 130 | +## Features |
| 131 | + |
| 132 | +- **Direct geodesic problem**: Given a starting point, azimuth, and distance, find the destination point |
| 133 | +- **Inverse geodesic problem**: Given two points, find the distance and azimuths between them |
| 134 | +- **Geodesic lines**: Efficiently compute multiple points along a geodesic |
| 135 | +- **Polygon areas**: Calculate areas and perimeters of geodesic polygons |
| 136 | +- **Multiple ellipsoids**: Support for WGS-84, GRS-80, custom ellipsoids, and spheres |
| 137 | +- **High precision**: Accurate to round-off for distances up to 180° |
| 138 | +- **Thread-safe**: All types are immutable and `Sendable` |
| 139 | + |
| 140 | +## Requirements |
| 141 | + |
| 142 | +- Swift 5.5 or later |
| 143 | +- Platforms: macOS, iOS, tvOS, watchOS, Linux, Windows |
0 commit comments