[PLUGINS] +clavettes et dependances
[lhc/web/clavette_www.git] / www / plugins / gis / inc / kml_infos.php
1 <?php
2 /**
3 * Plugin GIS
4 * Récupération de données dans les fichiers kml permettant de :
5 * -* récupérer latitude et longitude d'un point correspondant centré sur la moyenne des points ou polygones du kml
6 * -* récupérer un titre
7 * -* récupérer un descriptif
8 */
9 if (!defined("_ECRIRE_INC_VERSION")) return;
10
11 function inc_kml_infos($id_document){
12 if(!intval($id_document))
13 return false;
14 include_spip('inc/documents');
15 $document = sql_fetsel("*", "spip_documents","id_document=".intval($id_document));
16 $chemin = $document['fichier'];
17 $chemin = get_spip_doc($chemin);
18 $extension = $document['extension'];
19
20 if(in_array($extension,array('kml','kmz'))){
21 $supprimer_chemin = false;
22 /**
23 * Si on est dans un kmz (kml + autres fichiers compressés en zip),
24 * On dézip pour trouver le kml
25 */
26 if($extension == 'kmz'){
27 include_spip('inc/pclzip');
28 $zip = new PclZip($chemin);
29 $list = $zip->listContent();
30 foreach($list as $fichier => $info_fichier){
31 if(substr(basename($info_fichier['filename']),-3) == 'kml'){
32 $zip->extractByIndex($info_fichier['index'],_DIR_TMP);
33 $chemin = _DIR_TMP.$info_fichier['filename'];
34 $supprimer_chemin = true;
35 break;
36 }
37 }
38 }
39 include_spip('inc/xml');
40 $ret = lire_fichier($chemin,$donnees);
41 $arbre = spip_xml_parse($donnees);
42 spip_xml_match_nodes(",^Document,",$arbre, $documents);
43 foreach($documents as $document => $info){
44 $infos['titre'] = $info[0]['name'][0];
45 $infos['descriptif'] = $info[0]['description'][0];
46 $infos['longitude'] = $info[0]['LookAt'][0]['longitude'][0] ? $info[0]['LookAt'][0]['longitude'][0] : false;
47 $infos['latitude'] = $info[0]['LookAt'][0]['latitude'][0] ? $info[0]['LookAt'][0]['latitude'][0] : false;
48 }
49
50 /**
51 * Si on n'a pas de longitude ou de latitude,
52 * on essaie de faire une moyenne des placemarks
53 */
54 if(!$infos['longitude'] OR !$infos['latitude']){
55 spip_xml_match_nodes(",^Placemark,",$arbre, $placemarks);
56 $latitude = 0;
57 $longitude = 0;
58 $compte = 0;
59 foreach($placemarks as $places){
60 foreach($places as $placemark => $lieu){
61 if($compte > 500)
62 break;
63 if($lieu['LookAt'][0]['longitude'][0] && $latitude + $lieu['LookAt'][0]['latitude'][0]){
64 if($compte > 500)
65 break;
66 $latitude = $latitude + $lieu['LookAt'][0]['latitude'][0];
67 $longitude = $longitude + $lieu['LookAt'][0]['longitude'][0];
68 $compte++;
69 }else if($lieu['Point'][0]['coordinates'][0]){
70 if($compte > 500)
71 break;
72 $coordinates = explode(',',$lieu['Point'][0]['coordinates'][0]);
73 $latitude = $latitude + trim($coordinates[1]);
74 $longitude = $longitude + trim($coordinates[0]);
75 $compte++;
76 }else if($lieu['Polygon'][0]['outerBoundaryIs'][0]['LinearRing'][0]['coordinates'][0]){
77 if($compte > 500)
78 break;
79 $coordinates = explode(' ',trim($lieu['Polygon'][0]['outerBoundaryIs'][0]['LinearRing'][0]['coordinates'][0]));
80 foreach($coordinates as $coordinate){
81 if($compte > 500)
82 break;
83 $coordinate = explode(',',$coordinate);
84 $latitude = $latitude + trim($coordinate[1]);
85 $longitude = $longitude + trim($coordinate[0]);
86 $compte++;
87 }
88 }
89 }
90 }
91 if(($latitude != 0) && ($longitude != 0)){
92 $infos['latitude'] = $latitude / $compte;
93 $infos['longitude'] = $longitude / $compte;
94 }
95 }
96
97 /**
98 * Si pas de titre ou si le titre est égal au nom de fichier ou contient kml ou kmz :
99 * -* on regarde s'il n'y a qu'un seul Folder et on récupère son nom;
100 * -* on regarde s'il n'y a qu'un seul Placemark et on récupère son nom;
101 */
102 if(!$infos['titre'] OR ($infos['titre'] == basename($chemin)) OR (preg_match(',\.km.,',$infos['titre']) > 0)){
103 spip_xml_match_nodes(",^Folder,",$arbre, $folders);
104 if(count($folders['Folder']) == 1){
105 foreach($folders['Folder'] as $folder => $dossier){
106 if($dossier['name'][0])
107 $infos['titre'] = $dossier['name'][0];
108 if(!$infos['descriptif'] && $dossier['description'][0])
109 $infos['descriptif'] = $dossier['description'][0];
110 }
111 }else{
112 if(!is_array($placemarks)){
113 spip_xml_match_nodes(",^Placemark,",$arbre, $placemarks);
114 }
115 if(count($placemarks) == 1){
116 foreach($placemarks as $places){
117 if(count($places) == 1){
118 foreach($places as $placemark => $lieu){
119 if($lieu['name'][0])
120 $infos['titre'] = $lieu['name'][0];
121 if(!$infos['descriptif'] && $lieu['description'][0])
122 $infos['descriptif'] = $lieu['description'][0];
123 }
124 }
125 }
126 }
127 }
128 }
129 }else if(in_array($extension,array('gpx'))){
130 $supprimer_chemin = false;
131 include_spip('inc/xml');
132 $ret = lire_fichier($chemin,$donnees);
133 $arbre = spip_xml_parse($donnees);
134 spip_xml_match_nodes(",^metadata,",$arbre, $metadatas);
135 foreach($metadatas as $metadata => $info){
136 $infos['titre'] = $info[0]['name'][0];
137 //$infos['date'] = $info[0]['time'][0];
138 $infos['descriptif'] = $info[0]['description'][0];
139 foreach($info[0] as $meta => $data){
140 if(preg_match(',^bounds ,',$meta)){
141 $meta = '<'.$meta.'>';
142 $maxlat = extraire_attribut($meta,'maxlat');
143 $minlat = extraire_attribut($meta,'minlat');
144 $maxlon = extraire_attribut($meta,'maxlon');
145 $minlon = extraire_attribut($meta,'minlon');
146 if($maxlat && $minlat)
147 $infos['latitude'] = (($maxlat+$minlat)/2);
148 if($maxlon && $minlon)
149 $infos['longitude'] = (($maxlon+$minlon)/2);
150 }
151 }
152 }
153 /**
154 * Si on n'a pas de longitude ou de latitude,
155 * on essaie de faire une moyenne des placemarks
156 */
157 if(!$infos['longitude'] OR !$infos['latitude']){
158 spip_xml_match_nodes(",^trkpt,",$arbre, $trackpoints);
159 $latitude = 0;
160 $longitude = 0;
161 $compte = 0;
162 foreach($trackpoints as $places => $place){
163 foreach($place as $placemark => $lieu){
164 if($compte > 10)
165 break;
166 }
167 }
168 if(($latitude != 0) && ($longitude != 0)){
169 $infos['latitude'] = $latitude / $compte;
170 $infos['longitude'] = $longitude / $compte;
171 }
172 }
173 }else
174 return false;
175
176 if(isset($infos['titre']))
177 $infos['titre'] = preg_replace('/<!\[cdata\[(.*?)\]\]>/is', '$1',$infos['titre']);
178 if(isset($infos['descriptif']))
179 $infos['descriptif'] = preg_replace('/<!\[cdata\[(.*?)\]\]>/is', '$1', $infos['descriptif']);
180
181 if($supprimer_chemin){
182 supprimer_fichier($chemin);
183 }
184
185 return $infos;
186 }
187 ?>