forked from robho/php-igc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMLReader.php
More file actions
105 lines (100 loc) · 2.81 KB
/
KMLReader.php
File metadata and controls
105 lines (100 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
require_once(dirname(__FILE__).'/ITrackfileReader.php');
/**
* Reads KML file
*/
class KMLReader implements ITrackfileReader
{
private $fhandle;
public $duration;
/**
* Class constructor creates the KMLReader object from a file path.
*
* @param string $file_path usually this will be the request vars
*/
public function __construct($file_path)
{
if (@is_file($file_path)) {
$this->fhandle = @simplexml_load_file($file_path);
} else {
$this->fhandle = @simplexml_load_string($file_path);
}
}
/**
* Returns the point list
*/
public function getRecords($first = false)
{
$pt_records = array();
$xml = null;
$prefixns = "";
if ($this->fhandle) {
$namespaces = $this->fhandle->getNamespaces(true);
if(isset($namespaces[""])) // if you have a default namespace
{
// register a prefix for that default namespace:
$this->fhandle->registerXPathNamespace("default", $namespaces[""]);
$prefixns = "default:";
// and use that prefix in all of your xpath expressions:
$xpath_to_document = "//".$prefixns."Document";
}
else
$xpath_to_document = "//Document";
$xml = $this->fhandle->xpath($xpath_to_document);
if (is_array($xml))
$xml = $xml[0];
if(strlen($prefixns)>0)
$xml->registerXPathNamespace("default", $namespaces[""]);
}
if ($xml) {
foreach ($namespaces as $ns => $nsurl)
{
$nstmpprefix1 = $prefixns;
if (strlen($ns)>0)
$nstmpprefix1 = $ns.":";
foreach($xml->xpath("//".$nstmpprefix1."Track") as $trk)
{
if(strlen($prefixns)>0)
$trk->registerXPathNamespace("default", $namespaces[""]);
foreach ($namespaces as $ns2 => $nsurl2)
{
$nstmpprefix2 = $prefixns;
if (strlen($ns2)>0)
$nstmpprefix2 = $ns2.":";
foreach($trk->xpath($nstmpprefix2."when") as $pt)
$pt_records[] = (object)['date' => DateTime::createFromFormat('Y-m-d\TH:i:s+', (string) $pt, new DateTimeZone('UTC')), 'latitude' => 0.0, 'longitude' => 0.0, 'altitude' => 0.0];
$i = 0;
foreach($trk->xpath($nstmpprefix2."coord") as $pt) {
//-93.3806146339391 44.8823651507134 2743
$line = explode(' ', $pt);
if (count($line)<3)
continue;
$pt_records[$i]->latitude = floatval($line[0]);
$pt_records[$i]->longitude = floatval($line[1]);
$pt_records[$i]->altitude = floatval($line[2]);
$i++;
if ($first)
break;
}
}
}
}
}
/*echo "<pre>";
print_r($pt);
echo "</pre>";*/
$this->duration = ($pt_records[count($pt_records)-1]->date->getTimestamp()-$pt_records[0]->date->getTimestamp());
return $pt_records;
}
/**
* Returns the first point
*/
public function getFirstRecord()
{
$pt_records = $this->getRecords(true);
if (is_array($pt_records) && count($pt_records)>0)
return $pt_records[0];
return null;
}
}
?>