PHPackages                             derickr/gpx-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. derickr/gpx-utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

derickr/gpx-utils
=================

Set of utils to merge and sort different types of GPX files

0.3.0(1y ago)2471MITPHPPHP &gt;=8.0.0

Since Sep 4Pushed 1y ago2 watchersCompare

[ Source](https://github.com/derickr/gpx-tools)[ Packagist](https://packagist.org/packages/derickr/gpx-utils)[ Docs](https://derickrethans.nl/projects.php#gpx-utils)[ Fund](https://xdebug.org/support)[ GitHub Sponsors](https://github.com/derickr)[ RSS](/packages/derickr-gpx-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

GPX Utils
---------

[](#gpx-utils)

A set of utilities to operate on GPX files and sets of GPX files.

### Reading GPX Files

[](#reading-gpx-files)

The GPX reader class `DerickR/GPX/Reader` can be used to read a GPX file.

It can read GPX routes (`rte`/`rtept`) and if none are present, it will read a GPX track (`trk`/`trkseg`/`trkpt`) instead.

You can read a GPX file with:

```

```

To access the tracks, you can use the `getTrack()` method which returns a `DerickR/GPX/Track` object, which encapsulates a ordered set of coordinate pairs. If the GPX file had multiple tracks, these are merged into one:

```
$track = $gpxInformation->getTrack();
```

To get individual tracks out of a GPX file, you can use the `getTracks()`method to obtain an array of `Track` objects:

```
$tracks = $gpxInformation->getTracks();
foreach ( $tracks as $track )
{
        /* Do something with $track */
}
```

For GPX files that contain routes (`rte`/`rtept`) this will still return a merged track.

For GPX files that contain tracks (`trk`/`trkseg`) this return an array of tracks, with each `Track` object representing a track as stored in the GPX file.

To merge tracks stored in multiple GPX files, see [Merging GPX Files](#merging-gpx-files).

### Writing GPX Files

[](#writing-gpx-files)

The GPX writer class `DerickR/GPX/Writer` can be used to write a `Track`object as a GPX track using `trk`/`trkseg`/`trkpt` elements. It only supports the `lat` and `lon` XML elements.

You can write a track to GPX file with:

```

```

Instead of writing GPX XML directly to a file, it is also possible to retrieve a string with this information, by using the getGpxString() method:

```

```

### Operating on Tracks

[](#operating-on-tracks)

The `Track` class has several methods to operate on GPX tracks. The following methods are supported:

`getTrackData`Returns an array with lat/lon coordinate pairs.`getFirstPointAsGeoJson`Returns the first element in the track as a GeoJson formatted array.`getLastPointAsGeoJson`Returns the last element in the track as a GeoJson formatted array.`appendTrack( Track $newTrack )`Adds all the coordinate pairs from `$newTrack` in sequence to the track.`reverse()`Reverses the array of coordinate pairs that make up the track.`truncatePrecision( int $decimals )`Loops over all the coordinate pairs in the track, and truncates the number of decimals in the coordinate pair to `$decimals`. In a second loop, it merges two consecutive coordinate pairs if they are the same after truncation.### Merging GPX Files

[](#merging-gpx-files)

The `Utils::createMergedTrack` static method can be used to combine a set of GPX files. The method automatically reverses and selects the order in which the tracks are merged, based on the first and last points in the track.

The second argument to `createMergedTrack` determines how far (in metres) the different points of different tracks might lay away from each other to be considered as consecutive. By default, this is `25.0` metres.

If the GPX files are not consecutive then an Exception in thrown.

For example to merge all GPX files in a directory, you would use:

```

```

Changelog
---------

[](#changelog)

ReleaseChanges0.1.0Initial Release0.2.0Added the DerickRGPXWriter::getGpxString() method0.3.0Added the DerickRGPXReader::getTracks() method

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~350 days

Total

3

Last Release

651d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/87eaea74e4be4e86084decfe79612a4fb7b3bd76718a54368a0fe8593de7873b?d=identicon)[derickr](/maintainers/derickr)

---

Top Contributors

[![derickr](https://avatars.githubusercontent.com/u/208074?v=4)](https://github.com/derickr "derickr (17 commits)")[![jsoares7](https://avatars.githubusercontent.com/u/3789490?v=4)](https://github.com/jsoares7 "jsoares7 (2 commits)")

---

Tags

gpsgpxalgorithms

### Embed Badge

![Health badge](/badges/derickr-gpx-utils/health.svg)

```
[![Health](https://phpackages.com/badges/derickr-gpx-utils/health.svg)](https://phpackages.com/packages/derickr-gpx-utils)
```

###  Alternatives

[mjaschen/phpgeo

Simple Yet Powerful Geo Library

1.6k8.6M22](/packages/mjaschen-phpgeo)[doganoo/php-algorithms

A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell

9485.4k7](/packages/doganoo-php-algorithms)[ballen/distical

A distance (Lat/Long) calculation library for PHP

40180.2k2](/packages/ballen-distical)[funiq/geophp

Open-source native PHP library for doing geometry operations. Can read and write a wide variety of formats: (E)WKT, (E)WKB, TWKB, GeoJSON, KML, GPX, GeoRSS. Works with all Simple-Feature geometries (Point, LineString, Polygon...) and can be used to get centroids, bounding-boxes, area, etc.

21114.4k1](/packages/funiq-geophp)[vojtech-dobes/nette-forms-gpspicker

Google Maps based picker of coordinates for Nette Framework.

2456.6k](/packages/vojtech-dobes-nette-forms-gpspicker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
