You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gis/inc/kml_infos.php

216 lines
6.7 KiB
PHTML

<?php
/**
* Plugin GIS
* Récupération de données dans les fichiers kml permettant de :
* -* récupérer latitude et longitude d'un point correspondant centré sur la moyenne des points ou polygones du kml
* -* récupérer un titre
* -* récupérer un descriptif
*/
8 years ago
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
8 years ago
function inc_kml_infos($id_document) {
if (!intval($id_document)) {
return false;
8 years ago
}
include_spip('inc/documents');
8 years ago
$document = sql_fetsel('*', 'spip_documents', 'id_document=' . intval($id_document));
$chemin = $document['fichier'];
$chemin = get_spip_doc($chemin);
$extension = $document['extension'];
8 years ago
if (in_array($extension, ['kml','kmz'])) {
$supprimer_chemin = false;
/**
* Si on est dans un kmz (kml + autres fichiers compressés en zip),
* On dézip pour trouver le kml
*/
8 years ago
if ($extension == 'kmz') {
if (include_spip('inc/archives') and class_exists('Spip\Archiver\SpipArchiver')) {
$zip = new Spip\Archiver\SpipArchiver($chemin, 'zip');
$infos = $zip->informer();
foreach ($infos['fichiers'] as $info_fichier) {
if (substr(basename($info_fichier['filename']), -3) == 'kml') {
$zip->deballer(_DIR_TMP, [$info_fichier['filename']]);
$chemin = _DIR_TMP . $info_fichier['filename'];
$supprimer_chemin = true;
break;
}
}
}
}
include_spip('inc/xml');
$donnees = '';
lire_fichier($chemin, $donnees);
$arbre = spip_xml_parse($donnees);
$documents = $infos = [];
$placemarks = null;
8 years ago
spip_xml_match_nodes(',^Document,', $arbre, $documents);
foreach ($documents as $document => $info) {
$infos['titre'] = $info[0]['name'][0];
$infos['descriptif'] = $info[0]['description'][0];
$infos['longitude'] = $info[0]['LookAt'][0]['longitude'][0] ?: false;
$infos['latitude'] = $info[0]['LookAt'][0]['latitude'][0] ?: false;
}
8 years ago
/**
8 years ago
* Si on n'a pas de longitude ou de latitude,
* on essaie de faire une moyenne des placemarks
*/
8 years ago
if (!$infos['longitude'] or !$infos['latitude']) {
$placemarks = [];
8 years ago
spip_xml_match_nodes(',^Placemark,', $arbre, $placemarks);
$latitude = 0;
$longitude = 0;
$compte = 0;
8 years ago
foreach ($placemarks as $places) {
foreach ($places as $lieu) {
8 years ago
if ($compte > 500) {
break;
}
if ($lieu['LookAt'][0]['longitude'][0] && $latitude + $lieu['LookAt'][0]['latitude'][0]) {
if ($compte > 500) {
break;
}
$latitude = $latitude + $lieu['LookAt'][0]['latitude'][0];
$longitude = $longitude + $lieu['LookAt'][0]['longitude'][0];
$compte++;
8 years ago
} elseif ($lieu['Point'][0]['coordinates'][0]) {
if ($compte > 500) {
break;
8 years ago
}
$coordinates = explode(',', $lieu['Point'][0]['coordinates'][0]);
$latitude = $latitude + trim($coordinates[1]);
$longitude = $longitude + trim($coordinates[0]);
$compte++;
8 years ago
} elseif ($lieu['Polygon'][0]['outerBoundaryIs'][0]['LinearRing'][0]['coordinates'][0]) {
if ($compte > 500) {
break;
8 years ago
}
$coordinates = explode(' ', trim($lieu['Polygon'][0]['outerBoundaryIs'][0]['LinearRing'][0]['coordinates'][0]));
foreach ($coordinates as $coordinate) {
if ($compte > 500) {
break;
8 years ago
}
$coordinate = explode(',', $coordinate);
$latitude = $latitude + trim($coordinate[1]);
$longitude = $longitude + trim($coordinate[0]);
$compte++;
}
}
}
}
8 years ago
if (($latitude != 0) and ($longitude != 0)) {
$infos['latitude'] = $latitude / $compte;
8 years ago
$infos['longitude'] = $longitude / $compte;
}
}
8 years ago
/**
* Si pas de titre ou si le titre est égal au nom de fichier ou contient kml ou kmz :
* -* on regarde s'il n'y a qu'un seul Folder et on récupère son nom;
* -* on regarde s'il n'y a qu'un seul Placemark et on récupère son nom;
*/
8 years ago
if (!$infos['titre'] or ($infos['titre'] == basename($chemin)) or (preg_match(',\.km.,', $infos['titre']) > 0)) {
$folders = [];
8 years ago
spip_xml_match_nodes(',^Folder,', $arbre, $folders);
if ((is_countable($folders['Folder']) ? count($folders['Folder']) : 0) == 1) {
foreach ($folders['Folder'] as $dossier) {
8 years ago
if ($dossier['name'][0]) {
$infos['titre'] = $dossier['name'][0];
8 years ago
}
if (!$infos['descriptif'] && $dossier['description'][0]) {
$infos['descriptif'] = $dossier['description'][0];
8 years ago
}
}
8 years ago
} else {
if (!is_array($placemarks)) {
spip_xml_match_nodes(',^Placemark,', $arbre, $placemarks);
}
if (count((array) $placemarks) == 1) {
8 years ago
foreach ($placemarks as $places) {
if ((is_countable($places) ? count($places) : 0) == 1) {
foreach ($places as $lieu) {
8 years ago
if ($lieu['name'][0]) {
$infos['titre'] = $lieu['name'][0];
8 years ago
}
if (!$infos['descriptif'] && $lieu['description'][0]) {
$infos['descriptif'] = $lieu['description'][0];
8 years ago
}
}
}
}
}
}
}
} elseif (in_array($extension, ['gpx'])) {
$infos = [];
$supprimer_chemin = false;
include_spip('inc/xml');
$donnees = '';
lire_fichier($chemin, $donnees);
$arbre = spip_xml_parse($donnees);
$metadatas = [];
8 years ago
spip_xml_match_nodes(',^metadata,', $arbre, $metadatas);
foreach ($metadatas as $info) {
$infos['titre'] = $info[0]['name'][0];
//$infos['date'] = $info[0]['time'][0];
$infos['descriptif'] = $info[0]['description'][0];
8 years ago
foreach ($info[0] as $meta => $data) {
if (preg_match(',^bounds ,', $meta)) {
$meta = '<' . $meta . '>';
8 years ago
$maxlat = extraire_attribut($meta, 'maxlat');
$minlat = extraire_attribut($meta, 'minlat');
$maxlon = extraire_attribut($meta, 'maxlon');
$minlon = extraire_attribut($meta, 'minlon');
if ($maxlat && $minlat) {
$infos['latitude'] = (($maxlat + $minlat) / 2);
8 years ago
}
if ($maxlon && $minlon) {
$infos['longitude'] = (($maxlon + $minlon) / 2);
8 years ago
}
}
}
}
/**
8 years ago
* Si on n'a pas de longitude ou de latitude,
* on essaie de faire une moyenne des placemarks
*/
8 years ago
if (!$infos['longitude'] or !$infos['latitude']) {
$trackpoints = [];
8 years ago
spip_xml_match_nodes(',^trkpt,', $arbre, $trackpoints);
$latitude = 0;
$longitude = 0;
$compte = 0;
8 years ago
foreach ($trackpoints as $places => $place) {
foreach ($place as $lieu) {
8 years ago
if ($compte > 10) {
break;
}
}
}
8 years ago
if (($latitude != 0) && ($longitude != 0)) {
$infos['latitude'] = $latitude / $compte;
8 years ago
$infos['longitude'] = $longitude / $compte;
}
}
8 years ago
} else {
return false;
8 years ago
}
if (isset($infos['titre'])) {
$infos['titre'] = preg_replace('/<!\[cdata\[(.*?)\]\]>/is', '$1', $infos['titre']);
}
if (isset($infos['descriptif'])) {
$infos['descriptif'] = preg_replace('/<!\[cdata\[(.*?)\]\]>/is', '$1', $infos['descriptif']);
8 years ago
}
if ($supprimer_chemin) {
supprimer_fichier($chemin);
}
8 years ago
return $infos;
}