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

UTF8 Bug #130

Closed CoLoDot closed 4 years ago

CoLoDot commented 6 years ago

Hello,

I can't fix that bug. I searched into stackoverflow and all and it appears that the problem is coming from gpxpy lib because it used to work and i didn't change anything in my script.

If i missed an error please excuse me !

File "tes.py", line 27, in <module>
    gpx = gpxpy.parse(gpx_file)
  File "....Desktop/Python/Formation/Colo_fun/GPX/env/lib/python3.6/site-packages/gpxpy/__init__.py", line 34, in parse
    parser = mod_parser.GPXParser(xml_or_file)
  File "....Desktop/Python/Formation/Colo_fun/GPX/env/lib/python3.6/site-packages/gpxpy/parser.py", line 58, in __init__
    self.init(xml_or_file)
  File "....Desktop/Python/Formation/Colo_fun/GPX/env/lib/python3.6/site-packages/gpxpy/parser.py", line 70, in init
    text = xml_or_file.read() if hasattr(xml_or_file, 'read') else xml_or_file
  File "....Desktop/Python/Formation/Colo_fun/GPX/env/bin/../lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte

Here is my main script

#!/usr/bin/env python
# coding: utf-8

from os import listdir
from os.path import isfile, join
import matplotlib
matplotlib.use('TkAgg')
import gpxpy
import matplotlib.pyplot as plt

data_path = 'Fichiers_GPX'
data = [f for f in listdir(data_path) if isfile(join(data_path,f))]

gps_lat = [] # empty list init
gps_long = []

fig = plt.figure(facecolor = '0.05')
ax = plt.Axes(fig, [0., 0., 1., 1.], )
ax.set_aspect('equal')
ax.set_axis_off()
fig.add_axes(ax)

for activity in data:
    gpx_filename = join(data_path, activity)
    gpx_file = open(gpx_filename, 'r')
    gpx = gpxpy.parse(gpx_file)
    for track in gpx.tracks: # lat and long from a GPX file
        for segment in track.segments:
            for point in segment.points:
                gps_lat.append(point.latitude)
                gps_long.append(point.longitude)

    plt.plot(gps_long, gps_lat, color = 'deepskyblue', lw = 0.2, alpha = 0.8)        
    gps_lat = [] # empty list init
    gps_long = []

filename = data_path + '.jpg'
plt.savefig(filename, facecolor = fig.get_facecolor(), bbox_inches='tight', pad_inches=0, dpi=300)
zugspitze commented 6 years ago

@CoLoDot how did you fix it? I am getting the same error.

CoLoDot commented 6 years ago

Hi, I did not fix it, it only works when i am not using a repository with multiple files. It only works with the direct link to one file... So i still don't know for the moment why it cannot works with multiple files. If i find, i'll post the answer here.

Furthermore, you should use .png to export your file, i have a bug with .jpg.