Skip to content

Commit

Permalink
Bugfix: Holes in polygons where not subtracted from the outer ring area
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed Jul 29, 2024
1 parent 8303092 commit 2a6717d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/GISTools/Algorithms/Area.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Polygon {
var area: Double = abs(outerRing.area)

if let innerRings = innerRings {
area += innerRings.reduce(0.0, { $0 + abs($1.area) })
area -= innerRings.reduce(0.0, { $0 + abs($1.area) })
}

return area
Expand Down
32 changes: 32 additions & 0 deletions Tests/GISToolsTests/Algorithms/AreaTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@testable import GISTools
import XCTest

final class AreaTests: XCTestCase {

func testArea() throws {
let polygon1 = try XCTUnwrap(Polygon([[
Coordinate3D(x: 0.0, y: 0.0),
Coordinate3D(x: 100.0, y: 0.0),
Coordinate3D(x: 100.0, y: 100.0),
Coordinate3D(x: 0.0, y: 100.0),
Coordinate3D(x: 0.0, y: 0.0)
]]))
let polygon2 = try XCTUnwrap(Polygon([[
Coordinate3D(x: 0.0, y: 0.0),
Coordinate3D(x: 100.0, y: 0.0),
Coordinate3D(x: 100.0, y: 100.0),
Coordinate3D(x: 0.0, y: 100.0),
Coordinate3D(x: 0.0, y: 0.0)
], [
Coordinate3D(x: 25.0, y: 25.0),
Coordinate3D(x: 75.0, y: 25.0),
Coordinate3D(x: 75.0, y: 75.0),
Coordinate3D(x: 25.0, y: 75.0),
Coordinate3D(x: 25.0, y: 25.0)
]]))

XCTAssertEqual(polygon1.area, 10000.0, accuracy: 0.1)
XCTAssertEqual(polygon2.area, 7500.0, accuracy: 0.1)
}

}

0 comments on commit 2a6717d

Please sign in to comment.