SwiftSimplify

repository·master·Indexed 43 days ago

https://github.com/malcommac/swiftsimplify

A high-performance Swift library for polyline simplification ported from Simplify.js. It uses Douglas-Peucker and Radial Distance algorithms via the `SwiftSimplify.simplify(_:tolerance:highQuality:)` method to reduce point counts in polylines while preserving shape. Supports CGPoint and CLLocationCoordinate2D through the Point2DRepresentable protocol.

Tokens
416
Snippets
2
Records
3
Agent score
5%

What's inside SwiftSimplify

  1. Install SwiftSimplify via CocoaPods or SPM

    master

    You can install SwiftSimplify using CocoaPods or Swift Package Manager (SPM).

    CocoaPods

    Add the following line to your Podfile:

    use_frameworks!
    pod 'SwiftSimplify'

    Swift Package Manager (SPM)

    Add the following dependency to your Package.swift:

    .Package(url: "https://github.com/malcommac/SwiftSimplify.git", majorVersion: 0)
    use_frameworks!
    pod 'SwiftSimplify'
  2. Simplify polylines with SwiftSimplify.simplify()

    master

    To simplify a polyline, call the SwiftSimplify.simplify(_:tolerance:highQuality:) class method. This method reduces the number of points in a polyline while retaining its shape using a combination of Douglas-Peucker and Radial Distance algorithms.

    Parameters

    • points: An array of objects conforming to the Point2DRepresentable protocol. Both CGPoint and CLLocationCoordinate2D conform to this protocol.
    • tolerance: A Double (defaults to 1) that affects the amount of simplification. The value is in the same metric as the point coordinates.
    • highQuality: A Bool (defaults to false). When set to true, it excludes the distance-based preprocessing step. This results in the highest quality simplification but runs approximately 10-20 times slower.
    let allPoints: [Point2DRepresentable] = ...
    let simplifiedPoints = SwiftSimplify.simplify(allPoints, tolerance: tolerance)