Repository: mie-lab/trackintel
Pull request #68 (2020-11-22 23:11:47)

douglas peucker function and test
- replaced the simplify function in simplification.cutil to the one in shapely

- modified the test function

codecov[bot]: # [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=h1) Report
> Merging [#68](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=desc) (fcc9759) into [master](https://codecov.io/gh/mie-lab/trackintel/commit/7ef8d4cc7da53d4b9c197096621ea8c57f7e1540?el=desc) (7ef8d4c) will **increase** coverage by `0.39%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/mie-lab/trackintel/pull/68/graphs/tree.svg?width=650&height=150&src=pr&token=ITDZEEE1MO)](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #68      +/-   ##
==========================================
+ Coverage   52.42%   52.81%   +0.39%     
==========================================
  Files          30       30              
  Lines        1280     1278       -2     
  Branches      207      208       +1     
==========================================
+ Hits          671      675       +4     
+ Misses        544      539       -5     
+ Partials       65       64       -1     
```


| [Impacted Files](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [trackintel/preprocessing/triplegs.py](https://codecov.io/gh/mie-lab/trackintel/pull/68/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9wcmVwcm9jZXNzaW5nL3RyaXBsZWdzLnB5) | `68.96% <100.00%> (+2.29%)` | :arrow_up: |
| [trackintel/preprocessing/staypoints.py](https://codecov.io/gh/mie-lab/trackintel/pull/68/diff?src=pr&el=tree#diff-dHJhY2tpbnRlbC9wcmVwcm9jZXNzaW5nL3N0YXlwb2ludHMucHk=) | `90.90% <0.00%> (+5.19%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=footer). Last update [7ef8d4c...fcc9759](https://codecov.io/gh/mie-lab/trackintel/pull/68?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/trackintel/preprocessing/triplegs.py b/trackintel/preprocessing/triplegs.py
index 9a091ac..f0e19e7 100644
--- a/trackintel/preprocessing/triplegs.py
+++ b/trackintel/preprocessing/triplegs.py
@@ -1,27 +1,26 @@
 import ast
 import copy
 import datetime
-
-import pandas as pd
 import pandas as pd
-from shapely.geometry import LineString
-from simplification.cutil import simplify_coords  # , simplify_coordsvw
 
 
-def smoothen_triplegs(triplegs, method='douglas-peucker', epsilon = 1.0):
+
+def smoothen_triplegs(triplegs, method='douglas-peucker', tolerance=1.0):
     """reduces number of points while retaining structure of tripleg
     Parameters
     ----------
     triplegs: shapely file
         triplegs to be reduced
     method: method used to smoothen
-        only one method available so far
-    epsilon: float
-        slack parameter, higher epsilon means removing more points
+        only the douglas-peucker method is available so far
+    tolerance: float
+        a higher tolerance removes more points; the units of tolerance are the same as the projection of the input geometry
     """
     input_copy = copy.deepcopy(triplegs)
-    input_copy.geometry = [LineString(ast.literal_eval(str(simplify_coords(input_copy.geometry[i].coords, epsilon))))
-                           for i in range(len(input_copy.geometry))]
+    origin_geom = input_copy.geom
+    simplified_geom = origin_geom.simplify(tolerance, preserve_topology=False)
+    input_copy.geom = simplified_geom
+
     return input_copy
 
 def _temp_trip_stack_has_tripleg(temp_trip_stack):


Test patch:
diff --git a/tests/preprocessing/test_triplegs.py b/tests/preprocessing/test_triplegs.py
index 379f7f9..6c99ca4 100644
--- a/tests/preprocessing/test_triplegs.py
+++ b/tests/preprocessing/test_triplegs.py
@@ -1,9 +1,10 @@
 import os
 
 import pandas as pd
-
+import trackintel as ti
 from trackintel.io.dataset_reader import read_geolife
 from trackintel.preprocessing.triplegs import generate_trips
+from trackintel.preprocessing.triplegs import smoothen_triplegs
 
 
 class TestGenerate_trips():
@@ -26,3 +27,24 @@ class TestGenerate_trips():
 
         spts, tpls, trips = generate_trips(spts, tpls, gap_threshold=15, id_offset=0)
         pd.testing.assert_frame_equal(trips_loaded, trips)
+
+    def test_douglas_peucker_algorithm_reduce_tripleg_length(self):
+        """
+        test the douglas peucker algorithm in simplifying triplegs
+        """
+        orig_file = 'tests/data/triplegs_with_too_many_points_test.csv'
+        tpls = ti.read_triplegs_csv(orig_file, sep=';')
+        tpls_smoothed = smoothen_triplegs(tpls, tolerance=0.0001)
+        line1 = tpls.iloc[0].geom
+        line1_smoothed = tpls_smoothed.iloc[0].geom
+        line2 = tpls.iloc[1].geom
+        line2_smoothed = tpls_smoothed.iloc[1].geom
+
+        print(line1)
+        print(line1_smoothed)
+        assert line1.length == line1_smoothed.length
+        assert line2.length == line2_smoothed.length
+        assert len(line1.coords) == 10
+        assert len(line2.coords) == 7
+        assert len(line1_smoothed.coords) == 4
+        assert len(line2_smoothed.coords) == 3
\ No newline at end of file