4

I have a few gpx files which I want to parse and then feed into a GIS format. I've downloaded gpxpy because I need some of its functions rather than just wanting to extract the lat and lon from the files. But when I make a parser

import gpxpy
p = gpxpy.parse(path_to_gpx_file)

it gives me this:

ERROR:root:not well-formed (invalid token): line 1, column 2
Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 196, in parse
    self.xml_parser = XMLParser(self.xml)
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 43, in __init__
    self.dom = mod_minidom.parseString(xml)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
ExpatError: not well-formed (invalid token): line 1, column 2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\__init__.py", line 32, in parse
    return parser.parse()
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 219, in parse
    raise mod_gpx.GPXXMLSyntaxException('Error parsing XML: %s' % str(e), e)
GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 1, column 2

After spending some time googling, this lead me to suspect that there are errors in the xml structure. However, I can't spot them.

I've used http://www.validome.org/xml/validate/ to validate the files but it says they're valid.

This is what my gpx files look like. I've reduced this one to include only 3 trackpoints, but it's still giving me the same error as the full (35k lines)file.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
     xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
     xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1"
     xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
     creator="Dakota 20" version="1.1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://www8.garmin.com/xmlschemas/GpxExtensionsv3.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">
    <metadata>
        <link href="http://www.garmin.com">
            <text>Garmin International</text>
        </link>
        <time>2015-03-01T16:59:53Z</time>
    </metadata>
    <trk>
        <name>SKI1</name>
        <extensions>
            <gpxx:TrackExtension></gpxx:TrackExtension>
        </extensions>
        <trkseg>
            <trkpt lat="43.3737357836" lon="130.0217922572">
                <ele>166.26</ele>
                <time>2015-03-01T08:34:40Z</time>
            </trkpt>
            <trkpt lat="43.3737673834" lon="130.0218102783">
                <ele>166.22</ele>
                <time>2015-03-01T08:34:42Z</time>
            </trkpt>
            <trkpt lat="43.3737869971" lon="130.0217925087">
                <ele>166.78</ele>
                <time>2015-03-01T08:35:02Z</time>
            </trkpt>
        </trkseg>
    </trk>
</gpx>

EDIT maybe should add: using python 2.6, gpxpy 0.9.8, pycharm 3.1.1

Menno
  • 241
  • 2
  • 8
  • I think the xml is fine. I think the problem is that it is not finding the file specified in `path_to_gpx_file`. Either the file is not really there or the path and filename is not specified correctly (e.g. a problem with directory or file specifiers like / or \ or \\\) – Al Lelopath Mar 19 '15 at 17:26
  • Also might be a bug in gpxpy, read here: http://stackoverflow.com/questions/16201548/python-typeerror-file-object-has-no-attribute-getitem – Al Lelopath Mar 19 '15 at 17:41
  • The file is sound, so that's not the problem. However, I figured it out, check my answer. – Menno Mar 23 '15 at 09:02

2 Answers2

10

It isn't really documented anywhere (in my opinion), so I'll post it here. Instead of letting the parser try to open the file, generate a file object first, and feed that to the parser, so:

import gpxpy
f = open(path_to_gpx_file, 'r')
p = gpxpy.parse(f)

I don't know why I didn't try that before...

Menno
  • 241
  • 2
  • 8
3

Here is the current documentation for the parse function:

In [4]: import gpxpy

In [5]: gpxpy.parse?
Signature: gpxpy.parse(xml_or_file, parser=None)
Docstring:
Parse xml (string) or file object. This is just an wrapper for
GPXParser.parse() function.

parser may be 'lxml', 'minidom' or None (then it will be automatically
detected, lxml if possible).

xml_or_file must be the xml to parse or a file-object with the XML.
File:      ~/venv/gpxpy-test/lib/python3.4/site-packages/gpxpy/__init__.py
Type:      function

It specifies you need to pass in an xml string or a file object, but doesn't say anything about accepting a filename path.

b-jazz
  • 859
  • 1
  • 7
  • 16