Trajectory¶
trajectory ¶
Trajectory ¶
Trajectory(positions: Positions, rotations: Rotations | None = None, timestamps: ndarray | None = None, name: str = '', path_lengths: ndarray | None = None, velocity_xyz: ndarray | None = None, sorting: Sorting = TIME)
Class representing a trajectory containing synchronized position, orientation, and time data.
Attributes:
-
positions(Positions) –Container for spatial coordinates and coordinate reference system (EPSG) data.
-
rotations(Rotations) –Container for orientation data (quaternions), or None if not provided.
-
timestamps(ndarray) –A 1D array of timestamps corresponding to each pose.
-
name(str) –An identifier string for the trajectory.
-
path_lengths(ndarray) –A 1D array of cumulative path lengths starting from zero.
-
sorting(Sorting) –The current sorting strategy (Sorting.TIME or Sorting.ARC_LENGTH).
Initialize a Trajectory object.
Parameters:
-
positions(Positions) –Container for spatial coordinates and coordinate reference system (EPSG) data.
-
rotations(Rotations | None, default:None) –Container for orientation data (quaternions). Defaults to None.
-
timestamps(ndarray | None, default:None) –Array of timestamps. If None, a range index is used.
-
name(str, default:'') –Name of the trajectory. Defaults to generic counter name.
-
path_lengths(Union[ndarray, None], default:None) –Pre-calculated path lengths. If None, they are computed from xyz.
-
velocity_xyz(Union[ndarray, None], default:None) –Pre-calculated 3D velocities. If None, they are computed via gradient.
-
sorting(Sorting, default:TIME) –Definition of the sorting logic (TIME or ARC_LENGTH). Defaults to Sorting.TIME.
Raises:
-
TrajectoryError–If the number of positions and rotations do not match.
Source code in trajectopy\core\trajectory.py
is_unix_time
property
¶
Checks if the supplied trajectory has (likely) unix timestamps as seconds.
total_length
property
¶
Returns the total cumulative path length of the trajectory in meters.
sorting
property
¶
Returns the current sorting strategy (read-only).
To change sorting, use set_sorting() which physically reorders the data.
index
property
¶
Returns the independent variable currently parameterizing the trajectory.
This is either the Timestamp vector or the Path Length vector, depending on self.sorting.
datetimes
property
¶
Returns the timestamps converted to Pandas datetime objects (unit='s').
index_unit
property
¶
Returns the unit string of the current index ('s' for Time, 'm' for Path Length).
index_label
property
¶
Returns the label string of the current index (e.g., 'time [s]').
data_rate
property
¶
Calculates the average data rate (frequency in Hz) based on timestamp differences.
velocity_xyz
property
writable
¶
Returns the 3D velocity vectors. If not set manually, they are computed via gradient of the positions over time.
absolute_velocity
property
¶
Returns the norm (magnitude) of the 3D velocity vectors.
xyz
property
¶
Returns the XYZ coordinates.
Since trajectory data is always stored in sorted order (by time or arc length),
this is equivalent to self.positions.xyz.
se3
property
writable
¶
Returns a list of SE3 poses (4x4 homogeneous transformation matrices).
from_file
classmethod
¶
from_file(filename: str, io_stream: bool = False) -> Trajectory
Create a trajectory instance from a file.
The file is expected to be a CSV-like format. It handles extraction of
timestamps, xyz positions, rotations, path lengths, and velocities via ascii trajectory reader.
Parameters:
-
filename(str) –Path to the file or string content if io_stream is True.
-
io_stream(bool, default:False) –If True,
filenameis treated as the raw string content of the file/stream. Defaults to False.
Returns:
-
Trajectory(Trajectory) –The loaded trajectory object.
Source code in trajectopy\core\trajectory.py
from_arrays
classmethod
¶
from_arrays(xyz: ndarray, quat: ndarray | None = None, rpy: ndarray | None = None, epsg: int = 0, **kwargs) -> Trajectory
Factory: Handles creation from raw numpy arrays.
Source code in trajectopy\core\trajectory.py
__str__ ¶
Returns a formatted string summary of the trajectory, including name, length, EPSG, and data rate.
Source code in trajectopy\core\trajectory.py
__len__ ¶
__eq__ ¶
__eq__(other: Trajectory) -> bool
Checks equality between two trajectories using np.allclose for numerical arrays.
Compares positions, rotations, timestamps, path lengths, velocities, and names.
Source code in trajectopy\core\trajectory.py
copy ¶
copy() -> Trajectory
init_path_lengths ¶
Computes cumulative path lengths based on Euclidean distances between consecutive local coordinates.
set_sorting ¶
Changes the sorting strategy and physically reorders all trajectory data.
After calling this method, all data arrays (positions, rotations, timestamps, path_lengths, velocities) will be reordered according to the new sorting.
Parameters:
-
sorting(Sorting) –The new sorting strategy (TIME or ARC_LENGTH).
Source code in trajectopy\core\trajectory.py
overlaps_with ¶
overlaps_with(other: Trajectory) -> bool
Checks if the time span of this trajectory overlaps with another.
Parameters:
-
other(Trajectory) –The trajectory to compare against.
Returns:
-
bool(bool) –True if the time ranges overlap, False otherwise.
Source code in trajectopy\core\trajectory.py
crop ¶
crop(t_start: float, t_end: float, inverse: bool = False, inplace: bool = True) -> Trajectory
Crops (or cuts) the trajectory based on a time window.
Parameters:
-
t_start(float) –Start timestamp of the window.
-
t_end(float) –End timestamp of the window.
-
inverse(bool, default:False) –If True, removes data inside the window (cutting). If False, keeps data inside the window (cropping). Defaults to False.
-
inplace(bool, default:True) –If True, modifies self. If False, returns a new instance. Defaults to True.
Returns:
-
Trajectory(Trajectory) –The modified or new trajectory instance.
Source code in trajectopy\core\trajectory.py
intersect ¶
intersect(timestamps: ndarray, max_gap_size: float = 10.0, inplace: bool = True) -> Trajectory
Filters the trajectory to overlap with a reference timestamp vector.
This method finds the common time span between self and the reference timestamps,
crops self to that span, and then filters points that are either exact matches
or exist within valid gaps defined by max_gap_size.
Parameters:
-
timestamps(ndarray) –The reference timestamps to intersect with.
-
max_gap_size(float, default:10.0) –The maximum allowed time gap (in seconds) between reference timestamps to include trajectory points. Defaults to 10.0.
-
inplace(bool, default:True) –If True, modifies self. Defaults to True.
Raises:
-
ValueError–If the time spans do not overlap.
Returns:
-
Trajectory(Trajectory) –The intersected trajectory.
Source code in trajectopy\core\trajectory.py
mask ¶
mask(mask: list | ndarray, inplace: bool = True) -> Trajectory
Applies a boolean mask or index array to filter all trajectory components.
Filtered components include: timestamps, positions, rotations, path lengths, and velocities.
Parameters:
-
mask(Union[list, ndarray]) –Boolean array or list of indices to keep.
-
inplace(bool, default:True) –If True, modifies self. Defaults to True.
Returns:
-
Trajectory(Trajectory) –The masked trajectory.
Source code in trajectopy\core\trajectory.py
transform ¶
transform(transformation: ndarray, inplace: bool = True) -> Trajectory
Applies a rigid body transformation to the trajectory poses.
Parameters:
-
transformation(ndarray) –A 4x4 homogeneous transformation matrix.
-
inplace(bool, default:True) –If True, modifies self. Defaults to True.
Returns:
-
Trajectory(Trajectory) –The transformed trajectory.
Source code in trajectopy\core\trajectory.py
to_dataframe ¶
Exports the trajectory to a Pandas DataFrame.
Columns usually include: time, path_length, pos_x, pos_y, pos_z, speed_x, speed_y, speed_z, and rotation columns (rot_x/y/z/w) if available.
Parameters:
-
sort_by(str, default:'') –Column name to sort by. If empty, uses
self.sorting.
Returns:
-
DataFrame–pd.DataFrame: A dataframe containing the trajectory data.
Source code in trajectopy\core\trajectory.py
to_string ¶
Serializes the trajectory to a CSV-formatted string with metadata headers.
Headers included: #epsg, #name, #nframe, #sorting, #fields.
Source code in trajectopy\core\trajectory.py
to_file ¶
Writes the trajectory to an ASCII file using the format defined in to_string.
Parameters:
-
filename(str) –The output file path.
-
mode(str, default:'w') –File open mode. Defaults to "w".
Source code in trajectopy\core\trajectory.py
to_kml ¶
Exports the trajectory to a Google Earth KML file.
Requires the trajectory to have a valid EPSG code so it can be converted to WGS84 (EPSG:4326).
Parameters:
-
filename(str) –The output filename (e.g., "track.kml").
-
precision(float, default:1e-06) –Coordinate precision in degrees for rounding/simplification. Defaults to 1e-6.
Raises:
-
ValueError–If the trajectory does not have a known EPSG code.