Giter Site home page Giter Site logo

malcommac / swiftsimplify Goto Github PK

View Code? Open in Web Editor NEW
295.0 10.0 34.0 3.06 MB

🥷 High-performance polyline simplification library - port of simplify.js

License: MIT License

Swift 94.94% Ruby 5.06%
polyline swift-library simplifyjs polyline-simplification polyline-algorithm cgpoint cllocationcoordinate2d

swiftsimplify's Introduction

SwiftSimplify

High-performance polyline simplification library

SwiftSimplify is a tiny high-performance Swift polyline simplification library ported from Javascript's Simplify.js. Original work come from Leaflet, a JS interactive maps library by Vladimir Agafonkin.

It uses a combination of Douglas-Peucker and Radial Distance algorithms. Works both on browser and server platforms.

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 70k-points line chart or a map route in the browser using MapKit.

SwiftSimplify

Requirements

  • iOS 8.0, watchOS 2.0, tvOS 9.0
  • Swift 5.x+

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

SwiftSimplify is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftSimplify"

Usage

Usage is pretty straightforward: in fact you need just call the SwiftSimplify's class method simplify by passing your configuration:

let allPoints: [Point2DRepresentable] = ...
let simplifiedPoints = SwiftSimplify.simplify(allPoints, tolerance: tolerance)

Allowed parameters are:

  • points: An array of points. SwiftSimplify supports Swift's generic so you can pass an array of objects which are conforms to Point2DRepresentable protocol (both CGPoint and CLLocationCoordinate2D objects supports it).
  • tolerance: (1 by default) Affects the amount of simplification (in the same metric as the point coordinates)
  • highQuality: (false by default) Excludes distance-based preprocessing step which leads to highest quality simplification but runs ~10-20 times slower.

❤️ Your Support

Hi fellow developer!
You know, maintaing and developing tools consumes resources and time. While I enjoy making them your support is foundamental to allow me continue its development.

If you are using SwiftSimplify or any other of my creations please consider the following options:

Installation

SwiftSimplify is compatible with Swift 5.x+ under iOS (11+) and macOS platforms.

You can install it via CocoaPods:

use_frameworks!
pod 'SwiftSimplify'

or SPM in your Package.swift:

import PackageDescription

  let package = Package(name: "YourPackage",
    dependencies: [
      .Package(url: "https://github.com/malcommac/SwiftSimplify.git", majorVersion: 0),
    ]
  )

Consider ❤️ support the development of this library!

Contributing

  • If you need help or you'd like to ask a general question, open an issue.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Copyright & Acknowledgements

SwiftSimplify is currently owned and maintained by Daniele Margutti.
You can follow me on Twitter @danielemargutti.
My web site is https://www.danielemargutti.com

This software is licensed under MIT License.

Follow me on:

swiftsimplify's People

Contributors

coledunsby avatar malcommac avatar matvdg avatar overswift avatar sobri909 avatar tackgyu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftsimplify's Issues

Error or less-than-ideal-naming on distance functions

public func distanceFrom(_ otherPoint: Self) -> Float {

These appear to be returning a standard Pythagoras result, but the result is not square rooted. AFAIK this is not meant to be the case but I haven't had time to dig (I only know the DP algorithm at a distance).

If this is meant to be a linear distance, it looks like it needs to be square rooted. Else it might help to modify the function names to indicate that this is a squared result - otherwise it looks like a straightforward distance calculation function.

Tolerance not working as intended?

Hi all I'm currently using SwiftSimplify to reduce the number of coordinates while running, I'm testing runs that go on for about 1 KM but after using Simplify and giving it about 20 points, all it does is return start and end coordinates.

Simplify is currently like this.
SwiftSimplify.simplify(self.activityPolyline!, tolerance: 10, highQuality: false)

Giving it an array of coordinates like this:
[(37.330075379999997, -122.02126848), (37.330058049999998, -122.02100589), (37.33000706, -122.02074717000001), (37.329941720000001, -122.02053031), (37.329871330000003, -122.0202987), (37.329823259999998, -122.02006170999999), (37.329705570000002, -122.01986866), (37.329525689999997, -122.01982409), (37.329339840000003, -122.01980544), (37.329133579999997, -122.0197871), (37.328938319999999, -122.01979684), (37.328738469999998, -122.01980584), (37.328532039999999, -122.0198032), (37.32833935, -122.01980995), (37.32814552, -122.01983108), (37.327952580000002, -122.01982651), (37.327758189999997, -122.01980315), (37.327560220000002, -122.01974095), (37.327370989999999, -122.01972170000001), (37.327185819999997, -122.01971872), (37.326979710000003, -122.01972916)]

After simplify I get this:
[(37.330075379999997, -122.02126848), (37.326979710000003, -122.01972916)]

What am I doing wrong? The location manager is currently configured as follows:
locationManager.activityType = .fitness
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 20

Appreciate the help.

Use projected coordinates when simplifying CLLocationCoordinate2D data

If you do calculations based on latitude/longitude, then:

  1. You get different scales on the x/y-axis which leads to weird simplification logic
  2. You can't relate to the tolerance value

The solution would be to e.g. convert it to MKMapPoints before simplification and if the data points are MKMapPoints you could interpret the tolerance value as meters and it would be both correct (1 above) and use a tolerance that you can relate to (2 above).

API for using ArraySlice

For my work I needed to keep array copies at a minimum (dealing with arrays with 100s of 1000s of points) and modified the code here to include an ArraySlice API for dealing with large subsets of the big arrays

Any interest in a pull request for that API? I wasn't sure how to handle the highestQuality parameter - in my use case I took it out to prevent an array copy but I think the simplifyRadialDistance method could be overloaded to slice the input array even thinner.

Swift 2.0 & iOS 7

  1. I found out that it perfect works on iOS7. Why you decided to limit podspec on iOS 8+?
  2. Code is not optimized for Swift 2.0

[Enhancement]check count before func simplify

In func simplifyRadialDistance, it use force unwrap var prevPoint: T = points.first! and var point: T = points[1]. This would cause crash when input parameter points is just 0 or 1 count.
Although I add something like guard line.count > 1 before use SwiftSimplify.simplify to avoid crash, hope this check could add, thank you very much.

Pod doesn't work

After installing the pod I tried calling it like I would another pod (say like Alamofire) and the methods were not accessible. It did work, however, if I simply added it to my project.

Extensions not public

Referencing instance method 'simplify(tolerance:highestQuality:)' on 'Array' requires the types 'CLLocationCoordinate2D' and 'Point2DRepresentable' be equivalent

When the lib is added to a project via CocoaPods, the extensions on CLLocationCoordinate2D etc are not visible.

Returned array with only two values

Hi, I'm using the pods and when I try to simplify my [CCLocation2D] consisting in 2125 points the function return my an array with only 2 values.

I'm doing this:

let simplifiedCoordinates = SwiftSimplify.simplify(coordinates!, tolerance: 1.75) (I've tried also with 1 tolerance, but nothing change)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.