Repository: mie-lab/trackintel

Issue:
Raise error in gpx reader if path does not exist
We had a problem that the gpx reader function still executed even though the path did not exist, and then an error was only thrown in the last line `pd.concat(pfs_list, ignore_index=True)` with the error `no objects to concatenate`. There should be an error `Path does not exist` or so

Discussion:

Patch:
diff --git a/trackintel/io/dataset_reader.py b/trackintel/io/dataset_reader.py
index 25c4cc3..c2c6b2a 100644
--- a/trackintel/io/dataset_reader.py
+++ b/trackintel/io/dataset_reader.py
@@ -700,10 +700,13 @@ def read_gpx(path):
     Positionfixes
     """
     pattern = os.path.join(path, "*.gpx")
+    # sorted to make result deterministic
+    files = sorted(glob.glob(pattern))
+    if not files:
+        raise FileNotFoundError(f'Found no gpx files in path "{path}"')
     pfs_list = []
     track_fid_offset = 0
-    # sorted to make result deterministic
-    for file in sorted(glob.glob(pattern)):
+    for file in files:
         pfs = _read_single_gpx_file(file)
         # give each track an unique ID
         pfs["track_fid"] += track_fid_offset


Test patch:
diff --git a/tests/io/test_dataset_reader.py b/tests/io/test_dataset_reader.py
index d963554..577d7c8 100644
--- a/tests/io/test_dataset_reader.py
+++ b/tests/io/test_dataset_reader.py
@@ -268,3 +268,8 @@ class TestGpx_reader:
         }
         pfs_test = Positionfixes(data, geometry="geometry", crs=4326)
         assert_geodataframe_equal(pfs, pfs_test)
+
+    def test_missing_files(self):
+        """Test if useful message is output if directory has no gpx files"""
+        with pytest.raises(FileNotFoundError, match="Found no gpx files"):
+            read_gpx(os.path.join("tests", "data"))