SimplifySwift is a high-performance Swift polyline simplification library ported from Simplify.js. The original Javascript library was extracted from Leaflet, a JS interactive maps library by Vladimir Agafonkin. It uses a combination of Douglas-Peucker and Radial Distance algorithms.
Polyline simplification dramatically reduces the number of points in a polyline while retaining its shape, giving a huge performance boost when processing it and also reducing visual noise. For example, it's essential when rendering a large polyline in MapKit.
To run the example project, clone the repo and open:
Example/Simplify-Swift.xcodeproj
SimplifySwift 2.0.0 and later are distributed via Swift Package Manager.
To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift:
.package(url: "https://github.com/tomislav/Simplify-Swift.git", .upToNextMajor(from: "2.0.0"))static func simplify<T: SimplifyValue>(_ points: [T], tolerance: Double = 1, highQuality: Bool = false) -> [T]Returns an array of simplified points
- parameter
points: An array of points inSimplifyValue {x: Double, y: Double}format. Generics are supported so you can pass[CGPoint],[CLLocationCoordinate2D], or your own model type. - parameter note for
CLLocationCoordinate2D:xValuemaps tolatitude,yValuemaps tolongitude. - parameter
tolerance: Affects the amount of simplification (in the same metric as the point coordinates). Default:1. - parameter
highQuality: Excludes distance-based preprocessing step which leads to highest quality simplification but runs ~10-20 times slower. Default: false.
Tomislav Filipcic, tf@7sols.com, @tomislav
SimplifySwift is available under the MIT license. See the LICENSE file for more info.
