Skip to content

Instantly share code, notes, and snippets.

@joaofig
Created January 9, 2024 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaofig/53a796ee2f2fead3c1796f68917ad94e to your computer and use it in GitHub Desktop.
Save joaofig/53a796ee2f2fead3c1796f68917ad94e to your computer and use it in GitHub Desktop.
Converts a trajectory segment to a node-based trajectory, recomputing traversal times.
def segment_to_trajectory(segment: list[LatLon],
dt: float,
t0: float) -> Trajectory:
lat = np.array([point.lat for point in segment])
lon = np.array([point.lon for point in segment])
time = np.zeros_like(lat)
seg_lens = vec_haversine(lat[1:], lon[1:], lat[:-1], lon[:-1])
avg_speed = np.sum(seg_lens) / dt
time[0] = t0
time[1:] = np.cumsum(seg_lens / avg_speed * 1000.0) + t0
return Trajectory(lat=lat, lon=lon, time=time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment