从文本日志文件建议中提取数据

时间:2014-07-09 08:01:26

标签: php

我想就这项任务的最佳方法提出建议。

我有一个文本日志文件,用于保存gps中的数据,格式为:

time,lat,lon,elevation,accuracy,bearing,speed

2014-07-08T12:56:52Z,56.187344,10.192660,116.400024,5.000000,285.000000,1.063350

2014-07-08T12:56:58Z,56.187299,10.192754,113.799988,5.000000,161.000000,3.753000

2014-07-08T12:57:07Z,56.186922,10.193048,129.200012,5.000000,159.000000,5.254200

2014-07-08T12:57:13Z,56.186694,10.193133,109.799988,5.000000,152.000000,3.878100

2014-07-08T12:57:16Z,56.186745,10.193304,142.900024,5.000000,149.000000,3.940650

2014-07-08T12:57:20Z,56.186448,10.193417,118.700012,5.000000,154.000000,2.376900

2014-07-08T12:57:27Z,56.186492,10.193820,131.299988,5.000000,65.000000,5.379300

我需要找到速度超过某个值的线,然后从该线获取时间,然后滚动线并找到速度低于此值的线,得到时间并写下这两个时间值到我的数据库中。

这必须是一个自动化任务,所以我假设一个cron PHP脚本可以完成这项工作。

最好的问候托马斯

1 个答案:

答案 0 :(得分:0)

尽管事实上并不需要特别的建议,但希望有人为您的问题编码 - 我会尽力让您朝着正确的方向前进。我编写了易于理解的代码,您可以在其中构建(未经测试)......

<?php
// Setup.
$pathGpsFile = 'gps.log';
$speedThreshold = 5;

//
// Execution.
//
if(!file_exists($pathGpsFile)) {
    die('File "'. $pathGpsFile .'" does not exist.');
}

// Read entries into array.
$gpsEntries = file($pathGpsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Loop through entries.
$lineCount = 0;
$currentDifferences = array();
$currentDifference = array();

foreach($gpsEntries as $gpsEntry) {
    // Skip head.
    if($lineCount == 0) {
        $lineCount++;
        continue;
    }

    // Extract values from gps entry.
    list($time, $lat, $lon, $elevation, $accuracy, $bearing, $speed) = explode(',', $gpsEntry);

    // Check if there is currently a difference monitored.
    if(count($currentDifference) == 1) {
        if($speed < $speedThreshold) {
            $currentDifference[] = $gpsEntry;
        }

        // Add to differences list.
        $currentDifferences[] = $currentDifference;

        // Reset current difference.
        $currentDifference = array();
    } else {
        if($speed > $speedThreshold) {
            $currentDifference[] = $gpsEntry;
        }
    }

    // Increase line count.
    $lineCount++;
}

// Check output.
var_dump($currentDifferences);
?>