我在 PHP 中有一个 foreach 循环,它从 GPX 文件中剔除数字并生成一个表格。
例如:
Distance | Elevation
--------------------
0 | 268
0.27 | 294
0.87 | 294
1.14 | 321
1.25 | 324
1.49 | 371
2.30 | 399
3.00 | 372
3.91 | 346
我想在海拔升高时加起来。所以,对于这张表:
增加:294 - 268 = 26, 294 - 294 = 0 (26), 321 - 294 = 27 (53), 324 - 321 = 3 (56).....等等
我还想计算它何时减少并将其分开以获得总增加和总高度减少。
所以,我基本上想问一下之前的每个数字是更高还是更低,然后根据它添加到一个变量中以备后用。
有任何想法吗?
我试图保持简单,但要求代码......提前道歉!
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
//$xmlUrl = "../wp-content/uploads/2012/11/stoodley-pike-walk-cragg-vale-withens-clough-circular.gpx"; // XML feed file/URL
$uploadyr = $_GET['yr'];
$uploadmth = $_GET['mth'];
$gpx = $_GET['gpx'];
$xmlUrl = "../wp-content/uploads/" . $uploadyr . "/" . $uploadmth . "/" . $gpx . ".gpx";
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
?>
<!doctype html>
<head>
<title>Graph</title>
<!--<link href="visualize.css" rel="stylesheet">
<link href="visualize-light.css" rel="stylesheet">-->
<link href="visualize-override.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="visualize.jQuery.js"></script>
<script>
$(document).ready(function(){
$('#elevation').visualize({
type: 'area',
parseDirection: 'y',
colors: ['#339933','#00ff00','#0000ff','#ffff00','#ff00ff','#00ffff','#000000','#666666','#AAAAAA']
});
$('.visualize-labels-x li:odd').hide();
});
</script>
</head>
<body>
<div id="wrapper">
<?php
echo '<table id="elevation"><caption>Elevation Profile</caption><thead><td>Total Distance (mi)</td><th>Elevation</th></thead><tbody>';
foreach($arrXml["rte"]["rtept"] as $single){
$lat1 = $prevLat;
$lng1 = $prevLon;
if($prevLat == null && $prevLon == null){
$lat1 = $single["@attributes"]["lat"];
$lng1 = $single["@attributes"]["lon"];
}
$lat2 = $single["@attributes"]["lat"];
$lng2 = $single["@attributes"]["lon"];
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
$distance += $km;
$buildrow = '';
$buildrow .= '<tr>';
if($prevLat !== null && $prevLon !== null){
$buildrow .= '<th>' . number_format(($miles ? ($distance * 0.621371192) : $distance),2) . '</th>';
} else {
$buildrow .= '<th>0</th>';
}
$buildrow .= '<td>' . number_format($single["ele"],0) . '</td>';
$buildrow .= '</tr>';
echo $buildrow;
$prevLat = $single["@attributes"]["lat"];
$prevLon = $single["@attributes"]["lon"];
}
echo '</tbody></table>';
echo '<p>Total Distance: ' . number_format($distance,2) . 'mi</p>';
?>
</div>
</body>
</html>