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
42 changes: 20 additions & 22 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
targets: ["GeoKitten"]),
],
dependencies: [
.package(url: "https://github.com/maparoni/geojsonkit.git", from: "0.5.2"),
.package(url: "https://github.com/maparoni/geojsonkit.git", from: "0.5.3"),
// .package(name: "GeoJSONKit", path: "../GeoJSONKit"),
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.0.0")),
],
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Turf.js | GeoJSONKit-Turf
----|----
[turf-along](https://github.com/Turfjs/turf/tree/master/packages/turf-along/) | `GeoJSON.LineString.coordinateFromStart(distance:)`
[turf-area](https://github.com/Turfjs/turf/blob/master/packages/turf-area/) | `GeoJSON.Polygon.area`
[turf-bbox-clip](https://turfjs.org/docs/#bboxClip) | `GeoJSON.Polygon.clip(to:)`
[turf-bearing](https://turfjs.org/docs/#bearing) | `GeoJSON.Position.direction(to:)`<br/> `RadianCoordinate2D.direction(to:)`
[turf-bezier-spline](https://github.com/Turfjs/turf/tree/master/packages/turf-bezier-spline/) | `GeoJSON.LineString.bezier(resolution:sharpness:)`
[turf-boolean-point-in-polygon](https://github.com/Turfjs/turf/tree/master/packages/turf-boolean-point-in-polygon) | `GeoJSON.Polygon.contains(_:)`
Expand Down
155 changes: 137 additions & 18 deletions Sources/GeoJSONKitTurf/Turf+Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension GeoJSON.Polygon {
}

var coordinates: [[GeoJSON.Position]] { positionsArray }

/// An area of current `.Polygon`
///
/// Ported from https://github.com/Turfjs/turf/blob/a94151418cb969868fdb42955a19a133512da0fd/packages/turf-area/index.js
Expand All @@ -54,6 +54,142 @@ extension GeoJSON.Polygon {
return true
}

/// Finds the nearest position on the polygon that's closest to the provided position.
///
/// If the provided point is contained by the polygon
public func nearestPoint(to position: GeoJSON.Position) -> GeoJSON.Position? {
if !exterior.contains(position, ignoreBoundary: false) {
return exterior.closestPosition(to: position)
}

if let inner = interiors.first(where: { $0.contains(position, ignoreBoundary: false) }) {
return inner.closestPosition(to: position)
}

// The exterior contains it, but none of the interiors do
// => The point is within the polygon
return position
}
}

// MARK: - Polygon.clip(_:)

extension GeoJSON.Polygon {

/// Clips a `.Polygon` to a bounding box
///
/// Ported from https://github.com/Turfjs/turf/blob/master/packages/turf-bbox-clip/index.ts
public func clip(to boundingBox: GeoJSON.BoundingBox) -> GeoJSON.Polygon {
var rings: [GeoJSON.Polygon.LinearRing] = []
for ring in [exterior] + interiors {
var clip: GeoJSON.Polygon.LinearRing = ring.clip(to: boundingBox)
if let first = clip.positions.first {
if first != clip.positions.last {
clip.positions.append(first)
}
if clip.positions.count >= 4 {
rings.append(clip)
}
}
}
return .init(rings.map(\.positions))
}

}

extension GeoJSON.Polygon.LinearRing {
// Sutherland-Hodgeman polygon clipping algorithm
func clip(to boundingBox: GeoJSON.BoundingBox) -> GeoJSON.Polygon.LinearRing {
var result = [GeoJSON.Position]()
var prev: GeoJSON.Position
var prevInside: Bool
var inside: Bool
var points = positions

for edge in [1, 2, 4, 8] {
result = []
prev = points.last!
prevInside = !(bitCode(p: prev, bbox: boundingBox) & edge != 0)

for point in points {
inside = !(bitCode(p: point, bbox: boundingBox) & edge != 0)

if inside != prevInside {
if let intersection = intersect(a: prev, b: point, edge: edge, bbox: boundingBox) {
result.append(intersection)
}
}

if inside {
result.append(point)
}

prev = point
prevInside = inside
}

points = result
if result.isEmpty {
break
}
}

return .init(positions: result)
}

private func intersect(a: GeoJSON.Position, b: GeoJSON.Position, edge: Int, bbox: GeoJSON.BoundingBox) -> GeoJSON.Position? {
if edge & 8 != 0 {
return .init(x: a.x + ((b.x - a.x) * (bbox.top - a.y)) / (b.y - a.y), y: bbox.top)
} else if edge & 4 != 0 {
return .init(x: a.x + ((b.x - a.x) * (bbox.bottom - a.y)) / (b.y - a.y), y: bbox.bottom)
} else if edge & 2 != 0 {
return .init(x: bbox.right, y: a.y + ((b.y - a.y) * (bbox.right - a.x)) / (b.x - a.x))
} else if edge & 1 != 0 {
return .init(x: bbox.left, y: a.y + ((b.y - a.y) * (bbox.left - a.x)) / (b.x - a.x))
} else {
return nil
}
}

private func bitCode(p: GeoJSON.Position, bbox: GeoJSON.BoundingBox) -> Int {
var code = 0

if p.x < bbox.left {
code |= 1
} else if p.x > bbox.right {
code |= 2
}

if p.y < bbox.bottom {
code |= 4
} else if p.y > bbox.top {
code |= 8
}

return code
}
}

fileprivate extension GeoJSON.BoundingBox {
var left: Double { southWesterlyLongitude }
var right: Double { northEasterlyLongitude }
var top: Double { northEasterlyLatitude }
var bottom: Double { southWesterlyLatitude }
}

fileprivate extension GeoJSON.Position {
var x: Double { longitude }
var y: Double { latitude }

init(x: Double, y: Double) {
self.init(latitude: y, longitude: x)
}
}

// MARK: - Polygon.smooth()

extension GeoJSON.Polygon {

/// Smooths a `.Polygon`. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).
/// Warning: may create degenerate polygons.
///
Expand All @@ -77,23 +213,6 @@ extension GeoJSON.Polygon {
return GeoJSON.Polygon(outCoords);
}

/// Finds the nearest position on the polygon that's closest to the provided position.
///
/// If the provided point is contained by the polygon
public func nearestPoint(to position: GeoJSON.Position) -> GeoJSON.Position? {
if !exterior.contains(position, ignoreBoundary: false) {
return exterior.closestPosition(to: position)
}

if let inner = interiors.first(where: { $0.contains(position, ignoreBoundary: false) }) {
return inner.closestPosition(to: position)
}

// The exterior contains it, but none of the interiors do
// => The point is within the polygon
return position
}

private func processPolygon(_ poly: GeoJSON.Polygon, _ tempOutput: inout [[GeoJSON.Position]]) {
var coordIndex = 0
var prevGeomIndex = 0;
Expand Down
Loading