Browse Source

log-location script: added --distance-threshold option

Fabian Peter Hammerle 6 years ago
parent
commit
fea9485b2b
2 changed files with 47 additions and 9 deletions
  1. 30 9
      log-location
  2. 17 0
      termux_log_location/__init__.py

+ 30 - 9
log-location

@@ -2,7 +2,9 @@
 
 import os
 import sys
-import termux_log_location
+import termux_log_location as tll
+
+LAST_YAML_SYMLINK_NAME = 'last.yml'
 
 
 def symlink(src, dst, force=False):
@@ -11,15 +13,27 @@ def symlink(src, dst, force=False):
     os.symlink(src=src, dst=dst)
 
 
-def compute(target_dir_path):
-    cur = termux_log_location.Position.from_termux_location()
-    if cur:
-        cur_path = cur.save_yaml(target_dir_path)
-        symlink(
-            src=os.path.relpath(cur_path, start=target_dir_path),
-            dst=os.path.join(target_dir_path, 'last.yml'),
-            force=True,
+def load_last(target_dir_path):
+    last_path = os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME)
+    if os.path.exists(last_path):
+        return tll.Position.load_yaml(
+            path=os.path.realpath(last_path),
         )
+    else:
+        return None
+
+
+def compute(target_dir_path, distance_threshold_metres):
+    cur = tll.Position.from_termux_location()
+    if cur:
+        last = load_last(target_dir_path)
+        if not last or tll.Position.haversine_distance_metres(last, cur) > distance_threshold_metres:
+            cur_path = cur.save_yaml(target_dir_path)
+            symlink(
+                src=os.path.relpath(cur_path, start=target_dir_path),
+                dst=os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME),
+                force=True,
+            )
 
 
 def _init_argparser():
@@ -31,6 +45,13 @@ def _init_argparser():
         dest='target_dir_path',
         default='.',
     )
+    argparser.add_argument(
+        '--distance-threshold',
+        metavar='METRES',
+        dest='distance_threshold_metres',
+        default=0,
+        type=float,
+    )
     return argparser
 
 

+ 17 - 0
termux_log_location/__init__.py

@@ -76,6 +76,23 @@ class Position(Location):
                 altitude=loc_attr['altitude'],
             )
 
+    @classmethod
+    def load_yaml(cls, path):
+        name = os.path.basename(path)
+        assert name.endswith('Z.yml'), path
+        ts = pytz.utc.localize(dt.datetime.strptime(
+            name[:-5],
+            '%Y%m%dT%H%M%S.%f',
+        ))
+        with open(path, 'r') as f:
+            attr = yaml.load(f.read())
+        return cls(
+            timestamp=ts,
+            latitude=attr['lat'],
+            longitude=attr['lon'],
+            altitude=attr['alt'],
+        )
+
     def save_yaml(self, target_dir_path):
         path = os.path.join(
             target_dir_path,