jpg2gpx.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Documentation, License etc.
  4. *
  5. * @package jpg2gpx
  6. */
  7. //exiftool -c "%.6f" /mnt/perso/Nextcloud/Chine_2019/chutes_hu_kou/IMG_20190920_*.jpg | grep GPS | grep Position
  8. setlocale(LC_ALL, 'fr_FR');
  9. CONST EOL = "\n";
  10. $dir="/mnt/perso/Nextcloud";
  11. $list = array();
  12. dirrecurse( $dir );
  13. //$dir = new RecursiveDirectoryIterator($path);
  14. //$files = new RecursiveIteratorIterator($dir);
  15. //echo "[$path]\n";
  16. //foreach ($files as $file) {
  17. // echo " ├ $file\n";
  18. //}
  19. function dirrecurse( $dir )
  20. {
  21. global $list;
  22. $x =0;
  23. $paths = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ));
  24. foreach ($paths as $file)
  25. {
  26. $x +=1;
  27. $gpscoord=readgps($x, $file);
  28. }
  29. exportGPX($list);
  30. }
  31. function readgps( $x, $path )
  32. {
  33. global $list;
  34. $flag = false;
  35. //echo $path .EOL;
  36. if (exif_imagetype($path) == 2)
  37. {
  38. $datas = exif_read_data($path, "ANY_TAG");
  39. //echo "$path\n";
  40. if (! empty($datas["GPSLatitude"]))
  41. {
  42. $list[$x]["name"] = $path;
  43. print_r( $datas );
  44. $latitude = convert2decimal( $datas["GPSLatitude"][0], $datas["GPSLatitude"][1],$datas["GPSLatitude"][2] );
  45. $dlatitude = $datas["GPSLatitudeRef"];
  46. $longitude = convert2decimal( $datas["GPSLongitude"][0], $datas["GPSLongitude"][1],$datas["GPSLongitude"][2] );
  47. $dlongitude = $datas["GPSLongitudeRef"];
  48. if ($dlongitude == "S")
  49. {
  50. $list[$x]["longitude"] = -$longitude;
  51. }else
  52. {
  53. $list[$x]["longitude"] = $longitude;
  54. }
  55. if ($dlatitude == "O")
  56. {
  57. $list[$x]["latitude"] = -$latitude;
  58. }else
  59. {
  60. $list[$x]["latitude"] = $latitude;
  61. }
  62. if ( ! empty($datas["GPSAltitude"]) )
  63. {
  64. $altitude = str_replace( ",", "", number_format( transform( $datas["GPSAltitude"] ),2 ));
  65. $list[$x]["altitude"] = $altitude;
  66. }else
  67. {
  68. $list[$x]["altitude"] = "";
  69. }
  70. $time = gpstime( $datas["GPSTimeStamp"] );
  71. //$datetime = new DateTime();
  72. $date = $datas["GPSDateStamp"] . " " . $time;
  73. $datetime = date_create_from_format("Y:m:d H:i:s", $date, new DateTimeZone( "UTC" ) );
  74. $list[$x]["datetimeUTC"] = date( "c", $datetime->getTimestamp());
  75. //echo $date . EOL;
  76. if ( ! empty($datas["DateTime"]) )
  77. {
  78. $datetime_local = date_create_from_format("Y:m:d H:i:s", $datas["DateTime"]);
  79. }elseif ( ! empty($datas["DateTimeOriginal"]) )
  80. {
  81. $datetime_local = date_create_from_format("Y:m:d H:i:s", $datas["DateTimeOriginal"]);
  82. }else
  83. {
  84. $flag = true;
  85. }
  86. if ( $flag = false )
  87. {
  88. $list[$x]["datetimelocal"] = date( "c", $datetime_local->getTimestamp());
  89. $dh = $datetime->diff( $datetime_local );
  90. $decalagehoraire = $dh->h;
  91. if ( $dh->i >55)
  92. {
  93. $decalagehoraire += 1;
  94. }
  95. if ( $decalagehoraire >=0 )
  96. {
  97. $decalagehoraire = "+" . str_pad(abs($decalagehoraire), 4, "0", STR_PAD_BOTH );
  98. }else
  99. {
  100. $decalagehoraire = "-" . str_pad(abs($decalagehoraire), 4, "0", STR_PAD_BOTH );
  101. }
  102. $list[$x]["fuseauhoraire"] = $decalagehoraire;
  103. }else
  104. {
  105. $decalagehoraire = "";
  106. }
  107. $datetime->setTimeZone(new DateTimeZone('Europe/Paris'));
  108. echo $path . EOL;
  109. echo strftime( "le %A %c heure de Paris", $datetime->getTimestamp() ) . EOL;
  110. echo strftime( "le %A %c heure locale", $datetime_local->getTimestamp() ) . EOL;
  111. echo "Fuseau horaire : " . $decalagehoraire .EOL;
  112. echo "Latitude : $latitude $dlatitude\nLongitude : $longitude $dlongitude\nAltitude : $altitude\n";
  113. //print_r($datas);
  114. }
  115. }
  116. }
  117. function convert2decimal( $d, $m, $s)
  118. {
  119. return transform( $d ) + transform($m)/60 + transform($s)/3600;
  120. }
  121. function transform($a)
  122. {
  123. $b = explode( "/", $a);
  124. $r = floatval($b[0]) / floatval($b[1]);
  125. return $r;
  126. }
  127. function gpstime( $a )
  128. {
  129. $h = explode("/", $a[0]);
  130. $m = explode("/", $a[1]);
  131. $s = explode("/", $a[2]);
  132. return str_pad( $h[0], 2 ,"0", STR_PAD_LEFT ) . ":" . str_pad( $m[0], 2 ,"0", STR_PAD_LEFT ) . ":" . str_pad( $s[0], 2 ,"0", STR_PAD_LEFT );
  133. }
  134. function exportKML( $list )
  135. {
  136. $enteteKML = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2">' . EOL;
  137. print_r($list);
  138. if ( ! $fh = fopen("export_jpg2gpx_" . date("YmdHis") . ".kml", "w") )
  139. {
  140. $erreur = error_get_last();
  141. echo "Erreur fopen ligne " . __LINE__ . " : " . $erreur["message"] . EOL;
  142. exit (1);
  143. }
  144. fwrite( $fh, $enteteKML );
  145. foreach ( $list as $ligne )
  146. {
  147. $text = "<Placemark>" . EOL . "<name>" . basename($ligne["name"]) . "</name>" . EOL . "<description>" . $ligne["name"] . EOL . "Altitude : " . $ligne["altitude"] . EOL . "Heure UTC : " . $ligne["datetimeUTC"] . EOL . "Heure locale :" . $ligne["datetimelocal"] . EOL . "Fuseau horaire : " . $ligne["fuseauhoraire"] . EOL . "Généré automatiquement</description>" .EOL;
  148. $text .= " <point>" . EOL . " <coordinates>" . $ligne["longitude"] . "," . $ligne["latitude"] . "</coordinates>" . EOL . " </point>" . EOL . " </Placemark>" . EOL;
  149. fwrite( $fh, $text );
  150. }
  151. fwrite( $fh, "</kml>" );
  152. }
  153. function exportGPX( $list )
  154. {
  155. $enteteXML = '<?xml version="1.1" encoding="UTF-8" standalone="no" ?><gpx version="1.1" creator="jpg2gpx" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"><metadata><name>export_jpg2gpx_' . date("YmdHis") . '</name></metadata>' . EOL;
  156. if ( ! $fh = fopen("export_jpg2gpx_" . date("YmdHis") . ".gpx", "w") )
  157. {
  158. $erreur = error_get_last();
  159. echo "Erreur fopen ligne " . __LINE__ . " : " . $erreur["message"] . EOL;
  160. exit (1);
  161. }
  162. fwrite( $fh, $enteteXML );
  163. foreach ( $list as $ligne )
  164. {
  165. $text = '<wpt lat="' . str_replace( ",", ".", $ligne["latitude"]) . '" lon="' . str_replace( ",", ".", $ligne["longitude"]) . '">';
  166. $text .= "<ele>" . $ligne["altitude"] . "</ele>";
  167. $text .= "<time>" . $ligne["datetimeUTC"] . "</time>" . EOL;
  168. $text .= "<name>" . basename($ligne["name"]) . "</name>" . EOL;
  169. $text .= "<cmt>Généré automatiquement</cmt>" . EOL;
  170. $text .= "<desc>" . $ligne["name"] . EOL;
  171. if ( ! empty($ligne["datetimelocal"]) )
  172. {
  173. $text .= " Heure locale :" . $ligne["datetimelocal"];
  174. }
  175. $text .= "</desc>" . EOL;
  176. $text .= "</wpt>" .EOL;
  177. fwrite( $fh, $text );
  178. }
  179. fwrite( $fh, "</gpx>" );
  180. }
  181. function remplacevirgule ( $f )
  182. {
  183. }