Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/georust/gpx
Rust read/write support for GPS Exchange Format (GPX)
https://github.com/georust/gpx
geospatial gps-data gpx gpx-parser hacktoberfest rust
Last synced: 7 days ago
JSON representation
Rust read/write support for GPS Exchange Format (GPX)
- Host: GitHub
- URL: https://github.com/georust/gpx
- Owner: georust
- License: mit
- Created: 2015-01-10T04:16:00.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-10-08T13:26:39.000Z (about 1 month ago)
- Last Synced: 2024-10-29T20:19:39.437Z (15 days ago)
- Topics: geospatial, gps-data, gpx, gpx-parser, hacktoberfest, rust
- Language: Rust
- Homepage: https://crates.io/crates/gpx
- Size: 414 KB
- Stars: 101
- Watchers: 16
- Forks: 46
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# gpx
[![Crates.io](https://img.shields.io/crates/v/gpx.svg)](https://crates.io/crates/gpx)
[![Build Status](https://github.com/georust/gpx/actions/workflows/test.yml/badge.svg)](https://github.com/georust/gpx/actions/workflows/test.yml)
[![docs.rs](https://docs.rs/gpx/badge.svg)](https://docs.rs/gpx)gpx is a library for reading and writing GPX (GPS Exchange Format) files. It uses the
primitives provided by [geo-types](https://github.com/georust/geo) to allow for storage
of GPS data.## Examples
### Read a GPX file
```rust
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.
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 ``Waypoint``s and ``Route``s.```rust
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>(out_path: P) -> Result<(), Box> {
// 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();
gpx.tracks[0].segments[0].points.push(Waypoint::new(geo_point));// Write to file
gpx::write(&gpx, buf)?;Ok(())
}
```### 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.
```rust
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.## Contributing
All contributions are welcome! Please open an issue if you find a bug / have any
questions, and pull requests are always appreciated.## License
rust-gpx is licensed under the [MIT license](./LICENSE).