gpt4 book ai didi

ios - 减少坐标数组

转载 作者:可可西里 更新时间:2023-11-01 01:35:52 30 4
gpt4 key购买 nike

我将用户在我的应用程序中的位置跟踪到包含所有坐标的数据库中。然后我做了一些事情来选择一个时间范围内的坐标范围,但是当我将它保存到服务器时由于数据量大需要很长时间。 (15 分钟是 900 个 CLCoordinate2D,这是相当多的)。

我想做的是删除与前后坐标相交的坐标。出于说明目的使用过于简单的坐标,但想象一下这是在包含几千个对象的数组中的真实坐标上完成的。

例子:

0,0 //Keep
1,1 //Drop
2,2 //Drop
3,3 //Keep
3,4 //Keep
4,4 //Keep
5,3 //Keep

或者,糟糕的可视化: enter image description here

我知道我可能应该使用一些向量的东西,但我不擅长数学。我怎样才能减少这个数组以删除过时的点?

最佳答案

你可以试试这样的……

var coordTimes:[(coord: CLLocationCoordinate2D, time: Double)] = []
// ...
func appendCoord(newCoord: CLLocationCoordinate2D, newTime: Double) {
guard coordTimes.count > 1 else {
coordTimes.append((newCoord, newTime))
return
}
let n = coordTimes.count
// So there are at least two already in the array
let c0 = coordTimes[n - 2].coord
let t0 = coordTimes[n - 2].time
let c1 = coordTimes[n - 1].coord
let t1 = coordTimes[n - 1].time
let dt = t1 - t0
let dtNew = newTime - t0

guard (dtNew > 0) && (dt > 0) else {
// decide what to do if zero time intervals. Shouldn't happen
return
}
// Scale the deltas by the time interval...
let dLat = (c1.latitude - c0.latitude) / dt
let dLon = (c1.longitude - c0.longitude) / dt
let dLatNew = (newCoord.latitude - c0.latitude) / dtNew
let dLonNew = (newCoord.longitude - c0.longitude) / dtNew

let tolerance = 0.00001 // arbitrary - choose your own
if (abs(dLat - dLatNew) <= tolerance) && (abs(dLon - dLonNew) <= tolerance) {
// Can be interpolated - replace the last one
coordTimes[n - 1] = (newCoord, newTime)
} else {
// Can't be interpolated, append new point
coordTimes.append((newCoord, newTime))
}
}

公差很重要,因为您不太可能获得完全匹配的间隔。此外,对于你们当中的测地学家来说,无需转换为 map 坐标或计算真实距离,因为 OP 只是想知道坐标是否可以插值。

关于ios - 减少坐标数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385434/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com