tkrajina / gpxpy

gpx-py is a python GPX parser. GPX (GPS eXchange Format) is an XML based file format for GPS tracks.
Apache License 2.0
1.01k stars 223 forks source link

Route issues #238

Closed pramach closed 2 years ago

pramach commented 2 years ago

Hi When working with Routes (e.g. from Garmin Basecamp), some important information are not accessible, e.g. like the trp:TransportationMode, the (planned) trp:DepartureTime or the trp:CalculationMode.

As per example:

<?xml version="1.0"?> <gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1" xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:trp="http://www.garmin.com/xmlschemas/TripExtensions/v1" xmlns:adv="http://www.garmin.com/xmlschemas/AdventuresExtensions/v1" xmlns:prs="http://www.garmin.com/xmlschemas/PressureExtension/v1" xmlns:tmd="http://www.garmin.com/xmlschemas/TripMetaDataExtensions/v1" xmlns:vptm="http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensions/v1" xmlns:ctx="http://www.garmin.com/xmlschemas/CreationTimeExtension/v1" xmlns:gpxacc="http://www.garmin.com/xmlschemas/AccelerationExtension/v1" xmlns:gpxpx="http://www.garmin.com/xmlschemas/PowerExtension/v1" xmlns:vidx1="http://www.garmin.com/xmlschemas/VideoExtension/v1" creator="Garmin Desktop App" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/ActivityExtension/v1 http://www8.garmin.com/xmlschemas/ActivityExtensionv1.xsd http://www.garmin.com/xmlschemas/AdventuresExtensions/v1 http://www8.garmin.com/xmlschemas/AdventuresExtensionv1.xsd http://www.garmin.com/xmlschemas/PressureExtension/v1 http://www.garmin.com/xmlschemas/PressureExtensionv1.xsd http://www.garmin.com/xmlschemas/TripExtensions/v1 http://www.garmin.com/xmlschemas/TripExtensionsv1.xsd http://www.garmin.com/xmlschemas/TripMetaDataExtensions/v1 http://www.garmin.com/xmlschemas/TripMetaDataExtensionsv1.xsd http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensions/v1 http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensionsv1.xsd http://www.garmin.com/xmlschemas/CreationTimeExtension/v1 http://www.garmin.com/xmlschemas/CreationTimeExtensionsv1.xsd http://www.garmin.com/xmlschemas/AccelerationExtension/v1 http://www.garmin.com/xmlschemas/AccelerationExtensionv1.xsd http://www.garmin.com/xmlschemas/PowerExtension/v1 http://www.garmin.com/xmlschemas/PowerExtensionv1.xsd http://www.garmin.com/xmlschemas/VideoExtension/v1 http://www.garmin.com/xmlschemas/VideoExtensionv1.xsd">

Garmin International

...

GBR Hull - Liverpool - 307 km false Magenta Motorcycling Rotterdam - Hull Ferry Ferry 2021-10-06T23:00:39Z Shorter Distance Standard ... ...

Using Gpxpy 1.4.2 installed via pip on python 3.9 on OSX BigSur

Is there any possibility to get this Trip information too ?

An full example file can be provided on request.

tkrajina commented 2 years ago

They are accessible, but they are not stored in default gpx fields because it's not part of the gpx standard. It's stored in extensions.

From the documentation:

gpx.py preserves GPX extensions. They are stored as ElementTree DOM objects.

gpx.routes[0].points[0].extension...

pramach commented 2 years ago

Hi Thanks for fast reply, but from the debugger:

point = {GPXRoutePoint}[rtept{Rotterdam - Hull Ferry}:53.74347986653447,-0.273923529312015@None] ... extensions = {list: 2} [<Element '{http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint' at 0x7ff00cc63ef0>, <Element '{http://www.garmin.com/xmlschemas/GpxExtensions/v3}RoutePointExtension' at 0x7ff0269ad090>] 0 = {Element: 3} <Element '{http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint' at 0x7ff00cc63ef0> attrib = {dict: 0} {} len = {int} 0 tag = {str} '{http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint' tail = {str} '\n ' text = {str} '\n ' 1 = {Element: 1207} <Element '{http://www.garmin.com/xmlschemas/GpxExtensions/v3}RoutePointExtension' at 0x7ff0269ad090> attrib = {dict: 0} {} len = {int} 0 tag = {str} '{http://www.garmin.com/xmlschemas/GpxExtensions/v3}RoutePointExtension' tail = {str} '\n ' text = {str} '\n ' len = {int} 2 geoid_height = {NoneType} None .... name = (str) 'Rotterdam - Hull Ferry'

tkrajina commented 2 years ago

Not sure what's wrong, but here's a script I made for testing this:

g = gpxpy.parse(gpx) # gpx is your file
print(g.routes[0].points[0].extensions)
e = g.routes[0].points[0].extensions[0]
print(len(e))
node = e.getchildren()[1]
print(node.text)

The output is:

$ python q.py                                                                                                                                                     (v1.4.2)
[<Element {http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint at 0x105850bc0>, <Element {http://www.garmin.com/xmlschemas/GpxExtensions/v3}RoutePointExtension at 0x105850b80>]
3
Shorter Distance
$ python --version
Python 3.9.6
pramach commented 2 years ago

Thanks again

When running your test code, I get

[<Element '{http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint' at 0x7fa1e29f47c0>, <Element '{http://www.garmin.com/xmlschemas/GpxExtensions/v3}RoutePointExtension' at 0x7fa1c85bbe00>] 3 Traceback (most recent call last): File "/main.py", line 131, in button_click_action LoadTourData.load_tour_data(self) File "/modules/load_tour_data.py", line 27, in load_tour_data LoadTourData.load_tour_gpx(self, _selected_file) File "/modules/load_tour_data.py", line 94, in load_tour_gpx node = e.getchildren()[1] AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'`

I will do some further tests.

pramach commented 2 years ago

Route Test.TXT That's my test file, just rename as GPX.

Looks like that none of the extensions has a children.

My code looks like: ` gpx_file = open('Route.Test.GPX', 'r')

    gpx = gpxpy.parse(gpx_file)
    if len(gpx.routes) == 0:
        return None

    print(gpx.version)

    print(gpx.routes[0].points[0].extensions[0])
    e = gpx.routes[0].points[0].extensions[0]
    print(len(e))
    for node in e.getchildren():
        print(node.text)`

Output is:

1.1 <Element '{http://www.garmin.com/xmlschemas/TripExtensions/v1}ViaPoint' at 0x7fc2e54c14a0> 3 Traceback (most recent call last): ... File "/modules/load_tour_data.py", line 94, in load_tour_gpx for node in e.getchildren(): AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren'

tkrajina commented 2 years ago

That error doesn't say that there are no children, just that Element has no getchildren() method. But note that the third line says 3 and that's len(e). So there are 3 children nodes, you just need to iterate through them (without getchildren).

g = gpxpy.parse(gpx)
print(g.routes[0].points[0].extensions)
e = g.routes[0].points[0].extensions[0]
print(len(e))
for node in e:
    print(node.text)
pramach commented 2 years ago

You are absolutely right. Thanks a lot. Sorry for disturbing and thanks for the great library.