从kml轨道gps坐标计算速度的算法

时间:2014-05-02 08:30:45

标签: android gps geolocation kml kmz

我正在尝试使用以下算法解析kml文件以计算两个相邻点之间的速度(类似于myTracks Android应用程序所做的)

public double calculateSpeed(Location prev, Location curr) {

    double deltaDistance = curr.distanceTo(prev);
    double deltaTime = (curr.getTime() - prev.getTime()) / 1000.0;
    double speedInMph = deltaDistance / deltaTime * 2.2369;
    return speedInMph;
}

初始化位置更新的代码:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);         
Criteria criteria = new Criteria();         
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider,1000,5,locationListener);

示例kml文件和日志文件的链接:

https://dl.dropboxusercontent.com/u/29927114/kml/doc.kml https://dl.dropboxusercontent.com/u/29927114/kml/Log.txt

但我的问题是有时价值观不正确。

例如:在下面的日志中,速度值为74,73,71, 39,97 ,75,74和22,22, 10,3085 ,20,20。除了突出显示的偏差值外,其余值看起来都是正确的。有没有办法找到/丢弃这些值,或者我的算法存在根本缺陷?

05-02 09:08:10.657: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:32.070Z
05-02 09:08:10.657: D/KMLParser(11545): Attributes: gx:coord: -1.40748616 52.13896311 175.0
05-02 09:08:10.658: I/KMLParser(11545): 1. -1.40783922, 52.13916636   2. -1.40748616, 52.13896311
05-02 09:08:10.658: I/KMLParser(11545): Dist = 33.102333068847656, Time = 0.999 mps = 74.1207295712766
05-02 09:08:10.658: E/KMLParser(11545): Index = 2, Speed = 74.0
05-02 09:08:10.658: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:33.073Z
05-02 09:08:10.658: D/KMLParser(11545): Attributes: gx:coord: -1.4071429 52.13875571 174.0
05-02 09:08:10.659: I/KMLParser(11545): 1. -1.40748616, 52.13896311   2. -1.4071429, 52.13875571
05-02 09:08:10.659: I/KMLParser(11545): Dist = 32.93754959106445, Time = 1.003 mph = 73.45763178489739
05-02 09:08:10.659: E/KMLParser(11545): Index = 3, Speed = 73.0
05-02 09:08:10.659: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:34.073Z
05-02 09:08:10.659: D/KMLParser(11545): Attributes: gx:coord: -1.40680545 52.13855525 173.0
05-02 09:08:10.660: I/KMLParser(11545): 1. -1.4071429, 52.13875571   2. -1.40680545, 52.13855525
05-02 09:08:10.660: I/KMLParser(11545): Dist = 32.11391067504883, Time = 1.0 mph = 71.83560678901672
05-02 09:08:10.660: E/KMLParser(11545): Index = 4, Speed = 71.0
05-02 09:08:10.660: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:35.073Z
05-02 09:08:10.660: D/KMLParser(11545): Attributes: gx:coord: -1.40663056 52.13844134 173.0
05-02 09:08:10.661: I/KMLParser(11545): 1. -1.40680545, 52.13855525   2. -1.40663056, 52.13844134
05-02 09:08:10.661: I/KMLParser(11545): Dist = 17.43634796142578, Time = 1.0 mph = 39.00336675491333
05-02 09:08:10.661: E/KMLParser(11545): Index = 5, Speed = 39.0
05-02 09:08:10.661: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:36.601Z
05-02 09:08:10.661: D/KMLParser(11545): Attributes: gx:coord: -1.40592909 52.13802792 172.0
05-02 09:08:10.662: I/KMLParser(11545): 1. -1.40663056, 52.13844134   2. -1.40592909, 52.13802792
05-02 09:08:10.662: I/KMLParser(11545): Dist = 66.50345611572266, Time = 1.528 mph = 97.35705561862565
05-02 09:08:10.662: E/KMLParser(11545): Index = 6, Speed = 97.0
05-02 09:08:10.662: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:37.601Z
05-02 09:08:10.662: D/KMLParser(11545): Attributes: gx:coord: -1.40557412 52.13781792 172.0
05-02 09:08:10.663: I/KMLParser(11545): 1. -1.40592909, 52.13802792   2. -1.40557412, 52.13781792
05-02 09:08:10.663: I/KMLParser(11545): Dist = 33.71452713012695, Time = 1.0 mph = 75.41602573738098
05-02 09:08:10.663: E/KMLParser(11545): Index = 7, Speed = 75.0
05-02 09:08:10.663: D/KMLParser(11545): Attributes: when: 2014-04-30T17:03:38.600Z
05-02 09:08:10.663: D/KMLParser(11545): Attributes: gx:coord: -1.40522992 52.13760866 172.0
05-02 09:08:10.663: I/KMLParser(11545): 1. -1.40557412, 52.13781792   2. -1.40522992, 52.13760866
05-02 09:08:10.663: I/KMLParser(11545): Dist = 33.12906265258789, Time = 0.999 mph = 74.18058082840226
05-02 09:08:10.663: E/KMLParser(11545): Index = 8, Speed = 74.0

05-02 09:08:11.551: E/KMLParser(11545): Index = 1035, Speed = 19.0
05-02 09:08:11.551: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:54.642Z
05-02 09:08:11.552: D/KMLParser(11545): Attributes: gx:coord: -1.20618821 51.89674183 128.0
05-02 09:08:11.556: I/KMLParser(11545): 1. -1.20617359, 51.89683068   2. -1.20618821, 51.89674183
05-02 09:08:11.556: I/KMLParser(11545): Dist = 9.93702220916748, Time = 0.998 mph = 22.272670320327393
05-02 09:08:11.556: E/KMLParser(11545): Index = 1036, Speed = 22.0
05-02 09:08:11.556: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:55.642Z
05-02 09:08:11.556: D/KMLParser(11545): Attributes: gx:coord: -1.20618857 51.89665264 129.0
05-02 09:08:11.557: I/KMLParser(11545): 1. -1.20618821, 51.89674183   2. -1.20618857, 51.89665264
05-02 09:08:11.557: I/KMLParser(11545): Dist = 9.92379093170166, Time = 1.0 mph = 22.198527935123444
05-02 09:08:11.557: E/KMLParser(11545): Index = 1037, Speed = 22.0
05-02 09:08:11.557: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:57.636Z
05-02 09:08:11.557: D/KMLParser(11545): Attributes: gx:coord: -1.20619499 51.89656647 129.0
05-02 09:08:11.558: I/KMLParser(11545): 1. -1.20618857, 51.89665264   2. -1.20619499, 51.89656647
05-02 09:08:11.558: I/KMLParser(11545): Dist = 9.597917556762695, Time = 1.994 mph = 10.767092167864831
05-02 09:08:11.558: E/KMLParser(11545): Index = 1038, Speed = 10.0
05-02 09:08:11.558: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:57.643Z
05-02 09:08:11.558: D/KMLParser(11545): Attributes: gx:coord: -1.20619719 51.89647971 130.0
05-02 09:08:11.559: I/KMLParser(11545): 1. -1.20619499, 51.89656647   2. -1.20619719, 51.89647971
05-02 09:08:11.559: I/KMLParser(11545): Dist = 9.654572486877441, Time = 0.007 mph = 3085.1875994137354
05-02 09:08:11.559: E/KMLParser(11545): Index = 1039, Speed = 3085.0
05-02 09:08:11.559: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:58.639Z
05-02 09:08:11.559: D/KMLParser(11545): Attributes: gx:coord: -1.20620314 51.89639872 130.0
05-02 09:08:11.560: I/KMLParser(11545): 1. -1.20619719, 51.89647971   2. -1.20620314, 51.89639872
05-02 09:08:11.560: I/KMLParser(11545): Dist = 9.020686149597168, Time = 0.996 mph = 20.25941048999388
05-02 09:08:11.560: E/KMLParser(11545): Index = 1040, Speed = 20.0
05-02 09:08:11.560: D/KMLParser(11545): Attributes: when: 2014-04-30T17:22:59.638Z
05-02 09:08:11.560: D/KMLParser(11545): Attributes: gx:coord: -1.2062078 51.89631631 129.0
05-02 09:08:11.560: I/KMLParser(11545): 1. -1.20620314, 51.89639872   2. -1.2062078, 51.89631631
05-02 09:08:11.560: I/KMLParser(11545): Dist = 9.174989700317383, Time = 0.999 mph = 20.54407853917913
05-02 09:08:11.560: E/KMLParser(11545): Index = 1041, Speed = 20.0

1 个答案:

答案 0 :(得分:3)

我认为问题出在GPS日志中的小误差。当你取得彼此非常接近的点(30 - 60米)时,一个小的变化几乎可以加倍速度。

我相信你可以这样清理你的数据:

public double calculateSpeed(Location prev, Location curr, double lastSpeed) {

    double deltaDistance = curr.distanceTo(prev);
    double deltaTime = (curr.getTime() - prev.getTime()) / 1000.0;
    double speedInMph = deltaDistance / deltaTime * 2.2369;

    if (abs(speedInMph - lastSpeed) > lastSpeed/100*20) {  //This check that the speed difference is no bigger than 20% of the last known valid speed
        return -9999; //Or anything that make signal a problem
    }


    return speedInMph;
}

因此,通过添加最后已知速度作为输入,您可以控制计算出的速度并非疯狂。但是,即使这可能有效,通过使用移动平均线也可能会获得更好的结果。问题在于,如果汽车突然刹车,它可能会显示出奇怪的行为,因为它可能会认为这是数据中的一个问题。

我看到的最佳解决方案如下:

  1. 像以前那样做
  2. 编写另一个代码,该代码将pt.1的输出作为输入。此代码将定位并消除速度的不规则性 - >因此,如果汽车刹车并以0英里/小时的速度停留一段时间,那么这不是一个孤立的疯狂速度,代码也不会将其删除。