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
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With