Note
This is an experimental Risor module written almost entirely by Claude 3.7, using a custom prompt.
The gpx Risor module provides functionality for parsing and working with GPX (GPS Exchange Format) files.
This module is a Risor wrapper around the tkrajina/gpxgo library, allowing you to parse GPX data from strings, byte arrays, or files, and access the structured GPS data they contain.
parse(xmlString string) gpxParses a GPX document from a string.
gpxData := gpx.parse(`<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="My GPS Device">
<trk>
<name>Morning Run</name>
<trkseg>
<trkpt lat="37.7749" lon="-122.4194">
<ele>10</ele>
<time>2023-01-01T08:00:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>`)
print(gpxData.tracks[0].name) // "Morning Run"parse_bytes(data bytes) gpxParses a GPX document from a byte array.
gpxBytes := os.read_file("route.gpx")
gpxData := gpx.parse_bytes(gpxBytes)parse_file(path string) gpxParses a GPX document directly from a file at the given path.
gpxData := gpx.parse_file("route.gpx")The main GPX object representing a complete GPX document.
| Name | Type | Description |
|---|---|---|
| track_points_no | func() int | Returns the total number of track points |
| info | func() string | Returns a string with information about the GPX document |
| tracks | list | A list of gpx.track objects in the document |
Represents a track in a GPX document, which typically corresponds to one recorded activity.
| Name | Type | Description |
|---|---|---|
| name | string | The name of the track |
| segments | list | A list of gpx.segment objects |
Represents a track segment, which is a continuous recorded portion of a track.
| Name | Type | Description |
|---|---|---|
| points | list | A list of gpx.point objects |
Represents a single GPS waypoint with coordinates and optional metadata.
| Name | Type | Description |
|---|---|---|
| lat | float | The latitude coordinate |
| latitude | float | Alias for lat |
| lon | float | The longitude coordinate |
| longitude | float | Alias for lon |
| elevation | float | The elevation in meters (nil if not available) |
| timestamp | time | The timestamp of the point (nil if not available) |
// Parse a GPX file
gpxData := gpx.parse_file("test.gpx")
// Get some basic information
print("GPX Info:", gpxData.info())
print("Total points:", gpxData.track_points_no())
// Iterate through tracks, segments and points
for i, track := range gpxData.tracks {
print('Track {i}: {track.name}')
for j, segment := range track.segments {
print(' Segment {j} has {len(segment.points)} points')
// Print the first point if available
if len(segment.points) > 0 {
point := segment.points[0]
print(' First point: lat={point.lat}, lon={point.lon}')
if point.elevation != nil {
print(' Elevation: {point.elevation}')
}
if point.timestamp != nil {
print(' Time: {point.timestamp}')
}
}
}
}