Skip to content

Commit f2057a7

Browse files
committed
Close rings if necessary
1 parent abc1f4b commit f2057a7

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed
File renamed without changes.

Sources/GISTools/GeoJson/Polygon.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct Polygon:
3434

3535
/// The receiver's outer ring.
3636
public var outerRing: Ring? {
37-
guard !coordinates.isEmpty else { return nil }
37+
guard coordinates.isNotEmpty else { return nil }
3838
return Ring(coordinates[0])
3939
}
4040

@@ -55,7 +55,7 @@ public struct Polygon:
5555

5656
/// Try to initialize a Polygon with some coordinates.
5757
public init?(_ coordinates: [[Coordinate3D]], calculateBoundingBox: Bool = false) {
58-
guard !coordinates.isEmpty,
58+
guard coordinates.isNotEmpty,
5959
coordinates[0].count >= 3
6060
else { return nil }
6161

Sources/GISTools/GeoJson/Ring.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ public struct Ring: Sendable {
2929

3030
/// Try to initialize a Ring with some coordinates.
3131
public init?(_ coordinates: [Coordinate3D]) {
32-
// TODO: Close the ring, if necessary
32+
// Close the ring, if necessary
33+
var coordinates = coordinates
34+
if coordinates.count >= 3,
35+
coordinates.first != coordinates.last
36+
{
37+
coordinates.append(coordinates[0])
38+
}
39+
3340
guard coordinates.count >= 4 else { return nil }
3441

3542
self.init(unchecked: coordinates)

0 commit comments

Comments
 (0)