pythongpx

get speed from gpxpy.GpxTrack


i trying to get speed,course from gpx track my trackPoint

<trkpt lon="77.209995" lat="28.520444"><ele>236.502853</ele><time>2022-02-20T02:04:51Z</time><extensions><speed>0.788036</speed><course>209.456757</course><hAcc>6.222376</hAcc><vAcc>1.408607</vAcc></extensions></trkpt>
with open('kaggle/input/workout-routes/route_2022-02-02_8.06pm.gpx', 'r') as gpx_file:
    gpx = gpxpy.parse(gpx_file)
route_info = []

for track in gpx.tracks:
    for segment in track.segments:
        for ind, point in enumerate(segment.points):
            print(point.speed)
            route_info.append({
                'latitude': point.latitude,
                'longitude': point.longitude,
                'elevation': point.elevation,
                'time': np.datetime64(point.time),
                'course': point.course
            })

point.speed = None

i trying to get speed with extensions like this

point.extensions

and get [<Element 'speed' at 0x000001ADBB50A7A0>]

how i can get speed and at it in route_info


Solution

  • To get the speed from the extensions in a GPX track point using gpxpy, you can extract the extension data manually.

    First, parse the code using::::

    with open('your_gpx_file.gpx', 'r') as gpx_file:
        gpx = gpxpy.parse(gpx_file)  
    

    then repeat through tracks and segments with:

    for track in gpx.tracks:
        for segment in track.segments:
            for point in segment.points:
                # Process each point
    

    then extract speed from extension:

    speed = None
    course = None
    for extension in point.extensions:
        if extension.tag == 'speed':
            speed = float(extension.text)
        if extension.tag == 'course':
            course = float(extension.text)
    

    then store the data in list:

    route_info.append({
        'latitude': point.latitude,
        'longitude': point.longitude,
        'elevation': point.elevation,
        'time': np.datetime64(point.time),
        'speed': speed,
        'course': course
    })
    

    It will give you the list of dictionaries with latitude, longitude, elevation, time, speed, and course for each track point in the GPX file.