Classes for Trajectory Data¶
Trajectory Point Table Class¶
-
class
mtldp.mtltrajs.OverallPoints(df=None)¶ class object for storing and manipulating a data frame of trajectory points from any number of trajectories Attributes
.df: data frame of trajectory points across all trajectories
-
__add__(other)¶ specifies the __add__ method to run
self.append(other)and create a newmtldp.mtltrajs.OverallPointsclass object tc = tc1 + tc2 :param other: data frame of trajectory points :return: a newmtldp.mtltrajs.OverallPointsclass object
-
__init__(df=None)¶ initiate a trajectory data frame from an existing data frame of trajectory points :param df: data frame of trajectory points from any number of trajectories
- Return type
-
__len__()¶ specifies the __len__ method to return number of trajectories :return: number of trajectories
-
add_basic_mapinfo(network)¶ add other basic map attributes (link and intersection ids) after fast map matching :param network: map network class (
mtldp.mtlmap.Network)- Return type
-
add_complete_mapinfo(network)¶ run both
self.add_basic_mapinfo(network)andself.add_movement(network):param network: map network class (mtldp.mtlmap.Network)- Return type
-
add_date_time_info(tz=None)¶ add another two columns for date and date_time :param tz: see mtlutil.TimeZone :return:
-
add_movement(network)¶ add movement information :param network: map network class (
mtldp.mtlmap.Network)- Return type
-
append(tc)¶ add more trajectory points from another data frame :param tc: data frame of trajectory points
- Return type
-
get_temporal_range()¶ get the first and last timestamp in
self.df:return: the first and last timestamp inself.df
-
get_timezone()¶ get timezone :return: timezone for trajectory points
-
get_trajs_dict(traj_attributes=None)¶ split
self.dfbased on trip id and initiate amtldp.mtltrajs.Trajectoryclass object for each trip and store them in amtldp.mtltrajs.TrajectoryDictclass object :return: amtldp.mtltrajs.TrajectoryDictclass object containing all of the trips inself.df
-
get_truncate_df(start_time, end_time)¶ get the truncated data frame according to a certain time period :param start_time: the beginning of the time period of interest :param end_time: the end of the time period of interest :return: the truncated data frame according to the time period
-
get_truncate_tc(start_time, end_time)¶ get the truncated data frame according to a certain time period and return a new
mtldp.mtltrajs.OverallPointsclass object- Parameters
start_time – the beginning of the time period of interest
end_time – the end of the time period of interest
- Returns
mtldp.mtltrajs.OverallPointsclass object with the truncated data frame
-
load_data(df, sort_trajs=False)¶ load an existing data frame of trajectory points as
self.dfand sort trajectory points by trip id and timestamp :param df: data frame of trajectory points from any number of trajectories :param sort_trajs: indicates if the trajectory points should be sorted by trip id and timestamp- Return type
-
load_file(filename)¶ read a csv file containing trajectory point information in a data frame and run
self.load_data:param filename: csv file path- Return type
-
load_files(file_list)¶ read a list of csv files containing trajectory point information in one single data frame and run
self.load_data:param file_list: list of csv file paths- Return type
-
load_fmm_results(fmm_file)¶ match the fast map matching output file with
self.df- Parameters
fmm_file – path name of fast map matching output file
- Return type
-
output_fmm_file(fmm_input_file, network_bound)¶ create input file for the fast map matching from
self.df- Parameters
fmm_input_file – path name for the fmm input file
network_bound – geographic bounding box for the area of interest
- Return type
-
split_by_link()¶ split
self.dfby link and store the resultingmtldp.mtltrajs.OverallPointsclass objects in a dictionary :return: a dictionary of :class:`mtldp.mtltrajs.OverallPoints’ class objects for each link
-
split_by_movement()¶ split
self.dfby movement and store the resultingmtldp.mtltrajs.OverallPointsclass objects in a dictionary :return: a dictionary of :class:`mtldp.mtltrajs.OverallPoints’ class objects for each movement
-
truncate_by_period(start_time, end_time)¶ truncate the trajectory collection by start/end time stamp
- Parameters
start_time – the beginning of the time period of interest
end_time – the end of the time period of interest
- Return type
Trajectory Dictionary Class¶
-
class
mtldp.mtltrajs.TrajectoryDict(trajs_dict=None)¶ class object for storing and manipulating a dictionary of individual trajectory objects (
mtldp.mtltrajs.Trajectory)- Attributes
.dict: dictionary of individual trajectory objects (mtldp.mtltrajs.Trajectory).timezone: timezone of trajectories inself.dict
-
__add__(other)¶ specifies the __add__ method to run
self.append(other)- Parameters
other – a
mtldp.mtltrajs.TrajectoryDictclass object- Returns
self
-
__init__(trajs_dict=None)¶ initiate the class object from an existing dictionary of trajectory objects (
mtldp.mtltrajs.Trajectory)- Parameters
trajs_dict – dictionary of trajectory objects (
mtldp.mtltrajs.Trajectory) where the keys are the trip ids
-
__len__()¶ specifies the __len__ method to return the number of
mtldp.mtltrajs.Trajectoryclass objects inself.dict:return:the number of
mtldp.mtltrajs.Trajectoryclass objects inself.dict
-
add_local_seconds(overall_timezone=None, in_same_timezone=True)¶ add ‘seconds_in_day’ attribute to each
mtldp.mtltrajs.Trajectoryclass objects inself.dict- Parameters
overall_timezone – mtldp.utils.Timezone
in_same_timezone – True if all trajectories in
self.dictare in the same timezone
- Return type
-
add_trajectory(trajectory)¶ add a
mtldp.mtltrajs.Trajectoryclass object toself.dictwhere the key is the trajectory id- Parameters
trajectory – a
mtldp.mtltrajs.Trajectoryclass object- Return type
-
append(other)¶ append the trajectories from another
mtldp.mtltrajs.TrajectoryDictobject toself.dict- Parameters
other – a
mtldp.mtltrajs.TrajectoryDictclass object- Return type
-
del_trajectories(trajs_id)¶ remove
mtldp.mtltrajs.Trajectoryclass objects fromself.dict- Parameters
trajs_id – a list of trajectory ids
-
eliminate_outliers(length_threshold=5)¶ eliminate outlier
mtldp.mtltrajs.Trajectoryclass objects that have a small number of trajectory points and updateself.dict- Parameters
length_threshold – the minimum number of trajectory points required in a
mtldp.mtltrajs.Trajectoryclass object- Return type
-
get_points_df(attributes='all', resort=True)¶ get a single data frame of all the trajectory points in
self.dictthat includes specific point attributes- Parameters
attributes – a list of point attributes to be included in data frame
resort – a mode that determines if the data frame will be resorted by ‘trip_id’ and ‘timestamp’
- Returns
a single data frame of all the trajectory points in
self.dict
-
get_trajs_df(attributes)¶ get a single data frame of all the trajectories in
self.dictthat includes specific trajectory attributes- Parameters
attributes – list of trajectory attributes
- Returns
a single data frame of trajectories and their attributes
-
pop_traversed_trajectory(timestamp=None, debug=False)¶ - Parameters
timestamp –
debug –
- Returns
-
split_trip_by_gap(time_threshold=60, distance_threshold=500)¶ split all of the
mtldp.mtltrajs.Trajectoryclass objects inself.dictinto shortermtldp.mtltrajs.Trajectoryclass objects and updateself.dict- Parameters
time_threshold – the minimum time between two consecutive trajectory point timestamps required to split an existing trajectory
distance_threshold – the minimum distance traveled between two consecutive trajectory points required to split an existing trajectory
- Return type
-
truncate_by_tod(start_tod, end_tod)¶ trim
self.dictto only containmtldp.mtltrajs.Trajectoryclass objects in a certain time range
Trajectory Class¶
-
class
mtldp.mtltrajs.Trajectory(traj_id, trip_id, trip_attributes=None, df=None)¶ class object for storing and manipulating data from an individual vehicle trajectory
- Attributes
.traj_id: trajectory id.trip_id: parent trajectory id after trajectory splitting, will be the same as.traj_idif the trajectory has not been split.df: data frame of trajectory points.trip_attributes: a list of trajectory attributes provided in the raw data.date: trajectory date.timestamp: timestamp of the last point in the trajectory.date_time: trajectory hour and minute.movement_id: includes the intersection id and phase movement id.text: basic text of pertinent trajectory information (to be plotted)
-
__add__(other)¶ specifies the __add__ method to add more trajectory points to
selffrom anothermtldp.mtltrajs.Trajectoryclass object- Parameters
other – a
mtldp.mtltrajs.Trajectoryclass object- Returns
updated
selfwith added trajectory points
-
__init__(traj_id, trip_id, trip_attributes=None, df=None)¶ Initiate trajectory class
- Parameters
traj_id (str) – trajectory id
trip_id (str) – if
selfis initiated from splitting a previousmtldp.mtltrajs.Trajectoryclass object, than the trip_id is the parent trajectory id, otherwise it is the same as the traj_idtrip_attributes – a list of uniform trajectory attributes provided in the raw data
df – data frame of trajectory points
- Return type
-
__len__()¶ Specifies the __len__ method to return the number trajectory points in
self- Returns
the number of trajectory points in
self
-
__setattr__(name, value, /)¶ Implement setattr(self, name, value).
-
add_local_seconds(overall_timezone=None)¶ Add attribute “seconds_in_day”
- Parameters
overall_timezone – if None, a timezone will be calculated for current trajectory, default None
- Return type
-
generate_tod(tz=None)¶ Generate the tod info of trajectory data
- Parameters
tz – time zone of the trajectory
- Returns
self.dateandself.date_time
-
get_point_attributes(*args)¶ Get point attributes as a list
example: >> lats, lons, times = trajectory.get_point_attributes(“latitude”, “longitude”, “timestamp”)
- Parameters
args – point attributes of interest
- Returns
a list for each point attribute in args
- Return type
-
init_timezone()¶ Get the timezone of current trajectory
if a timezone cannot be inferred from position of current trajectory, the timezone will be set to GMT
- Returns
mtldp.utils.Timezone
-
set_point_attributes(**kwargs)¶ set the point attributes from a list
example: >> trajectory.set_point_attributes(relative_dis=list())
- Parameters
kwargs – new attribute name and attribute values
-
split_by_gap(time_threshold=60, distance_threshold=500)¶ Split
selfby a certain gap (time interval and distance) into shortermtldp.mtltrajs.Trajectoryclass objects- Parameters
time_threshold – the minimum time between two consecutive trajectory point timestamps required to split an existing trajectory
distance_threshold – the minimum distance traveled between two consecutive trajectory points required to split an existing trajectory
- Returns
a
mtldp.mtltrajs.TrajectoryDictclass object with all of the resultingmtldp.mtltrajs.Trajectoryclass objects from the split