tkrajina/gpxpy

Tracks with extensions can't be copied to a fresh GPX

Closed this issue · 1 comments

I'd like to copy some tracks that have Garmin extensions from a GPX file into a new file.

Trying this results in non-well-formed XML output...

import gpxpy

with open("gpxx-in.gpx", 'r') as gpx_file:
    input_gpx = gpxpy.parse(gpx_file)

output_gpx = gpxpy.gpx.GPX()
output_gpx.tracks = [t.clone() for t in input_gpx.tracks]

with open("gpxx-out.gpx", 'w') as gpx_file:
    print(output_gpx.to_xml(), file=gpx_file)

This:

    <extensions>
      <gpxx:TrackExtension>
        <gpxx:DisplayColor>DarkGray</gpxx:DisplayColor>
      </gpxx:TrackExtension>
    </extensions>

gpxx-in.gpx.txt

Gets converted to

    <extensions>
      <http://www.garmin.com/xmlschemas/GpxExtensions/v3:TrackExtension>
        <http://www.garmin.com/xmlschemas/GpxExtensions/v3:DisplayColor>DarkGray</http://www.garmin.com/xmlschemas/GpxExtensions/v3:DisplayColor>
      </http://www.garmin.com/xmlschemas/GpxExtensions/v3:TrackExtension>
    </extensions>

gpxx-out.gpx.txt

Which causes XML parsers to break.

I'm able to get well-formed XML output if I do this instead of creating a fresh GPX object...

output_gpx = input_gpx.clone()

But this seems like a hack (what else am I carrying over from the old file?). Is there a better way to handle this?

I'm using gpxpy 1.3.4 without lxml on Python 3.6.1

(By the way, thanks a lot for making gpxpy! It has enabled me to do a lot of things that had previously frustrated me that I couldn't do easily in commercial GPS applications)

Along with extensions, you should also change the nsmap and schema_locations fields (https://github.com/tkrajina/gpxpy/blob/master/gpxpy/gpx.py#L1995)