|
| 1 | +Simple library written in PHP for reading and creating [GPX files](https://en.wikipedia.org/wiki/GPS_Exchange_Format). |
| 2 | + |
| 3 | +Library is currently marked as Release Candidate but is already used in production on several projects without any |
| 4 | +problems. [Documentation](https://sibyx.github.io/phpGPX/) is available using GitHub pages generated by Jekyll. |
| 5 | + |
| 6 | +Contribution and feedback is welcome! Please check the issues for TODO. I will be happy every feature or pull request. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | + - Full support of [official specification](http://www.topografix.com/GPX/1/1/). |
| 11 | + - Statistics calculation. |
| 12 | + - Extensions. |
| 13 | + - JSON & XML & PHP Array output. |
| 14 | + |
| 15 | +### Supported Extensions |
| 16 | + - Garmin [TrackPointExtension](https://www8.garmin.com/xmlschemas/TrackPointExtensionv1.xsd): http://www.garmin.com/xmlschemas/TrackPointExtension/v1 |
| 17 | + |
| 18 | +### Stats calculation |
| 19 | + |
| 20 | + - (Smoothed) Distance (m) |
| 21 | + - Average speed (m/s) |
| 22 | + - Average pace (s/km) |
| 23 | + - Min / max altitude (m) |
| 24 | + - (Smoothed) Elevation gain / loss (m) |
| 25 | + - Start / end (DateTime object) |
| 26 | + - Duration (seconds) |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | +### Open GPX file and load basic stats |
| 31 | +```php |
| 32 | +<?php |
| 33 | +use phpGPX\phpGPX; |
| 34 | + |
| 35 | +$gpx = new phpGPX(); |
| 36 | + |
| 37 | +$file = $gpx->load('example.gpx'); |
| 38 | + |
| 39 | +foreach ($file->tracks as $track) |
| 40 | +{ |
| 41 | + // Statistics for whole track |
| 42 | + $track->stats->toArray(); |
| 43 | + |
| 44 | + foreach ($track->segments as $segment) |
| 45 | + { |
| 46 | + // Statistics for segment of track |
| 47 | + $segment->stats->toArray(); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Writing to file |
| 53 | +```php |
| 54 | +<?php |
| 55 | +use phpGPX\phpGPX; |
| 56 | + |
| 57 | +$gpx = new phpGPX(); |
| 58 | + |
| 59 | +$file = $gpx->load('example.gpx'); |
| 60 | + |
| 61 | +// XML |
| 62 | +$file->save('output.gpx', phpGPX::XML_FORMAT); |
| 63 | + |
| 64 | +//JSON |
| 65 | +$file->save('output.json', phpGPX::JSON_FORMAT); |
| 66 | +``` |
| 67 | + |
| 68 | +### Creating file from scratch |
| 69 | +```php |
| 70 | +<?php |
| 71 | + |
| 72 | +use phpGPX\Models\GpxFile; |
| 73 | +use phpGPX\Models\Link; |
| 74 | +use phpGPX\Models\Metadata; |
| 75 | +use phpGPX\Models\Point; |
| 76 | +use phpGPX\Models\Segment; |
| 77 | +use phpGPX\Models\Track; |
| 78 | + |
| 79 | +require_once '/vendor/autoload.php'; |
| 80 | + |
| 81 | +$sample_data = [ |
| 82 | + [ |
| 83 | + 'longitude' => 9.860624216140083, |
| 84 | + 'latitude' => 54.9328621088893, |
| 85 | + 'elevation' => 0, |
| 86 | + 'time' => new \DateTime("+ 1 MINUTE") |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'latitude' => 54.83293237320851, |
| 90 | + 'longitude' => 9.76092208681491, |
| 91 | + 'elevation' => 10.0, |
| 92 | + 'time' => new \DateTime("+ 2 MINUTE") |
| 93 | + ], |
| 94 | + [ |
| 95 | + 'latitude' => 54.73327743521187, |
| 96 | + 'longitude' => 9.66187816543752, |
| 97 | + 'elevation' => 42.42, |
| 98 | + 'time' => new \DateTime("+ 3 MINUTE") |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'latitude' => 54.63342326167919, |
| 102 | + 'longitude' => 9.562439849679859, |
| 103 | + 'elevation' => 12, |
| 104 | + 'time' => new \DateTime("+ 4 MINUTE") |
| 105 | + ] |
| 106 | +]; |
| 107 | + |
| 108 | +// Creating sample link object for metadata |
| 109 | +$link = new Link(); |
| 110 | +$link->href = "https://sibyx.github.io/phpgpx"; |
| 111 | +$link->text = 'phpGPX Docs'; |
| 112 | + |
| 113 | +// GpxFile contains data and handles serialization of objects |
| 114 | +$gpx_file = new GpxFile(); |
| 115 | + |
| 116 | +// Creating sample Metadata object |
| 117 | +$gpx_file->metadata = new Metadata(); |
| 118 | + |
| 119 | +// Time attribute is always \DateTime object! |
| 120 | +$gpx_file->metadata->time = new \DateTime(); |
| 121 | + |
| 122 | +// Description of GPX file |
| 123 | +$gpx_file->metadata->description = "My pretty awesome GPX file, created using phpGPX library!"; |
| 124 | + |
| 125 | +// Adding link created before to links array of metadata |
| 126 | +// Metadata of GPX file can contain more than one link |
| 127 | +$gpx_file->metadata->links[] = $link; |
| 128 | + |
| 129 | +// Creating track |
| 130 | +$track = new Track(); |
| 131 | + |
| 132 | +// Name of track |
| 133 | +$track->name = "Some random points in logical order. Input array should be already ordered!"; |
| 134 | + |
| 135 | +// Type of data stored in track |
| 136 | +$track->type = 'RUN'; |
| 137 | + |
| 138 | +// Source of GPS coordinates |
| 139 | +$track->source = "MySpecificGarminDevice"; |
| 140 | + |
| 141 | +// Creating Track segment |
| 142 | +$segment = new Segment(); |
| 143 | + |
| 144 | + |
| 145 | +foreach ($sample_data as $sample_point) |
| 146 | +{ |
| 147 | + // Creating trackpoint |
| 148 | + $point = new Point(Point::TRACKPOINT); |
| 149 | + $point->latitude = $sample_point['latitude']; |
| 150 | + $point->longitude = $sample_point['longitude']; |
| 151 | + $point->elevation = $sample_point['elevation']; |
| 152 | + $point->time = $sample_point['time']; |
| 153 | + |
| 154 | + $segment->points[] = $point; |
| 155 | +} |
| 156 | + |
| 157 | +// Add segment to segment array of track |
| 158 | +$track->segments[] = $segment; |
| 159 | + |
| 160 | +// Recalculate stats based on received data |
| 161 | +$track->recalculateStats(); |
| 162 | + |
| 163 | +// Add track to file |
| 164 | +$gpx_file->tracks[] = $track; |
| 165 | + |
| 166 | +// GPX output |
| 167 | +$gpx_file->save('CreatingFileFromScratchExample.gpx', \phpGPX\phpGPX::XML_FORMAT); |
| 168 | + |
| 169 | +// Serialized data as JSON |
| 170 | +$gpx_file->save('CreatingFileFromScratchExample.json', \phpGPX\phpGPX::JSON_FORMAT); |
| 171 | + |
| 172 | +// Direct GPX output to browser |
| 173 | + |
| 174 | +header("Content-Type: application/gpx+xml"); |
| 175 | +header("Content-Disposition: attachment; filename=CreatingFileFromScratchExample.gpx"); |
| 176 | + |
| 177 | +echo $gpx_file->toXML()->saveXML(); |
| 178 | +exit(); |
| 179 | +``` |
| 180 | + |
| 181 | +## Installation |
| 182 | + |
| 183 | +You can easily install phpGPX library with composer. There is no stable release yet, so please use release candidates. |
| 184 | + |
| 185 | +``` |
| 186 | +composer require sibyx/phpgpx:@RC |
| 187 | +``` |
| 188 | + |
| 189 | +## API Documentation |
| 190 | + |
| 191 | +* phpGPX |
| 192 | + * phpGPX\Parsers |
| 193 | + * [EmailParser](phpGPX-Parsers-EmailParser.md) |
| 194 | + * [CopyrightParser](phpGPX-Parsers-CopyrightParser.md) |
| 195 | + * [SegmentParser](phpGPX-Parsers-SegmentParser.md) |
| 196 | + * [LinkParser](phpGPX-Parsers-LinkParser.md) |
| 197 | + * [TrackParser](phpGPX-Parsers-TrackParser.md) |
| 198 | + * [WaypointParser](phpGPX-Parsers-WaypointParser.md) |
| 199 | + * [PersonParser](phpGPX-Parsers-PersonParser.md) |
| 200 | + * [MetadataParser](phpGPX-Parsers-MetadataParser.md) |
| 201 | + * phpGPX\Parsers\Extensions |
| 202 | + * [TrackPointExtensionParser](phpGPX-Parsers-Extensions-TrackPointExtensionParser.md) |
| 203 | + * [PointParser](phpGPX-Parsers-PointParser.md) |
| 204 | + * [RouteParser](phpGPX-Parsers-RouteParser.md) |
| 205 | + * [ExtensionParser](phpGPX-Parsers-ExtensionParser.md) |
| 206 | + * [BoundsParser](phpGPX-Parsers-BoundsParser.md) |
| 207 | + * phpGPX\Helpers |
| 208 | + * [DateTimeHelper](phpGPX-Helpers-DateTimeHelper.md) |
| 209 | + * [GeoHelper](phpGPX-Helpers-GeoHelper.md) |
| 210 | + * [SerializationHelper](phpGPX-Helpers-SerializationHelper.md) |
| 211 | + * phpGPX\Models |
| 212 | + * [Point](phpGPX-Models-Point.md) |
| 213 | + * [Extensions](phpGPX-Models-Extensions.md) |
| 214 | + * [AbstractExtension](phpGPX-Models-Extensions-AbstractExtension.md) |
| 215 | + * [TrackPointExtension](phpGPX-Models-Extensions-TrackPointExtension.md) |
| 216 | + * [Stats](phpGPX-Models-Stats.md) |
| 217 | + * [Route](phpGPX-Models-Route.md) |
| 218 | + * [Email](phpGPX-Models-Email.md) |
| 219 | + * [Collection](phpGPX-Models-Collection.md) |
| 220 | + * [StatsCalculator](phpGPX-Models-StatsCalculator.md) |
| 221 | + * [Copyright](phpGPX-Models-Copyright.md) |
| 222 | + * [Summarizable](phpGPX-Models-Summarizable.md) |
| 223 | + * [Segment](phpGPX-Models-Segment.md) |
| 224 | + * [Person](phpGPX-Models-Person.md) |
| 225 | + * [Link](phpGPX-Models-Link.md) |
| 226 | + * [GpxFile](phpGPX-Models-GpxFile.md) |
| 227 | + * [Bounds](phpGPX-Models-Bounds.md) |
| 228 | + * [Metadata](phpGPX-Models-Metadata.md) |
| 229 | + * [Track](phpGPX-Models-Track.md) |
| 230 | + * [phpGPX](phpGPX-phpGPX.md) |
| 231 | + |
| 232 | +## Contributors |
| 233 | + |
| 234 | + - [Jakub Dubec](https://github.com/Sibyx) - Initial works, maintenance |
| 235 | + - [Lukasz Lewandowski](https://github.com/luklewluk) |
| 236 | + |
| 237 | +I wrote this library as part of my job in [Backbone s.r.o.](https://www.backbone.sk/en/). |
| 238 | + |
| 239 | +## License |
| 240 | + |
| 241 | +This project is licensed under the terms of the MIT license. |
0 commit comments