Skip to content

Commit 362a3f5

Browse files
committed
Update polygon area function to take a tuple array for lat/lon as opposed to 2 separate arrays.
1 parent 6bd0555 commit 362a3f5

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
## `main`
4+
* Update polygon area function to take a tuple array for lat/lon as opposed to 2 separate arrays.
5+
6+
## 0.1.0
7+
* Initial releas

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,8 @@ let antarctica = [
136136
(-70.0, -4), (-71.0, -14), (-77.3, -33), (-77.9, -46), (-74.7, -61)
137137
]
138138

139-
let (area, perimeter) = geodesic.polygonArea(
140-
latitudes: antarctica.map { $0.0 },
141-
longitudes: antarctica.map { $0.1 }
142-
)
139+
let (area, perimeter) = geodesic.polygonArea(coordinates: antarctica)
140+
143141
print("Antarctica area: \(area)")
144142
print("Antarctica perimeter: \(perimeter) m")
145143

Sources/GeographicLib/GeodesicPolygon.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,18 @@ public extension Geodesic {
193193
/// Compute the area and perimeter of a polygon defined by arrays of coordinates.
194194
///
195195
/// - Parameters:
196-
/// - latitudes: Array of latitudes in degrees [-90, 90]
197-
/// - longitudes: Array of longitudes in degrees [-180, 180]
196+
/// - coordinates: Array of (latitudes, logitude) pairs in degrees ([-90, 90], [-180, 180])
198197
/// - Returns: A tuple containing the area in square meters and perimeter in meters
199198
/// - Precondition: latitudes and longitudes must have the same count
200-
func polygonArea(latitudes: [Double], longitudes: [Double]) -> (area: Double, perimeter: Double) {
201-
precondition(latitudes.count == longitudes.count, "Latitude and longitude arrays must have the same count")
202-
199+
func polygonArea(coordinates: [(latitude: Double, longitude: Double)]) -> (area: Double, perimeter: Double) {
203200
var area: Double = 0
204201
var perimeter: Double = 0
205202

206203
withUnsafePointer(to: geod) { geodPtr in
207-
latitudes.withUnsafeBufferPointer { latPtr in
208-
longitudes.withUnsafeBufferPointer { lonPtr in
209-
var mutableLats = Array(latPtr)
210-
var mutableLons = Array(lonPtr)
211-
212-
geod_polygonarea(geodPtr, &mutableLats, &mutableLons, Int32(latitudes.count), &area, &perimeter)
213-
}
214-
}
204+
var latitudes = coordinates.map { $0.latitude }
205+
var longitudes = coordinates.map { $0.longitude }
206+
207+
geod_polygonarea(geodPtr, &latitudes, &longitudes, Int32(coordinates.count), &area, &perimeter)
215208
}
216209

217210
return (area: area, perimeter: perimeter)

Tests/GeographicLibTests/GeodesicPolygonTests.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,28 @@ struct GeodesicPolygonTests {
108108

109109
@Test("Antarctica area from coordinates")
110110
func testAntarcticaArea() {
111-
let latitudes: [Double] = [-72.9, -71.9, -74.9, -74.3, -77.5, -77.4, -71.7, -65.9, -65.7,
112-
-66.6, -66.9, -69.8, -70.0, -71.0, -77.3, -77.9, -74.7]
113-
let longitudes: [Double] = [-74, -102, -102, -131, -163, 163, 172, 140, 113,
114-
88, 59, 25, -4, -14, -33, -46, -61]
111+
let antarctica = [
112+
(-72.9, -74.0),
113+
(-71.9, -102.0),
114+
(-74.9, -102.0),
115+
(-74.3, -131.0),
116+
(-77.5, -163.0),
117+
(-77.4, 163.0),
118+
(-71.7, 172.0),
119+
(-65.9, 140.0),
120+
(-65.7, 113.0),
121+
(-66.6, 88.0),
122+
(-66.9, 59.0),
123+
(-69.8, 25.0),
124+
(-70.0, -4.0),
125+
(-71.0, -14.0),
126+
(-77.3, -33.0),
127+
(-77.9, -46.0),
128+
(-74.7, -61.0)
129+
]
115130

116131
let geodesic = Geodesic()
117-
let (area, perimeter) = geodesic.polygonArea(latitudes: latitudes, longitudes: longitudes)
132+
let (area, perimeter) = geodesic.polygonArea(coordinates: antarctica)
118133

119134
// Expected values from GeographicLib implementation
120135
let expectedArea = 13376856682207.4 // square meters

0 commit comments

Comments
 (0)