From 21452ac7ee183930325e786575d552129c21b165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Sch=C3=B6nig?= Date: Tue, 21 Nov 2023 16:17:33 +1100 Subject: [PATCH] Allow opting out of bounding-box check (#16) * Allow opting out of bounding-box check * Update GHA * Update Package.swift and require newer OS versions --- .github/workflows/swift.yml | 32 +++++++++++-------- Package.swift | 8 +++-- README.md | 16 ++++++---- .../GeoJSONKitTurf/Turf+GeometryObject.swift | 12 +++---- Sources/GeoJSONKitTurf/Turf+LinearRing.swift | 12 ++++--- Sources/GeoJSONKitTurf/Turf+Polygon.swift | 6 ++-- 6 files changed, 48 insertions(+), 38 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index db8dbec..c17258c 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -2,28 +2,32 @@ name: Swift on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: macos: runs-on: macos-latest steps: - - uses: actions/checkout@v2 - - name: Build - run: swift build - - name: Test - run: swift test + - uses: actions/checkout@v3 + - name: Build + run: swift build + - name: Test + run: swift test linux: runs-on: ubuntu-latest + + strategy: + matrix: + swift: ["5.9.1", "5.7.3"] + container: - image: swift:5.4 + image: swift:${{ matrix.swift }} steps: - - uses: actions/checkout@v2 - - name: Build - run: swift build - - name: Test - run: swift test - + - uses: actions/checkout@v3 + - name: Build + run: swift build + - name: Test + run: swift test diff --git a/Package.swift b/Package.swift index 25b87a6..4577147 100644 --- a/Package.swift +++ b/Package.swift @@ -5,7 +5,7 @@ import PackageDescription let package = Package( name: "GeoJSONKitTurf", platforms: [ - .macOS(.v10_12), .iOS(.v10), .watchOS(.v3), .tvOS(.v12), + .macOS(.v12), .iOS(.v15), .watchOS(.v8), .tvOS(.v15), .custom("xros", versionString: "1.0") ], products: [ @@ -17,14 +17,16 @@ let package = Package( targets: ["GeoKitten"]), ], dependencies: [ - .package(name: "GeoJSONKit", url: "https://github.com/maparoni/geojsonkit.git", from: "0.5.2"), + .package(url: "https://github.com/maparoni/geojsonkit.git", from: "0.5.2"), // .package(name: "GeoJSONKit", path: "../GeoJSONKit"), .package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.0.0")), ], targets: [ .target( name: "GeoJSONKitTurf", - dependencies: ["GeoJSONKit"]), + dependencies: [ + .product(name: "GeoJSONKit", package: "geojsonkit"), + ]), .executableTarget( name: "GeoKitten", dependencies: [ diff --git a/README.md b/README.md index 362cc35..d762a5a 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ This package provides various geospatial extensions for [GeoJSONKit](https://git ## Requirements -GeoJSONKitTurf requires Xcode 12.x and supports the following minimum deployment targets: +GeoJSONKitTurf requires Xcode 14.x and supports the following minimum deployment targets: -* iOS 10.0 and above -* macOS 10.12 (Sierra) and above -* tvOS 10.0 and above -* watchOS 3.0 and above +- iOS 15 and above +- macOS 12 and above +- tvOS 15 and above +- watchOS 8.0 and above -It's also compatible with Linux (and possibly other platforms), as long as you have [Swift](https://swift.org/download/) 5.3 (or above) installed. +It's also compatible with Linux (and possibly other platforms), as long as you have [Swift](https://swift.org/download/) 5.7 (or above) installed. ## Installation @@ -27,7 +27,7 @@ To install GeoJSONKitTurf using the [Swift Package Manager](https://swift.org/pa .package(name: "GeoJSONKitTurf", url: "https://github.com/maparoni/geojsonkit-turf", from: "0.1.0") ``` -Then use: +Then use: ```swift import GeoJSONKitTurf @@ -85,6 +85,7 @@ First clone or download the repository, then run this: swift build -c release sudo cp .build/release/geokitten /usr/local/bin/geokitten ``` +
@@ -95,4 +96,5 @@ mint install maparoni/GeoJSONKit-Turf@main ``` If you get a permissions error, check [this Mint issue](https://github.com/yonaskolb/Mint/issues/188). +
diff --git a/Sources/GeoJSONKitTurf/Turf+GeometryObject.swift b/Sources/GeoJSONKitTurf/Turf+GeometryObject.swift index 814e87c..63dacf7 100644 --- a/Sources/GeoJSONKitTurf/Turf+GeometryObject.swift +++ b/Sources/GeoJSONKitTurf/Turf+GeometryObject.swift @@ -3,10 +3,10 @@ import Foundation import GeoJSONKit extension GeoJSON.Geometry { - public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false) -> Bool { + public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false, checkBoundingBox: Bool = true) -> Bool { switch self { case .polygon(let polygon): - return polygon.contains(coordinate, ignoreBoundary: ignoreBoundary) + return polygon.contains(coordinate, ignoreBoundary: ignoreBoundary, checkBoundingBox: checkBoundingBox) case .lineString, .point: return false } @@ -109,14 +109,14 @@ extension GeoJSON.GeometryObject { * * Calls contains function for each contained polygon */ - public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false) -> Bool { + public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false, checkBoundingBox: Bool = true) -> Bool { switch self { case .single(let geometry): - return geometry.contains(coordinate, ignoreBoundary: ignoreBoundary) + return geometry.contains(coordinate, ignoreBoundary: ignoreBoundary, checkBoundingBox: checkBoundingBox) case .multi(let geometries): - return geometries.contains(where: { $0.contains(coordinate, ignoreBoundary: ignoreBoundary) }) + return geometries.contains(where: { $0.contains(coordinate, ignoreBoundary: ignoreBoundary, checkBoundingBox: checkBoundingBox) }) case .collection(let objects): - return objects.contains(where: { $0.contains(coordinate, ignoreBoundary: ignoreBoundary) }) + return objects.contains(where: { $0.contains(coordinate, ignoreBoundary: ignoreBoundary, checkBoundingBox: checkBoundingBox) }) } } } diff --git a/Sources/GeoJSONKitTurf/Turf+LinearRing.swift b/Sources/GeoJSONKitTurf/Turf+LinearRing.swift index 8f2992a..44e2bb0 100644 --- a/Sources/GeoJSONKitTurf/Turf+LinearRing.swift +++ b/Sources/GeoJSONKitTurf/Turf+LinearRing.swift @@ -55,7 +55,7 @@ extension GeoJSON.Polygon.LinearRing { * * Ported from: https://github.com/Turfjs/turf/blob/e53677b0931da9e38bb947da448ee7404adc369d/packages/turf-boolean-point-in-polygon/index.ts#L77-L108 */ - public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false) -> Bool { + public func contains(_ coordinate: GeoJSON.Position, ignoreBoundary: Bool = false, checkBoundingBox: Bool = true) -> Bool { // Optimization for triangles, using barycentric method guard positions.count > 3 else { @@ -83,10 +83,12 @@ extension GeoJSON.Polygon.LinearRing { : (s >= 0 && s + t <= a) } - - let bbox = GeoJSON.BoundingBox(positions: positions) - guard bbox.contains(coordinate, ignoreBoundary: ignoreBoundary) else { - return false + + if checkBoundingBox { + let bbox = GeoJSON.BoundingBox(positions: positions) + guard bbox.contains(coordinate, ignoreBoundary: ignoreBoundary) else { + return false + } } let coordinates = positions diff --git a/Sources/GeoJSONKitTurf/Turf+Polygon.swift b/Sources/GeoJSONKitTurf/Turf+Polygon.swift index 5e69270..45db2c3 100644 --- a/Sources/GeoJSONKitTurf/Turf+Polygon.swift +++ b/Sources/GeoJSONKitTurf/Turf+Polygon.swift @@ -42,12 +42,12 @@ extension GeoJSON.Polygon { /// lies on the boundary line of the polygon or its interior rings. /// ///Ported from: https://github.com/Turfjs/turf/blob/e53677b0931da9e38bb947da448ee7404adc369d/packages/turf-boolean-point-in-polygon/index.ts#L31-L75 - public func contains(_ position: GeoJSON.Position, ignoreBoundary: Bool = false) -> Bool { - guard exterior.contains(position, ignoreBoundary: ignoreBoundary) else { + public func contains(_ position: GeoJSON.Position, ignoreBoundary: Bool = false, checkBoundingBox: Bool = true) -> Bool { + guard exterior.contains(position, ignoreBoundary: ignoreBoundary, checkBoundingBox: checkBoundingBox) else { return false } for ring in interiors { - if ring.contains(position, ignoreBoundary: !ignoreBoundary) { + if ring.contains(position, ignoreBoundary: !ignoreBoundary, checkBoundingBox: false) { return false } }