diff --git a/classes/Kml.php b/classes/Kml.php index 83e888e..83171e2 100644 --- a/classes/Kml.php +++ b/classes/Kml.php @@ -9,8 +9,11 @@ class Kml public function __construct($path) { + $xml = substr($path, -4) == '.kmz' ? $this->getKmzContent($path) : file_get_contents($path); + $xml = str_replace(array('kml = new DOMDocument; - $this->kml->loadXML(file_get_contents($path)); + $this->kml->loadXML($xml); foreach ($this->get($this->kml, 'Placemark') as $placemark) { if ($this->has($placemark, 'Point')) { @@ -29,6 +32,10 @@ class Kml 'latlng' => $this->getCoords($placemark), ); } + + if ($this->has($placemark, 'gxMultiTrack')) { + $this->tracks = $this->getTracksGX($placemark); + } }; } @@ -51,6 +58,7 @@ class Kml { return $this->waypoints; } + public function getTracks() { return $this->tracks; @@ -68,16 +76,14 @@ class Kml private function getValue($src, $tagName) { - foreach ($src->getElementsByTagName($tagName) AS $value) - { + foreach ($src->getElementsByTagName($tagName) AS $value) { return $value->nodeValue; } } private function getCoords($src) { - foreach ($src->getElementsByTagName('coordinates') AS $value) - { + foreach ($src->getElementsByTagName('coordinates') AS $value) { $result = array(); $coords = $value->nodeValue; $coords = str_replace(array(" ", "\n", "\r", "\t"), ' ', $coords); @@ -104,5 +110,62 @@ class Kml return array(); } + private function getTracksGX($placemark) + { + $result = array(); + foreach ($placemark->getElementsByTagName('gxTrack') AS $gxTrack) { + $track = array(); + foreach ($gxTrack->getElementsByTagName('gxcoord') AS $value) { + $coords = $value->nodeValue; + $coords = str_replace(array(" ", "\n", "\r", "\t"), ' ', $coords); + + // multi-space + $pr = '(\s{2,})'; + $rep = ' '; + $coords = preg_replace($pr, $rep, $coords); + + $tmp = explode(' ', trim($coords)); + + $track[] = array( + 'lat' => $tmp[1], + 'lng' => $tmp[0], + 'alt' => $tmp[2], + ); + } + + $result[] = array( + 'name' => '', + 'description' => '', + 'latlng' => $track, + ); + } + + return $result; + } + + private function getKmzContent($path) + { + $zip = zip_open($path); + if (!$zip) { + App::error404(); + } + + $zip_entry = zip_read($zip); + + if (zip_entry_name($zip_entry) != 'doc.kml') { + App::error404(); + } + + + if (zip_entry_open($zip, $zip_entry, "r")) { + $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); + zip_entry_close($zip_entry); + zip_close($zip); + + return $buf; + } + + App::error404(); + } } diff --git a/controllers/TracksController.php b/controllers/TracksController.php index 9db6286..08e0171 100644 --- a/controllers/TracksController.php +++ b/controllers/TracksController.php @@ -44,7 +44,7 @@ class TracksController extends Controller $extension = strtolower($temp['extension']); $uploaddir = App::getBasedir() . 'tracks/'; - $supportedExtensions = array(TrackFiles::FORMAT_GPX, TrackFiles::FORMAT_KML); + $supportedExtensions = array(TrackFiles::FORMAT_GPX, TrackFiles::FORMAT_KML, TrackFiles::FORMAT_KMZ); if (!in_array(strtoupper($extension), $supportedExtensions)) { App::error404('Неизвестный тип'); @@ -105,6 +105,7 @@ class TracksController extends Controller self::importGpx($trackFile); break; case TrackFiles::FORMAT_KML: + case TrackFiles::FORMAT_KMZ: self::importKml($trackFile); break; default: diff --git a/views/tracks/index.php b/views/tracks/index.php index 3bc7512..bd407cf 100644 --- a/views/tracks/index.php +++ b/views/tracks/index.php @@ -7,7 +7,7 @@
- +