function kml2igc($filename) { $l = file($filename); for ($i = 0; $i < count($l); $i++) { $l[$i] = trim($l[$i]); } $lines = implode(" ", $l); //echo $lines; //exit; if (!preg_match("/<SecondsFromTimeOfFirstPoint>(.*)<\\/SecondsFromTimeOfFirstPoint>/", $lines, $matches)) { return 0; } // echo "match<BR>"; // echo $matches[1]; $tm = explode(" ", trim($matches[1])); // echo count($tm).'<br>'; // now search for this // <FsInfo time_of_first_point="2007-04-05T09:51:50Z" // hash="2E361EB937B9D7D6C92855F38D37B399B17292AD"> // if( ! preg_match("/<FsInfo *time_of_first_point=\"([^\"]+)\" *hash=\"(.*)\">/i",$lines,$matches) ) { // return 0; // } /* this is the new format from kml <FsInfo time_of_first_point="2007-04-22T13:42:06Z" civl_pilot_id="4" comp_pilot_id="4" instrument="GPS 72 Software Version 2.30" downloaded="2007-04-29T12:39:49Z" hash="80F1FAC5CFFE4B97E202371EF6303BFCF83A6D78"> I'm a zero at the left in regular expressions, then i change a litle to this one... */ if (!preg_match("/<FsInfo.*time_of_first_point=\"([^\"]+)\".*hash=\"([^\"]+)\">/i", $lines, $matches)) { return 0; } //echo "match start time<BR>"; // echo $matches[1]; $start_time_str = trim($matches[1]); $hash_str = trim($matches[2]); // echo "$start_time_str # $hash_str<BR>"; // now search for this /* <name>Tracklog</name> <LineString> <altitudeMode>absolute</altitudeMode> <coordinates> */ if (!preg_match("/<name>Tracklog<\\/name> *<LineString> *<altitudeMode>absolute<\\/altitudeMode> *<coordinates>(.*)<\\/coordinates>/", $lines, $matches)) { return 0; } //echo "match coordinates<BR>"; // echo $matches[1]; $coords = explode(" ", trim($matches[1])); //echo count($coords); // print_r($coords); require_once dirname(__FILE__) . '/CL_gpsPoint.php'; // now make the igc file for ($i = 0; $i < count($coords); $i++) { if ($i == 0) { // igc date-> ddmmyy // 01234567890123456789 // kml date -> "2007-04-05T09:51:50Z" $dateStr = substr($start_time_str, 8, 2) . substr($start_time_str, 5, 2) . substr($start_time_str, 2, 2); // echo substr($start_time_str,11,2)." ".substr($start_time_str,14,2)." ".substr($start_time_str,17,2); $startTimeSecs = substr($start_time_str, 11, 2) * 3600 + substr($start_time_str, 14, 2) * 60 + substr($start_time_str, 17, 2); $igc .= "HFDTE{$dateStr}\r\n" . "HFSITSite:\r\n" . "HPGTYGliderType:\r\n" . "HPGIDGliderID:\r\n" . "HFDTM100DATUM:WGS-1984\r\n" . "HFCIDCOMPETITIONID:\r\n" . "HFCCLCOMPETITIONCLASS:\r\n" . "HFFXA100\r\n" . "HFRHWHARDWAREVERSION:1.00\r\n" . "HFFTYFRTYPE:Convert from GPSDump KML\r\n"; } // echo $coords[$i].'<BR>'; $thisPoint = new gpsPoint(); list($lon, $lat, $alt) = explode(",", $coords[$i]); $thisPoint->lat = $lat + 0; $thisPoint->lon = -$lon + 0; $thisPoint->gpsAlt = $alt + 0; $thisPoint->gpsTime = ($startTimeSecs + $tm[$i]) % (3600 * 24); $igc .= $thisPoint->to_IGC_Record() . "\r\n"; } //echo $igc; // write the igc to disk // $igcfilename=substr($filename,0,-3).'igc'; // overwrite the original $igcfilename = $filename; if (!($handle = fopen($igcfilename, 'w'))) { print "Cannot open file ({$igcfilename})"; return 0; } // Write $somecontent to our opened file. if (!fwrite($handle, $igc)) { print "Cannot write to file ({$igcfilename})"; return 0; } fclose($handle); return 1; }
function fromSyncArray($sArr) { /* echo "<pre>"; print_r($flightScore->scores); echo "</pre>"; */ /* Array ( [0] => Array ( [XCtype] => FreeFlight3TP [XCdistance] => 36.482 [turnpoints] => Array( [0] => Array([id] => 1,[lat] => 36.761466,[lon] => -119.09663,[UTCsecs] => 70730 ) [1] => Array([id] =>2 ,[lat] => 36.7794333, [lon] => -119.117866,[UTCsecs] => 71966) */ global $CONF; $this->scores = array(); foreach ($sArr as $i => $score) { //$mID=$score['XCscoreMethod']; $type = $this->flightTypesID[$this->syncTypesNames[$score['XCtype']]]; //$bestScore=0; //$bestScoreType=''; foreach ($CONF['scoring']['sets'] as $mID => $mArray) { $thisScore = $score['XCdistance'] * $mArray['types'][$type]; if ($thisScore >= $this->scores[$mID]['bestScore']) { $this->scores[$mID]['bestScoreType'] = $type; $this->scores[$mID]['bestScore'] = $thisScore; $this->scores[$mID]['bestDistance'] = $score['XCdistance']; } $this->scores[$mID][$type]['distance'] = $score['XCdistance']; $this->scores[$mID][$type]['score'] = $thisScore; $this->scores[$mID][$type]['isBest'] = 0; // will update later $tpNum = 1; foreach ($score['turnpoints'] as $j => $tp) { $thisTP = new gpsPoint(); $thisTP->setLat($tp['lat']); $thisTP->setLon($tp['lon']); $thisTP->gpsTime = $tp['UTCsecs']; $this->scores[$mID][$type]['tp'][$tpNum] = $thisTP->to_IGC_Record(); $tpNum++; } } } // now iterate through $this->scores and update best values foreach ($this->scores as $mID => $score) { $bestType = $this->scores[$mID]['bestScoreType']; $this->scores[$mID][$bestType]['isBest'] = 1; } $defaultMethodID = $CONF['scoring']['default_set']; $defaultScore = $this->scores[$defaultMethodID]; $this->bestScoreType = $this->scores[$defaultMethodID]['bestScoreType']; $this->bestScore = $this->scores[$defaultMethodID]['bestScore']; $this->bestDistance = $this->scores[$defaultMethodID]['bestDistance']; //echo "<pre>"; //print_r($this->scores); //echo "</pre>"; $this->gotValues = 1; }