Skip to content

Commit

Permalink
Add NSD Calculation (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Wu authored Oct 21, 2024
1 parent 2ceac38 commit 71b0c76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ecoscope/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Binary file modified tests/test_output/geofence_crossing_point.feather
Binary file not shown.

0 comments on commit 71b0c76

Please sign in to comment.