diff --git a/ecoscope/base/base.py b/ecoscope/base/base.py index 38234a45..776109b2 100644 --- a/ecoscope/base/base.py +++ b/ecoscope/base/base.py @@ -412,6 +412,7 @@ def _create_trajsegments(gdf): "heading": track_properties.heading, "geometry": shapely.linestrings(coords), "junk_status": gdf.junk_status, + "nsd": track_properties.nsd, }, crs=4326, index=gdf.index, @@ -625,6 +626,13 @@ def dist_meters(self): _, _, distance = self.inverse_transformation return distance + @property + def nsd(self): + start_point = df["geometry"].iloc[0] + geod = Geod(ellps="WGS84") + geod_displacement = [geod.inv(start_point.x, start_point.y, geo.x, geo.y)[2] for geo in df["_geometry"]] + return [(x**2) / (1000 * 2) for x in geod_displacement] + @property def timespan_seconds(self): return (df["_fixtime"] - df["fixtime"]).dt.total_seconds() diff --git a/tests/test_base.py b/tests/test_base.py index a2c21677..18c10e26 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -50,6 +50,30 @@ def test_relocations_from_gdf_preserve_fields(er_io): gpd.testing.assert_geodataframe_equal(relocations, ecoscope.base.Relocations.from_gdf(relocations)) +def test_trajectory_properties(movebank_relocations): + trajectory = ecoscope.base.Trajectory.from_relocations(movebank_relocations) + + assert "groupby_col" in trajectory + assert "segment_start" in trajectory + assert "segment_end" in trajectory + assert "timespan_seconds" in trajectory + assert "speed_kmhr" in trajectory + assert "heading" in trajectory + assert "geometry" in trajectory + assert "junk_status" in trajectory + assert "nsd" in trajectory + + trajectory = trajectory.loc[trajectory.groupby_col == "Habiba"].head(5) + + expected_nsd = pd.Series( + [0.446425, 1.803153, 2.916319, 28.909629, 72.475410], + dtype=np.float64, + index=pd.Index([368706890, 368706891, 368706892, 368706893, 368706894], name="event-id"), + name="nsd", + ) + pandas.testing.assert_series_equal(trajectory["nsd"], expected_nsd) + + def test_displacement_property(movebank_relocations): trajectory = ecoscope.base.Trajectory.from_relocations(movebank_relocations) expected = pd.Series( diff --git a/tests/test_output/geofence_crossing_point.feather b/tests/test_output/geofence_crossing_point.feather index f0ed3ad3..cd4bf130 100644 Binary files a/tests/test_output/geofence_crossing_point.feather and b/tests/test_output/geofence_crossing_point.feather differ