From fbe8125a72193d2b63ed5ef3f9de076fbb61db13 Mon Sep 17 00:00:00 2001
From: Yun-Wu <ywu910318@gmail.com>
Date: Fri, 29 Nov 2024 16:28:34 +0800
Subject: [PATCH] Fix crs in get_displacement (#342)

---
 ecoscope/base/base.py | 13 +++++--------
 tests/test_base.py    |  4 ++--
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/ecoscope/base/base.py b/ecoscope/base/base.py
index 8dfc7d10..ba82a0fb 100644
--- a/ecoscope/base/base.py
+++ b/ecoscope/base/base.py
@@ -1,4 +1,5 @@
 import warnings
+from functools import cached_property
 
 import geopandas as gpd
 import numpy as np
@@ -13,7 +14,6 @@
     RelocsSpeedFilter,
     TrajSegFilter,
 )
-from functools import cached_property
 
 
 class EcoDataFrame(gpd.GeoDataFrame):
@@ -352,7 +352,7 @@ def from_relocations(cls, gdf, *args, **kwargs):
         assert isinstance(gdf, Relocations)
         assert {"groupby_col", "fixtime", "geometry"}.issubset(gdf)
 
-        if kwargs.get("copy") is not False:
+        if kwargs.get("copy"):
             gdf = gdf.copy()
 
         gdf = EcoDataFrame(gdf)
@@ -367,14 +367,11 @@ def get_displacement(self):
         """
         Get displacement in meters between first and final fixes.
         """
-
         if not self["segment_start"].is_monotonic_increasing:
             self = self.sort_values("segment_start")
-
-        gs = self.geometry.iloc[[0, -1]]
-        start, end = gs.to_crs(gs.estimate_utm_crs())
-
-        return start.distance(end)
+        start = self.geometry.iloc[0].coords[0]
+        end = self.geometry.iloc[-1].coords[1]
+        return Geod(ellps="WGS84").inv(start[0], start[1], end[0], end[1])[2]
 
     def get_tortuosity(self):
         """
diff --git a/tests/test_base.py b/tests/test_base.py
index 7277088f..e39659a5 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -77,7 +77,7 @@ def test_trajectory_properties(movebank_relocations):
 def test_displacement_property(movebank_relocations):
     trajectory = ecoscope.base.Trajectory.from_relocations(movebank_relocations)
     expected = pd.Series(
-        [2633.760505, 147749.545621],
+        [2633.9728734217356, 147960.93352796172],
         index=pd.Index(["Habiba", "Salif Keita"], name="groupby_col"),
     )
     pd.testing.assert_series_equal(
@@ -89,7 +89,7 @@ def test_displacement_property(movebank_relocations):
 def test_tortuosity(movebank_relocations):
     trajectory = ecoscope.base.Trajectory.from_relocations(movebank_relocations)
     expected = pd.Series(
-        [51.65388458528601, 75.96149479123005],
+        [51.64971990070838, 75.85297059494991],
         index=pd.Index(["Habiba", "Salif Keita"], name="groupby_col"),
     )
     pd.testing.assert_series_equal(