Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polygon smooth #137

Merged
merged 1 commit into from
Mar 18, 2021
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Turf for Swift
# Turf for Swift

📱[![iOS](https://app.bitrise.io/app/49f5bcca71bf6c8d/status.svg?token=SzGBTkEtxsbuAnbcF9MTog&branch=main)](https://www.bitrise.io/app/49f5bcca71bf6c8d)    
🖥💻[![macOS](https://app.bitrise.io/app/b72273651db53613/status.svg?token=ODv2UnyAHoOxV8APATEBFw&branch=main)](https://www.bitrise.io/app/b72273651db53613)    
📺[![tvOS](https://app.bitrise.io/app/0b037542c2395ffb/status.svg?token=yOtMqbu-5bj8grB1Jmoefg)](https://www.bitrise.io/app/0b037542c2395ffb)    
⌚️[![watchOS](https://app.bitrise.io/app/0d4d611f02295183/status.svg?token=NiLB_E_0IvYYqV4Mj973TQ)](https://www.bitrise.io/app/0d4d611f02295183)    
<img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/TuxFlat.svg" width="20" alt="Linux">[![](https://api.travis-ci.com/mapbox/turf-swift.svg?branch=main)](https://travis-ci.com/mapbox/turf-swift) &nbsp;&nbsp;&nbsp;
[![codecov](https://codecov.io/gh/mapbox/turf-swift/branch/main/graph/badge.svg?token=WT3aUkO3BM)](https://codecov.io/gh/mapbox/turf-swift)
[![codecov](https://codecov.io/gh/mapbox/turf-swift/branch/main/graph/badge.svg?token=WT3aUkO3BM)](https://codecov.io/gh/mapbox/turf-swift)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) &nbsp;&nbsp;&nbsp;
[![CocoaPods](https://img.shields.io/cocoapods/v/Turf.svg)](http://cocoadocs.org/docsets/Turf/) &nbsp;&nbsp;&nbsp;
[![SPM compatible](https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat)](https://swift.org/package-manager/) &nbsp;&nbsp;&nbsp;
Expand Down Expand Up @@ -93,6 +93,7 @@ Turf.js | Turf-swift
[turf-nearest-point-on-line](https://github.com/Turfjs/turf/tree/master/packages/turf-nearest-point-on-line/) | `LineString.closestCoordinate(to:)`
[turf-polygon-to-line](https://github.com/Turfjs/turf/tree/master/packages/turf-polygon-to-line/) | `LineString(_:)`<br>`MultiLineString(_:)`<br>`FeatureCollection(_:)`
[turf-simplify](https://github.com/Turfjs/turf/tree/master/packages/turf-simplify) | `LineString.simplified(tolerance:highestQuality:)`
[turf-polygon-smooth](https://github.com/Turfjs/turf/tree/master/packages/turf-polygon-smooth) | `Polygon.smooth(iterations:)`
— | `CLLocationDirection.difference(from:)`<br>`LocationDirection.difference(from:)` on Linux
— | `CLLocationDirection.wrap(min:max:)`<br>`LocationDirection.wrap(min:max:)` on Linux

Expand Down
64 changes: 64 additions & 0 deletions Sources/Turf/Geometries/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,68 @@ extension Polygon {
}
return true
}

/// 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.
///
/// Ported from https://github.com/Turfjs/turf/blob/402716a29f6ae16bf3d0220e213e5380cc5a50c4/packages/turf-polygon-smooth/index.js
public func smooth(iterations: Int = 3) -> Polygon {
var poly = self
var tempOutput: [[LocationCoordinate2D]] = [[]];
var outCoords: [[LocationCoordinate2D]] = [[]];

(0..<iterations).forEach({ i in
tempOutput = [[]]

if (i > 0) {
poly = Polygon(outCoords);
}

processPolygon(poly, &tempOutput);
outCoords = tempOutput
})

return Polygon(outCoords);
}

private func processPolygon(_ poly: Polygon, _ tempOutput: inout [[LocationCoordinate2D]]) {
var coordIndex = 0
var prevGeomIndex = 0;
var geometryIndex = 0;
var subtractCoordIndex = 0;

(0..<poly.coordinates.count).forEach { j in
(0..<poly.coordinates[j].count - 1).forEach { k in
if (geometryIndex > prevGeomIndex) {
prevGeomIndex = geometryIndex;
subtractCoordIndex = coordIndex;
tempOutput.append([]);
}

let currentCoord = poly.coordinates[j][k]
let realCoordIndex = coordIndex - subtractCoordIndex;
let p1 = poly.coordinates[geometryIndex][realCoordIndex + 1];
let p0x = currentCoord.latitude;
let p0y = currentCoord.longitude;
let p1x = p1.latitude;
let p1y = p1.longitude;
tempOutput[geometryIndex].append(LocationCoordinate2D(
latitude: 0.75 * p0x + 0.25 * p1x,
longitude: 0.75 * p0y + 0.25 * p1y
));
tempOutput[geometryIndex].append(LocationCoordinate2D(
latitude: 0.25 * p0x + 0.75 * p1x,
longitude: 0.25 * p0y + 0.75 * p1y
));

coordIndex += 1
}

geometryIndex += 1
}

tempOutput.enumerated().forEach({ i, ring in
tempOutput[i] = ring + [ring[0]]
})
}
}
2 changes: 1 addition & 1 deletion Tests/TurfTests/GeoJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GeoJSONTests: XCTestCase {

let polygon = Geometry.polygon(.init(coordinates))
let polygonFeature = Feature(geometry: polygon)
XCTAssertEqual((polygonFeature.geometry.value as! Polygon).coordinates, coordinates)
XCTAssertEqual((polygonFeature.geometry.value as! Turf.Polygon).coordinates, coordinates)
}

func testMultiPoint() {
Expand Down
Loading