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
987 stars 223 forks source link

List of xsi:schemaLocation cannot be appended #210

Open ekspla opened 3 years ago

ekspla commented 3 years ago

I am very new to gpxpy and want to have a gpx file similar to those from Garmin. The xsi:schemaLocation should be looked like this:

xsi:schemaLocation="http://www.topografix.com/GPX/1/1 
http://www.topografix.com/GPX/1/1/gpx.xsd 
http://www.garmin.com/xmlschemas/GpxExtensions/v3 
http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd 
http://www.garmin.com/xmlschemas/TrackPointExtension/v1 
http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"

The following script generates the first two of the above mentioned schemaLocation.

gpx = gpxpy.gpx.GPX()
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
gpx.nsmap["gpxtpx"] = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
gpx.nsmap["gpxx"] = "http://www.garmin.com/xmlschemas/GpxExtensions/v3"
print(gpx.to_xml('1.1'))

However, appending the rest to the list of schema_locations fails. It generates only the list of the added four.

gpx.schema_locations += ['http://www.garmin.com/xmlschemas/GpxExtensions/v3',
                         'http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd',
                         'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
                         'http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd']

Is this an intentional behaviour that the list cannot be appended? We can, of course, write the complete list like this.

gpx.schema_locations = ['http://www.topografix.com/GPX/1/1',
                         'http://www.topografix.com/GPX/1/1/gpx.xsd',
                         'http://www.garmin.com/xmlschemas/GpxExtensions/v3',
                         'http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd',
                         'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
                         'http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd']

I am using Python 3.6 and gpxpy 1.4.2.

ekspla commented 3 years ago

I found that the default list of xsi:schemaLocation is generated after calling gpx.to_xml(). That is why the list can neither be appended nor extended before the call.

ekspla commented 3 years ago

I think it would be nice to check if schema_locations of topografix are given when 'to_xml()' is called.

For example, in the code of def to_xml() in gpx.py (lines from 2698, version 1.4.2):

        default_schema_locations = [
            p.format(version_path) for p in (
                'http://www.topografix.com/GPX/{0}',
                'http://www.topografix.com/GPX/{0}/gpx.xsd',
            )
        ]
        if not set(self.schema_locations) >= set(default_schema_locations):
            self.schema_locations = default_schema_locations + self.schema_locations