2016-05-23 100 views
2

我将我的应用程序中的用户位置跟踪到所有坐标数据库。然后我做了一些事情来选择时间范围内的一系列坐标,但是当我将它保存到服务器时,由于数据量很大,需要很长时间。 (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

我知道,我也许应该使用一些矢量的东西,但我不擅长数学。 如何减少此数组以删除过时的点?

+0

它们在时间上均匀分布吗? – Grimxn

+0

带有BestForNavigation的位置管理器大约每1秒发送一次更新。 –

回答

2

你可以尝试这样的事情......

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)) 
    } 
} 

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

+0

这看起来很有希望,会马上测试一下。我没有看到时间的相关性。也许我应该更具描述性,但是位置已经被保存并且从最旧到最新排序,而不是随时随地添加。 –

+0

如果是这种情况,只需将时间替换为原始索引号(而不是压缩索引号) – Grimxn