GPXParser.js

3.0.8 · active · verified Tue Apr 21

GPXParser.js is a JavaScript library designed for parsing .gpx (GPS Exchange Format) files, which are XML-based data structures for GPS information. It extracts core GPX data like metadata, waypoints, tracks, and routes, and further computes various derived metrics such as total and cumulative distances, minimum, maximum, average, positive, and negative elevation differences, and segment slopes. The current stable version is 3.0.8, actively maintained with regular patch releases addressing bug fixes and minor feature enhancements. A key differentiator is its ability to not only parse but also process the geographical data for insights, and export the parsed data into GeoJSON format, making it suitable for modern web mapping applications. It includes built-in polyfills for DOM functionalities, enabling its use in Node.js environments without manual setup since version 3.0.5.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate GPXParser, parse a sample GPX string, access its metadata, tracks, and waypoints, and export the processed data to GeoJSON.

import GPXParser from 'gpxparser';

// A minimal GPX string for demonstration
const gpxString = `<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="GPXParser.js">
  <metadata>
    <name>Sample Track</name>
    <desc>A short sample track by GPXParser.js</desc>
    <author>
      <name>Acme Corp</name>
    </author>
    <time>2023-01-01T12:00:00Z</time>
  </metadata>
  <trk>
    <name>Morning Walk</name>
    <trkseg>
      <trkpt lat="34.0522" lon="-118.2437"><ele>100</ele><time>2023-01-01T12:00:00Z</time></trkpt>
      <trkpt lat="34.0535" lon="-118.2500"><ele>110</ele><time>2023-01-01T12:05:00Z</time></trkpt>
      <trkpt lat="34.0548" lon="-118.2563"><ele>105</ele><time>2023-01-01T12:10:00Z</time></trkpt>
    </trkseg>
  </trk>
  <wpt lat="34.0500" lon="-118.2400"><name>Starting Point</name></wpt>
</gpx>`;

const gpx = new GPXParser();
gpx.parse(gpxString);

console.log('GPX Metadata Name:', gpx.metadata.name);
console.log('Total Tracks Found:', gpx.tracks.length);
if (gpx.tracks.length > 0) {
  console.log('First Track Name:', gpx.tracks[0].name);
  console.log('First Track Total Distance (meters):', gpx.tracks[0].distance.total);
  console.log('First Track Max Elevation (meters):', gpx.tracks[0].elevation.max);
}

console.log('Total Waypoints Found:', gpx.waypoints.length);
if (gpx.waypoints.length > 0) {
  console.log('First Waypoint Name:', gpx.waypoints[0].name);
}

// Export parsed GPX data to GeoJSON format
const geoJSON = gpx.toGeoJSON();
console.log('GeoJSON Output:', JSON.stringify(geoJSON, null, 2));

view raw JSON →