Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Sources/GISTools/Algorithms/BooleanPointInPolygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,16 @@ extension GeometryCollection {
let snappedCoordinate = gridSize.map { coordinate.snappedToGrid(tolerance: $0) } ?? coordinate

return snappedSelf.geometries.contains { (geometry) -> Bool in
guard let polygonGeometry = geometry as? PolygonGeometry else { return false }
switch geometry {
case let polygonGeometry as PolygonGeometry:
return polygonGeometry.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)

return polygonGeometry.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)
case let geometryCollection as GeometryCollection:
return geometryCollection.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)

default:
return false
}
}
}

Expand All @@ -287,9 +294,16 @@ extension Feature {
let snappedSelf = gridSize.map { self.snappedToGrid(tolerance: $0) } ?? self
let snappedCoordinate = gridSize.map { coordinate.snappedToGrid(tolerance: $0) } ?? coordinate

guard let polygonGeometry = snappedSelf.geometry as? PolygonGeometry else { return false }
switch snappedSelf.geometry {
case let polygonGeometry as PolygonGeometry:
return polygonGeometry.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)

case let geometryCollection as GeometryCollection:
return geometryCollection.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)

return polygonGeometry.contains(snappedCoordinate, ignoringBoundary: ignoringBoundary, gridSize: nil)
default:
return false
}
}

}
Expand Down
110 changes: 110 additions & 0 deletions Tests/GISToolsTests/Algorithms/BooleanPointInPolygonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,65 @@ struct BooleanPointInPolygonTests {
#expect(fc.contains(Coordinate3D(latitude: 20.0, longitude: 5.0)) == false)
}

// MARK: - GeometryCollection

// Validates that `Feature.contains` handles features whose geometry is a `GeometryCollection`.
@Test
func featureContainsGeometryCollection() async throws {
let polygon = try #require(Polygon([[
Coordinate3D(latitude: 0.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 0.0),
]]))
let feature = Feature(GeometryCollection([polygon]))

#expect(feature.contains(Coordinate3D(latitude: 5.0, longitude: 5.0)))
#expect(feature.contains(Coordinate3D(latitude: 20.0, longitude: 5.0)) == false)
}

// Validates that `FeatureCollection.contains` finds features whose geometry is a `GeometryCollection`.
@Test
func featureCollectionContainsGeometryCollection() async throws {
let polygon = try #require(Polygon([[
Coordinate3D(latitude: 0.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 0.0),
]]))
let fc = FeatureCollection([Feature(GeometryCollection([polygon]))])

#expect(fc.contains(Coordinate3D(latitude: 5.0, longitude: 5.0)))
#expect(fc.contains(Coordinate3D(latitude: 20.0, longitude: 5.0)) == false)
}

// Validates that nested `GeometryCollection`s resolve to their polygon members.
@Test
func geometryCollectionContainsNestedCollection() async throws {
let polygon = try #require(Polygon([[
Coordinate3D(latitude: 0.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 10.0),
Coordinate3D(latitude: 10.0, longitude: 0.0),
Coordinate3D(latitude: 0.0, longitude: 0.0),
]]))
let outer = GeometryCollection([GeometryCollection([polygon])])

#expect(outer.contains(Coordinate3D(latitude: 5.0, longitude: 5.0)))
#expect(outer.contains(Coordinate3D(latitude: 20.0, longitude: 5.0)) == false)
#expect(Feature(outer).contains(Coordinate3D(latitude: 5.0, longitude: 5.0)))
}

// Validates that a `GeometryCollection` containing only non-polygon geometries contains nothing.
@Test
func geometryCollectionContainsNonPolygon() async throws {
let feature = Feature(GeometryCollection([Point(Coordinate3D(latitude: 5.0, longitude: 5.0))]))

#expect(feature.contains(Coordinate3D(latitude: 5.0, longitude: 5.0)) == false)
}

// MARK: - Grid size

// Validates that `contains(gridSize:)` on Polygon matches manual pre-snapping.
Expand Down Expand Up @@ -313,6 +372,57 @@ struct BooleanPointInPolygonTests {
#expect(polygon.contains(point))
}

// Feature with a GeometryCollection in EPSG:3857.
@Test
func featureContainsGeometryCollection3857() throws {
let polygon = try #require(Polygon([[
Coordinate3D(x: 0.0, y: 0.0),
Coordinate3D(x: 1_000.0, y: 0.0),
Coordinate3D(x: 1_000.0, y: 1_000.0),
Coordinate3D(x: 0.0, y: 1_000.0),
Coordinate3D(x: 0.0, y: 0.0),
]]))
let feature = Feature(GeometryCollection([polygon]))

#expect(feature.contains(Coordinate3D(x: 500.0, y: 500.0)))
#expect(feature.contains(Coordinate3D(x: 2_000.0, y: 500.0)) == false)
}

// Feature with a GeometryCollection in EPSG:4978.
@Test
func featureContainsGeometryCollection4978() throws {
let ring = [
Coordinate3D(x: 0.0, y: 0.0),
Coordinate3D(x: 1_000.0, y: 0.0),
Coordinate3D(x: 1_000.0, y: 1_000.0),
Coordinate3D(x: 0.0, y: 1_000.0),
Coordinate3D(x: 0.0, y: 0.0),
].map { $0.projected(to: .epsg4978) }
let polygon = try #require(Polygon([ring]))
let feature = Feature(GeometryCollection([polygon]))
let inside = Coordinate3D(x: 500.0, y: 500.0).projected(to: .epsg4978)
let outside = Coordinate3D(x: 5_000.0, y: 500.0).projected(to: .epsg4978)

#expect(feature.contains(inside))
#expect(feature.contains(outside) == false)
}

// Feature with a GeometryCollection in noSRID.
@Test
func featureContainsGeometryCollectionNoSRID() throws {
let polygon = try #require(Polygon([[
Coordinate3D(x: 0.0, y: 0.0, projection: .noSRID),
Coordinate3D(x: 1_000.0, y: 0.0, projection: .noSRID),
Coordinate3D(x: 1_000.0, y: 1_000.0, projection: .noSRID),
Coordinate3D(x: 0.0, y: 1_000.0, projection: .noSRID),
Coordinate3D(x: 0.0, y: 0.0, projection: .noSRID),
]]))
let feature = Feature(GeometryCollection([polygon]))

#expect(feature.contains(Coordinate3D(x: 500.0, y: 500.0, projection: .noSRID)))
#expect(feature.contains(Coordinate3D(x: 2_000.0, y: 500.0, projection: .noSRID)) == false)
}

// MARK: - Antimeridian

// Point in polygon near antimeridian.
Expand Down
Loading