From 2a6717d852b1de573a340219fd2d350bf63e77ad Mon Sep 17 00:00:00 2001 From: Thomas Rasch Date: Mon, 29 Jul 2024 16:40:59 +0200 Subject: [PATCH] Bugfix: Holes in polygons where not subtracted from the outer ring area --- Sources/GISTools/Algorithms/Area.swift | 2 +- .../GISToolsTests/Algorithms/AreaTests.swift | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 Tests/GISToolsTests/Algorithms/AreaTests.swift diff --git a/Sources/GISTools/Algorithms/Area.swift b/Sources/GISTools/Algorithms/Area.swift index a35d59a..9b7b20e 100644 --- a/Sources/GISTools/Algorithms/Area.swift +++ b/Sources/GISTools/Algorithms/Area.swift @@ -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 diff --git a/Tests/GISToolsTests/Algorithms/AreaTests.swift b/Tests/GISToolsTests/Algorithms/AreaTests.swift new file mode 100644 index 0000000..39c40e7 --- /dev/null +++ b/Tests/GISToolsTests/Algorithms/AreaTests.swift @@ -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) + } + +}