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