flopp / GpxTrackPoster

Create a visually appealing poster from your GPX tracks
MIT License
411 stars 50 forks source link

TypeError: can't compare offset-naive and offset-aware datetimes #80

Closed lowtower closed 3 years ago

lowtower commented 3 years ago

I try to generate a poster with some gpx files extracted from my garmin account. I have downloaded the files using the proposed python script garmin-connect-export. Now, I get the following error:

Traceback (most recent call last):
  File "~/GpxTrackPoster/venv/bin/create_poster", line 8, in <module>
    sys.exit(main())
  File "~/GpxTrackPoster/venv/lib/python3.7/site-packages/gpxtrackposter/cli.py", line 217, in main
    tracks = loader.load_tracks(args.gpx_dir)
  File "~/GpxTrackPoster/venv/lib/python3.7/site-packages/gpxtrackposter/track_loader.py", line 114, in load_tracks
    return self._filter_and_merge_tracks(tracks)
  File "~/GpxTrackPoster/venv/lib/python3.7/site-packages/gpxtrackposter/track_loader.py", line 169, in _filter_and_merge_tracks
    tracks = self._merge_tracks(tracks)
  File "~/GpxTrackPoster/venv/lib/python3.7/site-packages/gpxtrackposter/track_loader.py", line 176, in _merge_tracks
    tracks = sorted(tracks, key=lambda t1: t1.start_time())
TypeError: can't compare offset-naive and offset-aware datetimes

Is something wrong with my gpx files? As said, they all come from the same source.

Thanks in advance, LT.

yihong0618 commented 3 years ago

@lowtower

Can you add this line in file timezone_adjuster.py for test? and try again.

I am thinking some gpx we can not get tz_name info.

time.replace(tzinfo=timezone.utc)

also need to add import line

from datetime import timezone

from datetime import timezone

class TimezoneAdjuster:
    _timezonefinder: typing.Optional[timezonefinder.TimezoneFinder] = None

    def __init__(self) -> None:
        if not TimezoneAdjuster._timezonefinder:
            TimezoneAdjuster._timezonefinder = timezonefinder.TimezoneFinder()

    @classmethod
    def adjust(cls, time: datetime.datetime, latlng: s2sphere.LatLng) -> datetime.datetime:
        # If a timezone is set, there's nothing to do.
        if time.utcoffset():
            return time
        assert cls._timezonefinder
        tz_name = cls._timezonefinder.timezone_at(lat=latlng.lat().degrees, lng=latlng.lng().degrees)
        if tz_name is None:
            time.replace(tzinfo=timezone.utc)
            return time
        tz = pytz.timezone(tz_name)
        tz_time = time.astimezone(tz)
        return tz_time

image

lowtower commented 3 years ago

Thanks for Your quick reply!

The modification did work! I get some errors for only a few gpx files such as:

Error while loading ~/2020/activity_5702364393.gpx: Track has no start or end time.

I have to have a closer look at these files.

Thanks a lot, cheers, LT

yihong0618 commented 3 years ago

Thanks for Your quick reply!

The modification did work! I get some errors for only a few gpx files such as:

Error while loading ~/2020/activity_5702364393.gpx: Track has no start or end time.

I have to have a closer look at these files.

Thanks a lot, cheers, LT

@lowtower

Maybe this gpx has something wrong, you can open it to see if it contains info or not.

You can delete the activity_5702364393.gpx file from the folder to see what happens.

If you have other gpx files like this, maybe you need to delete them too.

yihong0618 commented 3 years ago

@flopp @lowtower I think I can take a pr to fix this issue later~

lowtower commented 3 years ago

I checked the relevant gpx files - they were not "running" files but "cardio" without time info - so the error messages are reasonable.

Thanks for Your help and filing the pr!