wikipoints/classes/Kml.php

109 lines
2.3 KiB
PHP

<?php
class Kml
{
private $kml = NULL;
private $waypoints = array();
private $tracks = array();
public function __construct($path)
{
$this->kml = new DOMDocument;
$this->kml->loadXML(file_get_contents($path));
foreach ($this->get($this->kml, 'Placemark') as $placemark) {
if ($this->has($placemark, 'Point')) {
$this->waypoints[] = array(
'name' => trim($this->getValue($placemark, 'name')),
'description' => trim($this->getValue($placemark, 'description')),
'when' => date('c', strtotime($this->getValue($placemark, 'when'))),
'latlng' => $this->getCoords($placemark)[0],
);
}
if ($this->has($placemark, 'LineString')) {
$this->tracks[] = array(
'name' => trim($this->getValue($placemark, 'name')),
'description' => trim($this->getValue($placemark, 'description')),
'latlng' => $this->getCoords($placemark),
);
}
};
}
public function getName()
{
return $this->getValue($this->kml, 'name') ? strval($this->getValue($this->kml, 'name')) : 'Track KML';
}
public function getDescription()
{
return $this->getValue($this->kml, 'description') ? strval($this->getValue($this->kml, 'description')) : '';
}
public function getTime()
{
return $this->getValue($this->kml, 'when') ? date('c', strtotime($this->getValue($this->kml, 'when'))) : date('c');
}
public function getWaypoints()
{
return $this->waypoints;
}
public function getTracks()
{
return $this->tracks;
}
private function has($src, $tagName)
{
return $this->get($src, $tagName)->length > 0;
}
private function get($src, $tagName)
{
return $src->getElementsByTagName($tagName);
}
private function getValue($src, $tagName)
{
foreach ($src->getElementsByTagName($tagName) AS $value)
{
return $value->nodeValue;
}
}
private function getCoords($src)
{
foreach ($src->getElementsByTagName('coordinates') AS $value)
{
$result = array();
$coords = $value->nodeValue;
$coords = str_replace(array(" ", "\n", "\r", "\t"), ' ', $coords);
// multi-space
$pr = '(\s{2,})';
$rep = ' ';
$coords = preg_replace($pr, $rep, $coords);
$coords = explode(' ', trim($coords));
foreach ($coords as $coord) {
$tmp = explode(',', $coord);
$result[] = array(
'lat' => $tmp[1],
'lng' => $tmp[0],
'alt' => $tmp[2],
);
}
return $result;
}
return array();
}
}