This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_top_matching_trajectories(self, top=0.05, exclude_self=True): | |
| df = load_matching_links(self.traj_id) | |
| trajectories = np.unique(df["traj_id"].values) | |
| query_set = set(df[df["traj_id"] == self.traj_id]["quadkey"].values) | |
| if exclude_self: | |
| df = df[df["traj_id"] != self.traj_id] | |
| traj_df = pd.DataFrame(data=trajectories, columns=["traj_id"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def load_matching_links(traj_id, angle_delta=2.5): | |
| db = EVedDb() | |
| sql = """ | |
| select q.link_id | |
| , q.quadkey | |
| , l.traj_id | |
| from link_qk q | |
| inner join link l on l.link_id = q.link_id | |
| inner join ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_top_match_trajectories(self, level=20, top=0.05): | |
| match_df = pd.DataFrame(data=self.calculate_trajectory_matches(level), columns=['traj_id', 'similarity']) | |
| match_df["percent_rank"] = match_df["similarity"].rank(pct=True) | |
| filtered_df = match_df[match_df["percent_rank"] > (1.0 - top)] | |
| trajectories = filtered_df["traj_id"].values | |
| return trajectories |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculate_trajectory_matches(self, level=20): | |
| trajectories, links = self.get_matching_trajectories(level) | |
| route_qks = {qk[0] for qk in self.get_route_quadkeys(level)} | |
| data = [] | |
| for trajectory in trajectories: | |
| traj_qks = load_trajectory_quadkeys(int(trajectory)) | |
| similarity = jaccard_similarity(traj_qks, route_qks) | |
| data.append((trajectory, similarity)) | |
| return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def jaccard_similarity(set0, set1): | |
| return len(set0 & set1) / len(set0 | set1) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_overlapping_links(self, level=20, angle_delta=2.5): | |
| qks = self.get_route_quadkeys(level) | |
| cos_angle_delta = math.cos(math.radians(angle_delta)) | |
| sql = """ | |
| select q.link_id | |
| , l.traj_id | |
| , l.signal_ini | |
| , l.signal_end | |
| from link_qk q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_route_quadkeys(self, level=20): | |
| qks = set() | |
| g = self.graph | |
| shift = 64 - 2 * level | |
| for n0, n1 in pairwise(self.route): | |
| edge = g[n0][n1] | |
| l0 = g.nodes[n0] | |
| l1 = g.nodes[n1] | |
| qks.update([(qk.to_quadint() >> shift, edge[0]['bearing']) | |
| for qk, _ in get_qk_line(l0, l1, level)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gr = GraphRoute('Ann Arbor, Michigan') | |
| route = gr.generate_route(addr_ini="122 N Thayer St, Ann Arbor, MI 48104, USA", | |
| addr_end="1431 Ardmoor Ave, Ann Arbor, MI 48103, USA") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def insert_link_quadkeys(link_quadkey_density_list): | |
| sql = """ | |
| insert into link_qk | |
| (link_id, quadkey, density) | |
| values | |
| (?, ?, ?) | |
| """ | |
| db = EVedDb() | |
| db.execute_sql(sql, parameters=link_quadkey_density_list, many=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def insert_link(traj_id, signal_ini, signal_end, bearing): | |
| sql = """ | |
| insert into link | |
| (traj_id, signal_ini, signal_end, bearing) | |
| values | |
| (?, ?, ?, ifnull(?, -1.0)) | |
| """ | |
| db = EVedDb() | |
| db.execute_sql(sql, [traj_id, signal_ini, signal_end, bearing]) | |
| return db.query_scalar("select seq from sqlite_sequence where name = 'link';") |