折线简化。简化港.js
行简化器
LineSimplifier 是一个用于简化行的小型 Swift 包。
一条线连接多个点,不需要是直的。
在此包中,行也可以称为路径或路由。
局限性:此软件包仅适用于 2 维点。
有时你有很多点来描述这条线,但它们并不是全部需要的,因为一条粗略的线就足够了。
在一条线上使用大量点需要大量资源,而这些资源并不总是需要的。
所以简化版本的生产线就足够了
这个 Swift 包的灵感来自 Java 中的原始简化和 Swift 翻译。但是,我觉得它的快速版本可以使用一些改进。
全球定位系统轨迹示例
使用此软件包的一个现实示例是 GPS 轨迹。
根据数据的不同,它可能包括大量数据,这些数据并不是简单地在地图上绘制GPS路线所需要的。
特别是当显示多个路由时,当仅使用原始路由数据时,内存可能会成为问题。
使用LineSimplifier,可以简化路线,并且会丢失很多点,从而节省内存。
底层算法
LineSimplifier基于Ramer-Douglas-Peucker算法。
使用行简化器
LineSimplifier附带协议。只需使你的类型符合此协议即可使用简化算法。
符合协议后,只需使用该功能即可。使用普拉米来控制简化程度。
使用该参数获得最佳质量。但是,更好的质量需要更多的时间来计算简化。
如果使用,则仅使用Ramer-Douglas-Peucker算法。如果设置为 false,则使用径向距离的预滤波来节省 Ramer-Douglas-Peucker 算法中的一些步骤。Point2DRepresentable
LineSimplifier.simplify
tolerance
useHighestQuality
useHighestQuality
useHighestQuality
LineSimplifier 将一致性添加到开箱即用和开箱即用。
对于其他类型,您需要自己实现该协议。Point2DRepresentable
CLLocationCoordinate2D
CGPoint
这里有一个小例子:
import CoreLocation
import LineSimplifier
let points: [CLLocationCoordinate2D] = ...
let result = LineSimplifier.simplify(points: points, withTolerance: 0.003)
let result2 = LineSimplifier.simplify(points: points, withTolerance: 0.003, useHighestQuality: true)
With the extension you can also use it just like this:Array
import CoreLocation
import LineSimplifier
let points: [CLLocationCoordinate2D] = ...
let result = points.simplify(withTolerance: 0.003)
let result2 = points.simplify(withTolerance: 0.003, useHighestQuality: true)
Tolerance
The value of the tolerance heavyly depends on the points range.
For GPS coordinates the tests resulted like so:
Tolerance | Resulting points |
---|---|
0.0001 | 107 |
0.00001 | 818 |
0.000001 | 3.575 |
0.0000001 | 10.088 |
0.00000001 | 12.798 |
Original | 13.395 |
The tolerance needs to be quite small, because it is in gps coordinates nature to only differ in decimal points for distances in meter range.