diff --git a/gpxtrackposter/cli.py b/gpxtrackposter/cli.py index b20bc2a..75eadbe 100755 --- a/gpxtrackposter/cli.py +++ b/gpxtrackposter/cli.py @@ -193,6 +193,14 @@ def main() -> None: default=1.0, help="min distance by km for track filter", ) + args_parser.add_argument( + "--activity", + dest="activity", + metavar="ACTIVITY", + type=str, + default="all", + help="Filter tracks by activity; e.g. 'running' (default: all activities)", + ) args_parser.add_argument( "--with-animation", dest="with_animation", @@ -228,6 +236,7 @@ def main() -> None: loader.special_file_names = args.special loader.set_min_length(args.min_distance * Units().km) + loader.set_activity(args.activity) if args.clear_cache: print("Clearing cache...") loader.clear_cache() diff --git a/gpxtrackposter/track.py b/gpxtrackposter/track.py index 85ff0a4..3874983 100644 --- a/gpxtrackposter/track.py +++ b/gpxtrackposter/track.py @@ -26,10 +26,11 @@ class Track: Attributes: file_names: Basename of a given file passed in load_gpx. polylines: Lines interpolated between each coordinate. - start_time: Activity start time. - end_time: Activity end time. - length: Length of the track (2-dimensional). - self.special: True if track is special, else False. + _start_time: Activity start time. + _end_time: Activity end time. + _length_meters: Length of the track (2-dimensional). + special: True if track is special, else False. + activity_type: Activity type Methods: load_gpx: Load a GPX file into the current track. @@ -48,12 +49,14 @@ def __init__(self) -> None: # within a thread (which would create a second unit registry!) self._length_meters = 0.0 self.special = False + self.activity_type = None def load_gpx(self, file_name: str, timezone_adjuster: typing.Optional[TimezoneAdjuster]) -> None: """Load the GPX file into self. Args: - file_name: GPX file to be loaded . + file_name: GPX file to be loaded. + timezone_adjuster: timezone adjuster Raises: TrackLoadError: An error occurred while parsing the GPX file (empty or bad format). @@ -139,6 +142,7 @@ def _load_gpx_data(self, gpx: gpxpy.gpx.GPX, timezone_adjuster: typing.Optional[ for s in t.segments: line = [s2sphere.LatLng.from_degrees(p.latitude, p.longitude) for p in s.points] self.polylines.append(line) + self.activity_type = gpx.tracks[0].type.lower() def append(self, other: "Track") -> None: """Append other track to self.""" diff --git a/gpxtrackposter/track_loader.py b/gpxtrackposter/track_loader.py index 1e5f25b..600cc1b 100644 --- a/gpxtrackposter/track_loader.py +++ b/gpxtrackposter/track_loader.py @@ -53,10 +53,11 @@ class TrackLoader: """Handle the loading of tracks from cache and/or GPX files Attributes: - min_length: All tracks shorter than this value are filtered out. + _min_length: All tracks shorter than this value are filtered out. special_file_names: Tracks marked as special in command line args year_range: All tracks outside of this range will be filtered out. cache_dir: Directory used to store cached tracks + _activity_type: Only gpx files with activity type are considered Methods: clear_cache: Remove cache directory @@ -71,6 +72,7 @@ def __init__(self, workers: typing.Optional[int]) -> None: self.cache_dir: typing.Optional[str] = None self.strava_cache_file = "" self._cache_file_names: typing.Dict[str, str] = {} + self._activity_type: str = "all" def set_cache_dir(self, cache_dir: str) -> None: self.cache_dir = cache_dir @@ -87,6 +89,9 @@ def clear_cache(self) -> None: def set_min_length(self, min_length: pint.quantity.Quantity) -> None: self._min_length = min_length + def set_activity(self, activity_type: str) -> None: + self._activity_type = activity_type.lower() + def load_tracks(self, base_dir: str) -> typing.List[Track]: """Load tracks base_dir and return as a List of tracks""" file_names = list(self._list_gpx_files(base_dir)) @@ -169,7 +174,10 @@ def _filter_and_merge_tracks(self, tracks: typing.List[Track]) -> typing.List[Tr # merge tracks that took place within one hour tracks = self._merge_tracks(tracks) # filter out tracks with length < min_length - return [t for t in tracks if t.length() >= self._min_length] + tracks = [t for t in tracks if t.length() >= self._min_length] + # filter out tracks with wrong activity type + tracks = [t for t in tracks if t.activity_type == self._activity_type or self._activity_type == "all"] + return tracks @staticmethod def _merge_tracks(tracks: typing.List[Track]) -> typing.List[Track]: