def generate_speed_data(traj_dir, grid_idx, feature_path): MIN_DISTANCE_IN_METER = 5 MAX_DISTANCE_IN_METER = 300 speed_data = np.zeros((grid_idx.row_num, grid_idx.col_num, 1), dtype=np.float) cnt_data = np.zeros((grid_idx.row_num, grid_idx.col_num, 1), dtype=np.float) for filename in tqdm(os.listdir(traj_dir)): if not filename.endswith('.txt'): continue traj_list = parse_traj_file(os.path.join(traj_dir, filename)) for traj in traj_list: for i in range(len(traj.pt_list) - 1): cur_pt, next_pt = traj.pt_list[i], traj.pt_list[i + 1] delta_time = (next_pt.time - cur_pt.time).total_seconds() if MIN_DISTANCE_IN_METER < distance(cur_pt, next_pt) < MAX_DISTANCE_IN_METER: try: row_idx, col_idx = grid_idx.get_matrix_idx(cur_pt.lat, cur_pt.lng) speed = distance(next_pt, cur_pt) / delta_time # 120 km/h if speed > 34: continue speed_data[row_idx, col_idx, 0] += speed cnt_data[row_idx, col_idx, 0] += 1 except IndexError: continue speed_data = np.divide(speed_data, cnt_data, out=np.zeros_like(speed_data), where=cnt_data != 0) np.save(os.path.join(feature_path, 'speed.npy'), speed_data)
def generate_transition_view(traj_dir, grid_idx, nbhd_size, nbhd_dist, feature_path): MIN_DISTANCE_IN_METER = 5 MAX_DISTANCE_IN_METER = 300 meters_per_grid = grid_idx.lat_interval / LAT_PER_METER radius = int(nbhd_dist / meters_per_grid) transit_data = np.zeros((grid_idx.row_num, grid_idx.col_num, nbhd_size, nbhd_size, 2), dtype=np.uint8) for filename in tqdm(os.listdir(traj_dir)): if not filename.endswith('.txt'): continue traj_list = parse_traj_file(os.path.join(traj_dir, filename)) for traj in traj_list: for idx in range(len(traj.pt_list) - 1): cur_pt = traj.pt_list[idx] next_pt = traj.pt_list[idx + 1] if MIN_DISTANCE_IN_METER < distance(cur_pt, next_pt) < MAX_DISTANCE_IN_METER: try: global_cur_i, global_cur_j = grid_idx.get_matrix_idx(cur_pt.lat, cur_pt.lng) local_idx = get_local_idx(global_cur_i, global_cur_j, radius, grid_idx, nbhd_dist) local_next_i, local_next_j = local_idx.get_matrix_idx(next_pt.lat, next_pt.lng) transit_data[global_cur_i, global_cur_j, local_next_i, local_next_j, 0] = 1 global_next_i, global_next_j = grid_idx.get_matrix_idx(next_pt.lat, next_pt.lng) local_idx = get_local_idx(global_next_i, global_next_j, radius, grid_idx, nbhd_dist) local_cur_i, local_cur_j = local_idx.get_matrix_idx(cur_pt.lat, cur_pt.lng) transit_data[global_next_i, global_next_j, local_cur_i, local_cur_j, 1] = 1 except IndexError: continue np.save(os.path.join(feature_path, 'transition.npy'), transit_data)
def generate_line_image(traj_dir, grid_idx, feature_path): MIN_DISTANCE_IN_METER = 5 MAX_DISTANCE_IN_METER = 300 traj_line_img = np.zeros((grid_idx.row_num, grid_idx.col_num), dtype=np.uint8) for filename in tqdm(os.listdir(traj_dir)): if not filename.endswith('.txt'): continue traj_list = parse_traj_file(os.path.join(traj_dir, filename)) for traj in traj_list: one_traj_line_img = np.zeros((grid_idx.row_num, grid_idx.col_num), dtype=np.uint8) for j in range(len(traj.pt_list) - 1): cur_pt, next_pt = traj.pt_list[j], traj.pt_list[j + 1] if MIN_DISTANCE_IN_METER < distance( cur_pt, next_pt) < MAX_DISTANCE_IN_METER: try: y1, x1 = grid_idx.get_matrix_idx( cur_pt.lat, cur_pt.lng) y2, x2 = grid_idx.get_matrix_idx( next_pt.lat, next_pt.lng) cv2.line(one_traj_line_img, (x1, y1), (x2, y2), 16, 1, lineType=cv2.LINE_AA) except IndexError: continue traj_line_img = cv2.add(traj_line_img, one_traj_line_img) cv2.imwrite(os.path.join(feature_path, 'line.png'), traj_line_img)
def execute(self, filename, traj_path, mm_result_path): rn = load_rn_shp(self.rn_path, is_directed=True) for u, v, data in rn.edges(data=True): if data['type'] == 'virtual': data['weight'] = data['length'] * self.alpha else: data['weight'] = data['length'] map_matcher = TIHMMMapMatcher(rn, routing_weight='weight') traj_list = parse_traj_file(os.path.join(traj_path, filename)) all_paths = [] for traj in traj_list: paths = map_matcher.match_to_path(traj) all_paths.extend(paths) store_path_file(all_paths, os.path.join(mm_result_path, filename))
def generate_point_image(traj_dir, grid_idx, feature_path): pt_cnt = np.zeros((grid_idx.row_num, grid_idx.col_num)) for filename in tqdm(os.listdir(traj_dir)): if not filename.endswith('.txt'): continue trajs = parse_traj_file(os.path.join(traj_dir, filename)) for traj in trajs: for cur_pt in traj.pt_list: try: row_idx, col_idx = grid_idx.get_matrix_idx(cur_pt.lat, cur_pt.lng) pt_cnt[row_idx, col_idx] += 1 except IndexError: continue pt_cnt = pt_cnt / 2 * 255 pt_cnt[pt_cnt > 255] = 255 cv2.imwrite(os.path.join(feature_path, 'point.png'), pt_cnt)
def generate_dir_dist_data(traj_dir, grid_idx, feature_path): MIN_DISTANCE_IN_METER = 5 MAX_DISTANCE_IN_METER = 300 dir_data = np.zeros((grid_idx.row_num, grid_idx.col_num, 8), dtype=np.uint8) for filename in tqdm(os.listdir(traj_dir)): if not filename.endswith('.txt'): continue traj_list = parse_traj_file(os.path.join(traj_dir, filename)) for traj in traj_list: for i in range(len(traj.pt_list) - 1): cur_pt, next_pt = traj.pt_list[i], traj.pt_list[i + 1] if MIN_DISTANCE_IN_METER < distance(cur_pt, next_pt) < MAX_DISTANCE_IN_METER: try: row_idx, col_idx = grid_idx.get_matrix_idx(cur_pt.lat, cur_pt.lng) direction = int(((bearing(cur_pt, next_pt) + 22.5) % 360) // 45) dir_data[row_idx, col_idx, direction] += 1 except IndexError: continue np.save(os.path.join(feature_path, 'direction.npy'), dir_data)