vendor/sibyx/phpgpx/src/phpGPX/phpGPX.php line 109

Open in your IDE?
  1. <?php
  2. /**
  3. * Created 26/08/16 13:45
  4. * @author Jakub Dubec <jakub.dubec@gmail.com>
  5. */
  6. namespace phpGPX;
  7. use phpGPX\Models\GpxFile;
  8. use phpGPX\Parsers\MetadataParser;
  9. use phpGPX\Parsers\RouteParser;
  10. use phpGPX\Parsers\TrackParser;
  11. use phpGPX\Parsers\WaypointParser;
  12. /**
  13. * Class phpGPX
  14. * @package phpGPX
  15. */
  16. class phpGPX
  17. {
  18. const JSON_FORMAT = 'json';
  19. const XML_FORMAT = 'xml';
  20. const PACKAGE_NAME = 'phpGPX';
  21. const VERSION = '1.3.0';
  22. /**
  23. * Create Stats object for each track, segment and route
  24. * @var bool
  25. */
  26. public static $CALCULATE_STATS = true;
  27. /**
  28. * Additional sort based on timestamp in Routes & Tracks on XML read.
  29. * Disabled by default, data should be already sorted.
  30. * @var bool
  31. */
  32. public static $SORT_BY_TIMESTAMP = false;
  33. /**
  34. * Default DateTime output format in JSON serialization.
  35. * @var string
  36. */
  37. public static $DATETIME_FORMAT = 'c';
  38. /**
  39. * Default timezone for display.
  40. * Data are always stored in UTC timezone.
  41. * @var string
  42. */
  43. public static $DATETIME_TIMEZONE_OUTPUT = 'UTC';
  44. /**
  45. * Pretty print.
  46. * @var bool
  47. */
  48. public static $PRETTY_PRINT = true;
  49. /**
  50. * In stats elevation calculation: ignore points with an elevation of 0
  51. * This can happen with some GPS software adding a point with 0 elevation
  52. *
  53. * @var bool
  54. */
  55. public static $IGNORE_ELEVATION_0 = true;
  56. /**
  57. * Apply elevation gain/loss smoothing? If true, the threshold in
  58. * ELEVATION_SMOOTHING_THRESHOLD and ELEVATION_SMOOTHING_SPIKES_THRESHOLD (if not null) applies
  59. * @var bool
  60. */
  61. public static $APPLY_ELEVATION_SMOOTHING = false;
  62. /**
  63. * if APPLY_ELEVATION_SMOOTHING is true
  64. * the minimum elevation difference between considered points in meters
  65. * @var int
  66. */
  67. public static $ELEVATION_SMOOTHING_THRESHOLD = 2;
  68. /**
  69. * if APPLY_ELEVATION_SMOOTHING is true
  70. * the maximum elevation difference between considered points in meters
  71. * @var int|null
  72. */
  73. public static $ELEVATION_SMOOTHING_SPIKES_THRESHOLD = null;
  74. /**
  75. * Apply distance calculation smoothing? If true, the threshold in
  76. * DISTANCE_SMOOTHING_THRESHOLD applies
  77. * @var bool
  78. */
  79. public static $APPLY_DISTANCE_SMOOTHING = false;
  80. /**
  81. * if APPLY_DISTANCE_SMOOTHING is true
  82. * the minimum distance between considered points in meters
  83. * @var int
  84. */
  85. public static $DISTANCE_SMOOTHING_THRESHOLD = 2;
  86. /**
  87. * Load GPX file.
  88. * @param $path
  89. * @return GpxFile
  90. */
  91. public static function load($path)
  92. {
  93. $xml = file_get_contents($path);
  94. return self::parse($xml);
  95. }
  96. /**
  97. * Parse GPX data string.
  98. * @param $xml
  99. * @return GpxFile
  100. */
  101. public static function parse($xml)
  102. {
  103. $xml = simplexml_load_string($xml);
  104. $gpx = new GpxFile();
  105. // Parse creator
  106. $gpx->creator = isset($xml['creator']) ? (string)$xml['creator'] : null;
  107. // Parse metadata
  108. $gpx->metadata = isset($xml->metadata) ? MetadataParser::parse($xml->metadata) : null;
  109. // Parse waypoints
  110. $gpx->waypoints = isset($xml->wpt) ? WaypointParser::parse($xml->wpt) : [];
  111. // Parse tracks
  112. $gpx->tracks = isset($xml->trk) ? TrackParser::parse($xml->trk) : [];
  113. // Parse routes
  114. $gpx->routes = isset($xml->rte) ? RouteParser::parse($xml->rte) : [];
  115. return $gpx;
  116. }
  117. /**
  118. * Create library signature from name and version.
  119. * @return string
  120. */
  121. public static function getSignature()
  122. {
  123. return sprintf("%s/%s", self::PACKAGE_NAME, self::VERSION);
  124. }
  125. }