Created
April 15, 2014 06:17
-
-
Save kdavidjames/10706677 to your computer and use it in GitHub Desktop.
Strip Heart Rate and Cadence Data From A Garmin .GPX File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Check that we have a file | |
if((!empty($_FILES["file"])) && ($_FILES['file']['error'] == 0)) | |
{ | |
// Set some variables | |
$inputFile = $_FILES['file']['tmp_name']; | |
$outputFile = 'cleaned_'.$_FILES['file']['name']; | |
$nodesToStrip = $_POST['elements']; // array of child nodes to strip (hr|cad) | |
// Strip the data! You don't want your competitors knowing how much POWAH you're capable of | |
// or how great (or tired) your superhuman blood pumping device is. Keep it to yourself; Secrecy is KEY! | |
$strippedXML = cleanDOM($inputFile, $outputFile, $nodesToStrip); | |
// Serve the stripped XML data to the user | |
if($strippedXML){ | |
header('Content-type: text/xml'); | |
header('Content-Disposition: attachment; filename="'.$outputFile.'"'); | |
echo $strippedXML; | |
exit(); | |
} | |
} | |
/** | |
* cleanDOM load a GPX/XML file and remove specified child nodes. | |
* | |
* @param string $inputFile specify input file with XML data to be cleaned. | |
* @param string $outputFile specify the output file name (don't forget the file extension!) | |
* @param array $nodesToStrip specify which child nodes to remove from the XML data. | |
* @param bool $returnDOM determines what is returned once the function has been run. TRUE will return the stripped DOM, FALSE will save the file to disk. | |
* @param string $nameSpace specify the XML namespace to use. Default for Garmin Track Point Extension: http://www.garmin.com/xmlschemas/TrackPointExtension/v1 | |
* | |
* @todo strip power data! | |
*/ | |
function cleanDOM($inputFile, $outputFile, $nodesToStrip, $returnDOM = true, $nameSpace = 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1') | |
{ | |
if(!is_array($nodesToStrip)){ | |
return false; // We need an array, we don't have one | |
} | |
$dom = new DOMDocument; | |
$dom->preserveWhiteSpace = false; | |
$dom->formatOutput = true; | |
$dom->load($inputFile); | |
foreach($nodesToStrip as $nodeToStrip) | |
{ | |
$domNodeList = $dom->getElementsByTagNameNS($nameSpace, $nodeToStrip // Find the node | |
// Compile an array of nodes to remove | |
$domElemsToRemove = array(); | |
foreach($domNodeList as $domElement) | |
{ | |
// echo "{$elementTostrip}: {$domElement->nodeValue} <br />\n"; // Crazy nested XML can be hard. Show me da node, yo! | |
$domElemsToRemove[] = $domElement; | |
} | |
// Remove the node(s)! | |
foreach($domElemsToRemove as $domElement) | |
{ | |
$domElement->parentNode->removeChild($domElement); // Buh-Bye! | |
} | |
} | |
return ($returnDOM === false) ? $dom->save($outputFile) : $dom->saveXML(); // Save file to disk or return DOM | |
} | |
?> | |
<html> | |
<head> | |
<title>Strip Ride Metrics</title> | |
</head> | |
<body> | |
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> | |
<p><input type="file" name="file" /></p> | |
<p> | |
Strip your ride of the following metrics: | |
<ul> | |
<li><label><input type="checkbox" name="elements[]" value="hr">Heart Rate</label></li> | |
<li><label><input type="checkbox" name="elements[]" value="cad">Cadence</label></li> | |
<li><label><input type="checkbox" name="elements[]" value="watts" disabled="disabled">Power</label></li> | |
</ul> | |
</p> | |
<p><input type="submit" value="Strip!" /></p> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 50, missing ); at the end :)