src/App/Utils/Gpx.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use phpGPX\phpGPX;
  4. // VOIR DOC SUR phpGPX EN FIN DE CE FICHIER
  5. class Gpx
  6. {
  7. private string $filename;
  8. private $phpGpxfile;
  9. public function __construct($filename)
  10. {
  11. $this->filename = $filename;
  12. $this->phpGpxfile = (new phpGPX)->load($filename);
  13. }
  14. public function getPhpGpxfile()
  15. {
  16. return $this->phpGpxfile;
  17. }
  18. public function getFilename()
  19. {
  20. return $this->filename;
  21. }
  22. public function getKm() {
  23. // return self::getInfos($filename)["distance"]; en mètre
  24. return self::getInfos()["km"] ;
  25. }
  26. // getInfos($filename) : extrait une sélection d'infos d'une trace GPX
  27. //
  28. // Pas robuste pour l'instant : seule la première piste est traitée
  29. //
  30. // PARAMS: chemin d'in fichier gpx
  31. // RETURN
  32. // un array associatif avec les clés suivantes
  33. // - à détailler
  34. //
  35. public function getInfos() {
  36. $gpx = new phpGPX();
  37. $gpxfile = $gpx->load($this->filename);
  38. dump($gpxfile);
  39. $track1 = $gpxfile->tracks[0];
  40. // dump($track1->stats->toArray());
  41. $infos["name"] = $track1->name;
  42. $infos["comment"] = $track1->comment;
  43. $infos["description"] = $track1->description;
  44. $infos["distance"] = $track1->stats->distance; // car distance est en mètres
  45. $infos["km"] = round($track1->stats->distance)/1000; // car distance est en mètres
  46. // $infos["stats"] = $track1->stats;
  47. $infos["stats"] = $track1->stats->toArray();
  48. return $infos;
  49. }
  50. }
  51. // EXTRAIT DE : https://sibyx.github.io/phpGPX/
  52. //
  53. // phpGPX\Models\GpxFile {#440 ▼
  54. // +waypoints: []
  55. // +routes: []
  56. // +tracks: array:1 [▼
  57. // 0 => phpGPX\Models\Track {#444 ▼
  58. // +name: "9241313-MN rougeau"
  59. // +comment: null
  60. // +description: null
  61. // +source: null
  62. // +links: []
  63. // +number: null
  64. // +type: null
  65. // +extensions: null
  66. // +stats: phpGPX\Models\Stats {#628 ▼
  67. // +distance: 9196.8627613778
  68. // +realDistance: 9208.5165985748
  69. // +averageSpeed: 1.3888346060673
  70. // +averagePace: 720.02814131456
  71. // +minAltitude: 58.0
  72. // +maxAltitude: 94.0
  73. // +cumulativeElevationGain: 111.0
  74. // +cumulativeElevationLoss: 111.0
  75. // +startedAt: DateTime @1660117195 {#455 ▼
  76. // date: 2022-08-10 07:39:55.0 UTC (+00:00)
  77. // }
  78. // +finishedAt: DateTime @1660123817 {#631 ▶}
  79. // +duration: 6622
  80. // }
  81. // +segments: array:1 [▶]
  82. // }
  83. // ]
  84. // +metadata: null
  85. // +extensions: null
  86. // +creator: "Openrunner - https://www.openrunner.com"
  87. // }
  88. // ...
  89. // foreach ($file->tracks as $track) {
  90. // Statistics for whole track
  91. // $track->stats->toArray();
  92. // foreach ($track->segments as $segment) {
  93. // // Statistics for segment of track
  94. // $segment->stats->toArray();
  95. // }
  96. // dump($track->stats->toArray());
  97. // }
  98. // ...
  99. //
  100. // Exemple de manipulation
  101. // // Creating sample Metadata object
  102. // $gpx_file->metadata = new Metadata();
  103. // // Time attribute is always \DateTime object!
  104. // $gpx_file->metadata->time = new \DateTime();
  105. // // Description of GPX file
  106. // $gpx_file->metadata->description = "My pretty awesome GPX file, created using phpGPX library!";
  107. // // Adding link created before to links array of metadata
  108. // // Metadata of GPX file can contain more than one link
  109. // $gpx_file->metadata->links[] = $link;