Simple library written in PHP for reading and creating GPX files. Documentation is available using GitHub pages generated by Jekyll.
Contribution and feedback is welcome! Please check the issues for TODO. I will be happy every feature or pull request.
Repository branches:
master
: latest stable versiondevelop
: works on2.x
- Full support of official specification.
- Statistics calculation.
- Extensions.
- JSON & XML & PHP Array output.
- (Smoothed) Distance (m)
- Average speed (m/s)
- Average pace (s/km)
- Min / max altitude (m)
- Min / max coordinates ([lat,lng])
- (Smoothed) Elevation gain / loss (m)
- Start / end (DateTime object)
- Start / end coordinates ([lat,lng])
- Duration (seconds)
You can easily install phpGPX library with composer. There is no stable release yet, so please use release candidates.
composer require sibyx/phpgpx:1.3.0
<?phpusephpGPX\phpGPX; $gpx = newphpGPX(); $file = $gpx->load('example.gpx'); foreach ($file->tracksas$track) { // Statistics for whole track$track->stats->toArray(); foreach ($track->segmentsas$segment) { // Statistics for segment of track$segment->stats->toArray(); } }
<?phpusephpGPX\phpGPX; $gpx = newphpGPX(); $file = $gpx->load('example.gpx'); // XML$file->save('output.gpx', phpGPX::XML_FORMAT); //JSON$file->save('output.json', phpGPX::JSON_FORMAT);
<?phpusephpGPX\Models\GpxFile; usephpGPX\Models\Link; usephpGPX\Models\Metadata; usephpGPX\Models\Point; usephpGPX\Models\Segment; usephpGPX\Models\Track; require_once'/vendor/autoload.php'; $sample_data = [ [ 'longitude' => 9.860624216140083, 'latitude' => 54.9328621088893, 'elevation' => 0, 'time' => new \DateTime("+ 1 MINUTE") ], [ 'latitude' => 54.83293237320851, 'longitude' => 9.76092208681491, 'elevation' => 10.0, 'time' => new \DateTime("+ 2 MINUTE") ], [ 'latitude' => 54.73327743521187, 'longitude' => 9.66187816543752, 'elevation' => 42.42, 'time' => new \DateTime("+ 3 MINUTE") ], [ 'latitude' => 54.63342326167919, 'longitude' => 9.562439849679859, 'elevation' => 12, 'time' => new \DateTime("+ 4 MINUTE") ] ]; // Creating sample link object for metadata$link = newLink(); $link->href = "https://sibyx.github.io/phpgpx"; $link->text = 'phpGPX Docs'; // GpxFile contains data and handles serialization of objects$gpx_file = newGpxFile(); // Creating sample Metadata object$gpx_file->metadata = newMetadata(); // Time attribute is always \DateTime object!$gpx_file->metadata->time = new \DateTime(); // Description of GPX file$gpx_file->metadata->description = "My pretty awesome GPX file, created using phpGPX library!"; // Adding link created before to links array of metadata// Metadata of GPX file can contain more than one link$gpx_file->metadata->links[] = $link; // Creating track$track = newTrack(); // Name of track$track->name = "Some random points in logical order. Input array should be already ordered!"; // Type of data stored in track$track->type = 'RUN'; // Source of GPS coordinates$track->source = "MySpecificGarminDevice"; // Creating Track segment$segment = newSegment(); foreach ($sample_dataas$sample_point) { // Creating trackpoint$point = newPoint(Point::TRACKPOINT); $point->latitude = $sample_point['latitude']; $point->longitude = $sample_point['longitude']; $point->elevation = $sample_point['elevation']; $point->time = $sample_point['time']; $segment->points[] = $point; } // Add segment to segment array of track$track->segments[] = $segment; // Recalculate stats based on received data$track->recalculateStats(); // Add track to file$gpx_file->tracks[] = $track; // GPX output$gpx_file->save('CreatingFileFromScratchExample.gpx', \phpGPX\phpGPX::XML_FORMAT); // Serialized data as JSON$gpx_file->save('CreatingFileFromScratchExample.json', \phpGPX\phpGPX::JSON_FORMAT); // Direct GPX output to browserheader("Content-Type: application/gpx+xml"); header("Content-Disposition: attachment; filename=CreatingFileFromScratchExample.gpx"); echo$gpx_file->toXML()->saveXML(); exit();
Currently, supported output formats:
- XML
- JSON
Use the static constants in phpGPX to modify behaviour.
/** * Create Stats object for each track, segment and route */publicstatic$CALCULATE_STATS = true; /** * Additional sort based on timestamp in Routes & Tracks on XML read. * Disabled by default, data should be already sorted. */publicstatic$SORT_BY_TIMESTAMP = false; /** * Default DateTime output format in JSON serialization. */publicstatic$DATETIME_FORMAT = 'c'; /** * Default timezone for display. * Data are always stored in UTC timezone. */publicstatic$DATETIME_TIMEZONE_OUTPUT = 'UTC'; /** * Pretty print. */publicstatic$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 */publicstatic$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 */publicstatic$APPLY_ELEVATION_SMOOTHING = false; /** * if APPLY_ELEVATION_SMOOTHING is true * the minimum elevation difference between considered points in meters */publicstatic$ELEVATION_SMOOTHING_THRESHOLD = 2; /** * if APPLY_ELEVATION_SMOOTHING is true * the maximum elevation difference between considered points in meters */publicstatic$ELEVATION_SMOOTHING_SPIKES_THRESHOLD = null; /** * Apply distance calculation smoothing? If true, the threshold in * DISTANCE_SMOOTHING_THRESHOLD applies */publicstatic$APPLY_DISTANCE_SMOOTHING = false; /** * if APPLY_DISTANCE_SMOOTHING is true * the minimum distance between considered points in meters */publicstatic$DISTANCE_SMOOTHING_THRESHOLD = 2;
I wrote this library as part of my job in Backbone s.r.o..
This project is licensed under the terms of the MIT license.