ADD импорт KMZ файлов и KM* файлов с gxMultiTrack
This commit is contained in:
parent
56c21af49e
commit
d23756c16e
|
|
@ -9,8 +9,11 @@ class Kml
|
||||||
|
|
||||||
public function __construct($path)
|
public function __construct($path)
|
||||||
{
|
{
|
||||||
|
$xml = substr($path, -4) == '.kmz' ? $this->getKmzContent($path) : file_get_contents($path);
|
||||||
|
$xml = str_replace(array('<gx:', '</gx:'), array('<gx', '</gx'), $xml);
|
||||||
|
|
||||||
$this->kml = new DOMDocument;
|
$this->kml = new DOMDocument;
|
||||||
$this->kml->loadXML(file_get_contents($path));
|
$this->kml->loadXML($xml);
|
||||||
|
|
||||||
foreach ($this->get($this->kml, 'Placemark') as $placemark) {
|
foreach ($this->get($this->kml, 'Placemark') as $placemark) {
|
||||||
if ($this->has($placemark, 'Point')) {
|
if ($this->has($placemark, 'Point')) {
|
||||||
|
|
@ -29,6 +32,10 @@ class Kml
|
||||||
'latlng' => $this->getCoords($placemark),
|
'latlng' => $this->getCoords($placemark),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->has($placemark, 'gxMultiTrack')) {
|
||||||
|
$this->tracks = $this->getTracksGX($placemark);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,6 +58,7 @@ class Kml
|
||||||
{
|
{
|
||||||
return $this->waypoints;
|
return $this->waypoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTracks()
|
public function getTracks()
|
||||||
{
|
{
|
||||||
return $this->tracks;
|
return $this->tracks;
|
||||||
|
|
@ -68,16 +76,14 @@ class Kml
|
||||||
|
|
||||||
private function getValue($src, $tagName)
|
private function getValue($src, $tagName)
|
||||||
{
|
{
|
||||||
foreach ($src->getElementsByTagName($tagName) AS $value)
|
foreach ($src->getElementsByTagName($tagName) AS $value) {
|
||||||
{
|
|
||||||
return $value->nodeValue;
|
return $value->nodeValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCoords($src)
|
private function getCoords($src)
|
||||||
{
|
{
|
||||||
foreach ($src->getElementsByTagName('coordinates') AS $value)
|
foreach ($src->getElementsByTagName('coordinates') AS $value) {
|
||||||
{
|
|
||||||
$result = array();
|
$result = array();
|
||||||
$coords = $value->nodeValue;
|
$coords = $value->nodeValue;
|
||||||
$coords = str_replace(array(" ", "\n", "\r", "\t"), ' ', $coords);
|
$coords = str_replace(array(" ", "\n", "\r", "\t"), ' ', $coords);
|
||||||
|
|
@ -104,5 +110,62 @@ class Kml
|
||||||
return array();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class TracksController extends Controller
|
||||||
$extension = strtolower($temp['extension']);
|
$extension = strtolower($temp['extension']);
|
||||||
$uploaddir = App::getBasedir() . 'tracks/';
|
$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)) {
|
if (!in_array(strtoupper($extension), $supportedExtensions)) {
|
||||||
App::error404('Неизвестный тип');
|
App::error404('Неизвестный тип');
|
||||||
|
|
@ -105,6 +105,7 @@ class TracksController extends Controller
|
||||||
self::importGpx($trackFile);
|
self::importGpx($trackFile);
|
||||||
break;
|
break;
|
||||||
case TrackFiles::FORMAT_KML:
|
case TrackFiles::FORMAT_KML:
|
||||||
|
case TrackFiles::FORMAT_KMZ:
|
||||||
self::importKml($trackFile);
|
self::importKml($trackFile);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
<form method="POST" action="/tracks/uploadfile/" enctype="multipart/form-data" class="form-inline">
|
<form method="POST" action="/tracks/uploadfile/" enctype="multipart/form-data" class="form-inline">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="uploadfile">Загрузить новый файл</label>
|
<label for="uploadfile">Загрузить новый файл</label>
|
||||||
<input type="file" name="file" id="uploadfile" class="form-control" accept=".gpx, .kml"/>
|
<input type="file" name="file" id="uploadfile" class="form-control" accept=".gpx, .kml, .kmz"/>
|
||||||
<button type="submit" class="btn btn-success" title="Загрузить выбранный файл"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span></button>
|
<button type="submit" class="btn btn-success" title="Загрузить выбранный файл"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span></button>
|
||||||
|
|
||||||
<a href="/tracks/files" class="btn btn-default" title="Раздел с файлами треков"><span class="glyphicon glyphicon-download" aria-hidden="true"></span></a>
|
<a href="/tracks/files" class="btn btn-default" title="Раздел с файлами треков"><span class="glyphicon glyphicon-download" aria-hidden="true"></span></a>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue