gpxpy Documentation

repository·dev·Indexed 22 days ago

https://github.com/tkrajina/gpxpy

A Python library for parsing and manipulating GPX (GPS Exchange Format) files. It enables developers to read, create, and modify GPS tracks, segments, points, waypoints, and routes. The library supports GPX 1.0 and 1.1, provides methods for calculating statistics like max speed, and offers optional performance acceleration via lxml.

Tokens
1.1K
Snippets
5
Records
5
Agent score
28%

What's inside gpxpy

  1. Create a new GPX file from scratch

    dev

    To generate a new GPX file, instantiate gpxpy.gpx.GPX(). You must manually build the hierarchy by appending GPXTrack objects to the GPX object, GPXTrackSegment objects to the tracks, and GPXTrackPoint objects to the segments.

    Finally, use .to_xml() to serialize the object into a GPX XML string.

    import gpxpy
    import gpxpy.gpx
    
    # Initialize GPX object
    gpx = gpxpy.gpx.GPX()
    
    # Create and add a track
    gpx_track = gpxpy.gpx.GPXTrack()
    gpx.tracks.append(gpx_track)
    
    # Create and add a segment to the track
    gpx_segment = gpxpy.gpx.GPXTrackSegment()
    gpx_track.segments.append(gpx_segment)
    
    # Create and add points to the segment
    gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(2.1234, 5.1234, elevation=1234))
    
    # Serialize to XML
    print(gpx.to_xml())
  2. Parse an existing GPX file

    dev

    Use gpxpy.parse() to load an existing GPX file. The resulting object allows you to iterate through tracks, segments, points, waypoints, and routes.

    Each track contains segments, and each segment contains points. Points provide access to latitude, longitude, and elevation.

    Note: If lxml is installed in your environment, gpxpy will use it for significantly faster parsing (2-3x faster) than the default minidom.

    import gpxpy
    
    # Open and parse the file
    with open('test_files/cerknicko-jezero.gpx', 'r') as gpx_file:
        gpx = gpxpy.parse(gpx_file)
    
    # Accessing tracks, segments, and points
    for track in gpx.tracks:
        for segment in track.segments:
            for point in segment.points:
                print(f'Point at ({point.latitude},{point.longitude}) -> {point.elevation}')
    
    # Accessing waypoints
    for waypoint in gpx.waypoints:
        print(f'waypoint {waypoint.name} -> ({waypoint.latitude},{waypoint.longitude})')
    
    # Accessing routes
    for route in gpx.routes:
        for point in route.points:
            print(f'Point at ({point.latitude},{point.longitude}) -> {point.elevation}')
  3. Calculate max speed with or without heuristics

    dev

    The library provides methods to calculate statistics. When calculating max_speed, the library uses heuristics to remove the top 5% of speeds and points with nonstandard distances to filter out common GPS errors.

    If you require the "raw" maximum speed without these error-correction heuristics, use get_moving_data(raw=True).

    # Get raw data for manual speed calculation
    moving_data = gpx.get_moving_data(raw=True)
  4. Serialize GPX to XML with version control

    dev

    The .to_xml() method converts the GPX object model into an XML string.

    By default, the library handles both GPX 1.0 and 1.1. However, because the object model is a hybrid to support both, some data (like the speed attribute from GPX 1.0) might be lost if you serialize a GPX 1.1 object without using extensions. To ensure compatibility with older systems, you can force the output version using the version parameter.

    # Force serialization to GPX 1.0
    xml_string = gpx.to_xml(version="1.0")