Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter by activity type #89

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gpxtrackposter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 9 additions & 5 deletions gpxtrackposter/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand Down Expand Up @@ -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."""
Expand Down
12 changes: 10 additions & 2 deletions gpxtrackposter/track_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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]:
Expand Down