API Reference#

gpx.gpx Module#

This module provides a GPX object to contain GPX files, consisting of waypoints, routes and tracks.

class gpx.gpx.GPX(gpx=None)#

A GPX class for the GPX data format.

Parameters:

gpx (etree._Element | None) – The GPX XML element. Defaults to None.

author: dict[str, Any]#

The person or organization who created the GPX file.

bounds: tuple[float, float, float, float] | None#

Minimum and maximum coordinates which describe the extent of the coordinates in the file.

copyright: dict[str, str | int]#

Copyright and license information governing use of the file.

creator: str | None#

The name or URL of the software that created your GPX document. Defaults to “PyGPX”.

desc: str | None#

A description of the contents of the GPX file.

classmethod from_file(gpx_file, validate=False)#

Create an GPX instance from a file.

>>> from gpx import GPX
>>> gpx = GPX.from_file("path/to/file.gpx")
>>> print(gpx.bounds)
Parameters:
  • gpx_file (str | Path) – The file containing the GPX data.

  • validate (bool) – Whether to validate the GPX data.

Returns:

The GPX instance.

Return type:

GPX

classmethod from_string(gpx_str, validate=False)#

Create an GPX instance from a string.

>>> from gpx import GPX
>>> gpx = GPX.from_str("""<?xml version="1.0" encoding="UTF-8" ?>
... <gpx xmlns="http://www.topografix.com/GPX/1/1" creator="PyGPX" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
...     [...]
... </gpx>""")
>>> print(gpx.bounds)
Parameters:
  • gpx_str (str) – The string containing the GPX data.

  • validate (bool) – Whether to validate the GPX data.

Returns:

The GPX instance.

Return type:

GPX

keywords: str | None#

Keywords associated with the file. Search engines or databases can use this information to classify the data.

URLs associated with the location described in the file.

name: str | None#

The name of the GPX file.

routes: list[gpx.route.Route]#

A list of routes.

time: datetime | None#

The creation date of the file.

to_file(gpx_file)#

Serialize the GPX instance to a file.

Parameters:

gpx_file (str | Path) – The file to write the GPX data to.

to_string()#

Serialize the GPX instance to a string.

Returns:

The GPX data as a string.

Return type:

str

tracks: list[gpx.track.Track]#

A list of tracks.

waypoints: list[gpx.waypoint.Waypoint]#

A list of waypoints.

gpx.waypoint Module#

This module provides a Waypoint object to contain GPX waypoints.

class gpx.waypoint.Waypoint(wpt=None)#

A waypoint class for the GPX data format.

Parameters:

wpt (etree._Element | None) – The waypoint XML element. Defaults to None.

ageofdgpsdata: float | None#

Number of seconds since last DGPS update.

cmt: str | None#

GPS waypoint comment. Sent to GPS as comment.

desc: str | None#

A text description of the element. Holds additional information about the element intended for the user, not the GPS.

dgpsid: int | None#

ID of DGPS station used in differential correction.

distance_to(other, radius=6378137)#

Returns the distance to the other waypoint (in metres) using a simple spherical earth model.

Parameters:
  • other (Waypoint) – The other waypoint.

  • radius (int) – The radius of the earth (defaults to 6,378,137 metres).

Returns:

The distance to the other waypoint (in metres).

Return type:

float

Adapted from: https://github.com/chrisveness/geodesy/blob/33d1bf53c069cd7dd83c6bf8531f5f3e0955c16e/latlon-spherical.js#L187-L205

duration_to(other)#

Returns the duration to the other waypoint (in seconds).

Parameters:

other (Waypoint) – The other waypoint.

Returns:

The duration to the other waypoint (in seconds).

Return type:

float

ele: float | None#

Elevation (in meters) of the point.

fix: str | None#

Type of GPX fix.

geoidheight: float | None#

Height (in meters) of geoid (mean sea level) above WGS84 earth ellipsoid. As defined in NMEA GGA message.

hdop: float | None#

Horizontal dilution of precision.

lat: float#

The latitude of the point. Decimal degrees, WGS84 datum.

Link to additional information about the waypoint.

lon: float#

The longitude of the point. Decimal degrees, WGS84 datum.

magvar: float | None#

Magnetic variation (in degrees) at the point

name: str | None#

The GPS name of the waypoint. This field will be transferred to and from the GPS. GPX does not place restrictions on the length of this field or the characters contained in it. It is up to the receiving application to validate the field before sending it to the GPS.

pdop: float | None#

Position dilution of precision.

sat: int | None#

Number of satellites used to calculate the GPX fix.

src: str | None#

Source of data. Included to give user some idea of reliability and accuracy of data. “Garmin eTrex”, “USGS quad Boston North”, e.g.

sym: str | None#

Text of GPS symbol name. For interchange with other programs, use the exact spelling of the symbol as displayed on the GPS. If the GPS abbreviates words, spell them out.

time: datetime | None#

Creation/modification timestamp for element. Date and time in are in Universal Coordinated Time (UTC), not local time! Conforms to ISO 8601 specification for date/time representation. Fractional seconds are allowed for millisecond timing in tracklogs.

type: str | None#

Type (classification) of the waypoint.

vdop: float | None#

Vertical dilution of precision.

gpx.route Module#

This module provides a Route object to contain GPX routes - ordered lists of waypoints representing a series of turn points leading to a destination.

class gpx.route.Route(rte=None)#

A route class for the GPX data format.

Parameters:

rte (etree._Element | None) – The route XML element. Defaults to None.

property bounds: tuple[float, float, float, float]#

Returns the bounds of the route.

cmt: str | None#

GPS comment for route.

desc: str | None#

Text description of route for user. Not sent to GPS.

Links to external information about the route.

name: str | None#

GPS name of route.

number: int | None#

GPS route number.

points: list[gpx.waypoint.Waypoint]#

A list of route points.

src: str | None#

Source of data. Included to give user some idea of reliability and accuracy of data.

type: str | None#

Type (classification) of route.

gpx.track Module#

This module provides a Track object to contain GPX routes - an ordered list of points describing a path.

class gpx.track.Track(trk=None)#

A track class for the GPX data format.

Parameters:

trk (etree._Element | None) – The track XML element. Defaults to None.

property bounds: tuple[float, float, float, float]#

Returns the bounds of the track.

cmt: str | None#

GPS comment for track.

desc: str | None#

User description of track.

property distance: float#

Returns the distance of the track (in metres).

property duration: float#

Returns the duration of the track (in seconds).

Links to external information about track.

name: str | None#

GPS name of track.

number: int | None#

GPS track number.

segments: list[list[gpx.waypoint.Waypoint]]#

A Track Segment holds a list of Track Points which are logically connected in order. To represent a single GPS track where GPS reception was lost, or the GPS receiver was turned off, start a new Track Segment for each continuous span of track data.

src: str | None#

Source of data. Included to give user some idea of reliability and accuracy of data.

type: str | None#

Type (classification) of track.

gpx.utils Module#

This module contains various utility functions.

gpx.utils.remove_encoding_from_string(s)#

Removes encoding declarations (e.g. encoding=”utf-8”) from the string, if any.

Parameters:

s (str) – The string.

Returns:

The string with any encoding declarations removed.

Return type:

str

gpx.errors Module#

This module provices various error types.

exception gpx.errors.InvalidGPXError#

GPX is invalid.