Skip to content

Commit

Permalink
Don't crash when parsing an empty polygon (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighthawk authored Nov 23, 2023
1 parent d7cef4c commit 5e2c59a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/GeoJSONKit/GeoJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public struct GeoJSON: Hashable {
private let precomputedHash: Int

public init(_ rings: [[Position]]) {
self.exterior = LinearRing(positions: rings.first!)
self.exterior = LinearRing(positions: rings.first ?? [])
self.interiors = rings
.dropFirst()
.map(LinearRing.init)
Expand Down
19 changes: 19 additions & 0 deletions Tests/GeoJSONKitTests/GeoJSONParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ final class GeoJSONParserTest: XCTestCase {
XCTAssertNil(positions2[1].altitude)
}

func testEmptyPolygon() throws {
let parsed = try GeoJSON(geoJSONString: """
{
"type": "Feature",
"properties": {
"stroke": "#F00",
"fill": "#F00",
"stroke-width": 6,
"fill-opacity": 0.1
},
"geometry": {
"type": "Polygon",
"coordinates": []
}
}
""")
XCTAssertNotNil(parsed)
}

func testMultiPolygon() throws {
let data = try XCTestCase.loadData(filename: "multipolygon")
let parsed = try GeoJSON(data: data)
Expand Down

0 comments on commit 5e2c59a

Please sign in to comment.