18 releases (10 breaking)

0.10.0 Dec 4, 2023
0.9.1 Jun 5, 2023
0.9.0 May 30, 2023
0.8.6 May 28, 2022
0.0.1 Jan 21, 2015

#1 in #exchange

Download history 1142/week @ 2024-01-07 802/week @ 2024-01-14 1102/week @ 2024-01-21 1156/week @ 2024-01-28 1133/week @ 2024-02-04 1582/week @ 2024-02-11 1395/week @ 2024-02-18 1724/week @ 2024-02-25 977/week @ 2024-03-03 1317/week @ 2024-03-10 1344/week @ 2024-03-17 1157/week @ 2024-03-24 1251/week @ 2024-03-31 1308/week @ 2024-04-07 1595/week @ 2024-04-14 1114/week @ 2024-04-21

5,303 downloads per month
Used in 10 crates

MIT license

2.5K SLoC


Crates.io Build Status docs.rs

gpx is a library for reading and writing GPX (GPS Exchange Format) files. It uses the primitives provided by geo-types to allow for storage of GPS data.


Read a GPX file

extern crate gpx;

use std::io::BufReader;
use std::fs::File;

use gpx::read;
use gpx::{Gpx, Track, TrackSegment};

fn main() {
    // This XML file actually exists — try it for yourself!
    let file = File::open("tests/fixtures/wikipedia_example.gpx").unwrap();
    let reader = BufReader::new(file);

    // read takes any io::Read and gives a Result<Gpx, Error>.
    let gpx: Gpx = read(reader).unwrap();

    // Each GPX file has multiple "tracks", this takes the first one.
    let track: &Track = &gpx.tracks[0];
    assert_eq!(track.name, Some(String::from("Example GPX Document")));

    // Each track will have different segments full of waypoints, where a
    // waypoint contains info like latitude, longitude, and elevation.
    let segment: &TrackSegment = &track.segments[0];

    // This is an example of retrieving the elevation (in meters) at certain points.
    assert_eq!(segment.points[0].elevation, Some(4.46));
    assert_eq!(segment.points[1].elevation, Some(4.94));
    assert_eq!(segment.points[2].elevation, Some(6.87));

Generate a new GPX file

This example only generates tracks. You can add waypoints and routes as well by instantiating new Waypoints and Routes.

use geo_types::{coord, Point};
use gpx::{Gpx, GpxVersion, Track, TrackSegment, Waypoint};
use std::{error::Error, fs::File, io::BufWriter, path::Path};

pub fn to_gpx<P: AsRef<Path>>(out_path: P) -> Result<(), Box<dyn Error>> {
    // Instantiate Gpx struct
    let track_segment = TrackSegment {
        points: vec![]
    let track = Track {
        name: Some("Track 1".to_string()),
        comment: None,
        description: None,
        source: None,
        links: vec![],
        type_: None,
        number: None,
        segments: vec![track_segment],
    let mut gpx = Gpx {
        version: GpxVersion::Gpx11,
        creator: None,
        metadata: None,
        waypoints: vec![],
        tracks: vec![track],
        routes: vec![],

    // Create file at path
    let gpx_file = File::create(out_path)?;
    let buf = BufWriter::new(gpx_file);

    // Add track point
    let geo_coord = coord! { x: -121.1, y: 38.82 };
    let geo_point: Point = geo_coord.into();

    // Write to file
    gpx::write(&gpx, buf)?;


Write to string

write will write the GPX output to anything that implements std::io::Write. To save the output to a string, write it to a u8 vector, and then convert the vector to a string.

let mut vec = Vec::new();
gpx::write(&gpx, &mut vec)?;
let string = String::from_utf8(vec)?;

Current Status

rust-gpx currently supports reading and writing both GPX 1.1 and 1.0. GPX extensions are not yet supported.


All contributions are welcome! Please open an issue if you find a bug / have any questions, and pull requests are always appreciated.


rust-gpx is licensed under the MIT license.


~52K SLoC