PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/gis/trunk/inc/kml_infos.php

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