def get_tra_seg_A(df, end): """ Get every single trajectory from dataset by id. And then get group of their segmentation. """ line_seg = dict() seg = [] for idx in df.id.drop_duplicates(): if idx > end: continue t = df[df.id == idx] t = t.sort_values('BaseDateTime') t = t.reset_index() line_seg = [ Segment(Point(t.LON[i] * 100, t.LAT[i] * 100), Point(t.LON[i + 1] * 100, t.LAT[i + 1] * 100), int(idx)) for i in range(0, t.shape[0] - 1) ] seg.append( pd.DataFrame([[ line_seg[i].start.x, line_seg[i].start.y, line_seg[i].end.x, line_seg[i].end.y, line_seg[i].traj_id, line_seg[i].cluster_id ] for i in range(0, len(line_seg))], columns=[ 'start_x', 'start_y', 'end_x', 'end_y', 'traj_id', 'cluster_id' ])) print('Segment of trajectory %s has done' % idx) seg = pd.concat(seg) return seg
def read_cluster(): norm_cluster = dict() cluster_long, cluster_lat = [], [] file_root = 'result/AIS_cluster' fn = "AIS_922" # fn = "TD" for root, dir, files in os.walk(file_root): for f in files: if fn not in f: continue file_path = os.path.join(root, f) clu_t = pd.read_csv(file_path, engine='python') for idx in clu_t.cluster_id.drop_duplicates(): t = clu_t[clu_t.cluster_id == idx] if idx not in norm_cluster: norm_cluster[idx] = [] for k, s in t.iterrows(): norm_cluster[idx].extend([ Segment(Point(s.start_x, s.start_y), Point(s.end_x, s.end_y), s.traj_id, s.cluster_id) ]) cluster_long.append(s.start_x) cluster_long.append(s.end_x) cluster_lat.append(s.start_y) cluster_lat.append(s.end_y) print('%s has imported!' % f) return norm_cluster, cluster_long, cluster_lat
def gather_tra(): tra = dict() file_root = 'result/line_seg_files' fn1 = "AIS_seg" fn2 = "922" # fn = "TD_seg_" for root, dir, files in os.walk(file_root): for f in files: if fn1 not in f or fn2 not in f: continue file_path = os.path.join(root, f) seg_t = pd.read_csv(file_path, engine='python') for idx in seg_t.traj_id.drop_duplicates(): t = seg_t[seg_t.traj_id == idx] tra[idx] = [] for k, s in t.iterrows(): tra[idx].append(Point(s.start_x, s.start_y)) tra[idx].append(Point(s.end_x, s.end_y)) print('%s has imported!' % f) return tra
def get_tra_part(df): """ Get every single trajectory from dataset by id. And then get group of their segmentation. """ tra = dict() line_seg = dict() seg = [] for idx in df.id.drop_duplicates(): # if idx > 20: # continue t = df[df.id == idx] t = t.reset_index() t.sort_values('BaseDateTime') tra[idx] = [Point(t.LON[i], t.LAT[i]) for i in range(0, t.shape[0])] line_seg[idx] = approximate_trajectory_partitioning(tra[idx], theta=0, traj_id=int(idx)) seg.extend(line_seg[idx]) print('Segment of trajectory %s has done' % idx) return tra, line_seg, seg
from trajCluster.cluster import line_segment_clustering, representative_trajectory_generation from matplotlib import pyplot as plt # ts1 = [560.0, 652.0, 543.0, 651.0, 526.0, 649.0, 510.0, 647.0, 494.0, 644.0, 477.0, 639.0, 460.0, 632.0, 446.0, 622.0, 431.0, 611.0, 417.0, 604.0, 400.0, 597.0, 383.0, 587.0, 372.0, 579.0, 363.0, 573.0, 355.0, 563.0, 356.0, 552.0, 361.0, 537.0, 370.0, 523.0, 380.0, 510.0, 391.0, 498.0, 404.0, 485.0, 415.0, 475.0, 429.0, 466.0, 444.0, 459.0, 465.0, 451.0, 493.0, 442.0, 530.0, 432.0, 568.0, 423.0, 606.0, 417.0, 644.0, 412.0, 681.0, 408.0, 714.0, 404.0, 747.0, 401.0, 770.0, 399.0, 793.0, 397.0, 818.0, 395.0] ts2 = [565.0, 689.0, 547.0, 682.0, 525.0, 674.0, 502.0, 668.0, 480.0, 663.0, 452.0, 660.0, 424.0, 656.0, 400.0, 652.0, 380.0, 650.0, 356.0, 649.0, 335.0, 647.0, 314.0, 642.0, 297.0, 639.0, 283.0, 634.0, 272.0, 625.0, 259.0, 614.0, 245.0, 603.0, 237.0, 596.0, 228.0, 589.0, 218.0, 582.0, 208.0, 574.0, 198.0, 567.0, 193.0, 561.0, 191.0, 554.0, 185.0, 551.0, 181.0, 551.0, 179.0, 549.0, 178.0, 547.0, 178.0, 544.0, 177.0, 540.0, 174.0, 533.0, 170.0, 527.0, 164.0, 523.0, 154.0, 521.0, 145.0, 517.0, 131.0, 514.0, 118.0, 515.0, 106.0, 515.0, 92.0, 512.0, 74.0, 507.0, 57.0, 501.0, 40.0, 495.0, 23.0, 491.0] ts1 = [s-np.random.randint(10, 20) for s in ts2] ts3 = [s+np.random.randint(1, 10) for s in ts2] # [590.0, 495.0, 590.0, 498.0, 593.0, 503.0, 597.0, 507.0, 600.0, 507.0, 602.0, 505.0, 605.0, 497.0, 594.0, 487.0, 580.0, 482.0, 565.0, 483.0, 550.0, 492.0, 547.0, 497.0, 544.0, 499.0, 541.0, 494.0, 540.0, 489.0, 538.0, 479.0, 530.0, 474.0, 528.0, 485.0, 540.0, 480.0, 542.0, 477.0, 543.0, 474.0, 538.0, 476.0, 530.0, 486.0, 524.0, 497.0, 513.0, 507.0, 499.0, 516.0, 482.0, 527.0, 468.0, 538.0, 453.0, 547.0, 438.0, 555.0, 429.0, 563.0, 423.0, 566.0, 420.0, 569.0, 417.0, 572.0, 414.0, 570.0, 411.0, 566.0, 411.0, 557.0, 408.0, 545.0, 405.0, 536.0, 403.0, 530.0, 401.0, 526.0, 401.0, 522.0, 404.0, 523.0, 409.0, 523.0, 418.0, 522.0, 420.0, 522.0, 426.0, 530.0] ts4 = [s+np.random.randint(20, 30) for s in ts2] # [559.0, 492.0, 553.0, 483.0, 548.0, 475.0, 544.0, 466.0, 536.0, 456.0, 534.0, 447.0, 536.0, 438.0, 540.0, 429.0, 551.0, 419.0, 566.0, 408.0, 583.0, 399.0, 600.0, 389.0, 622.0, 379.0, 642.0, 373.0, 660.0, 373.0, 676.0, 375.0, 693.0, 381.0, 708.0, 391.0, 721.0, 402.0, 732.0, 412.0, 737.0, 421.0, 741.0, 429.0, 742.0, 437.0, 738.0, 443.0, 733.0, 447.0, 728.0, 449.0, 722.0, 450.0, 714.0, 451.0, 710.0, 445.0, 700.0, 440.0, 695.0, 440.0, 695.0, 434.0, 700.0, 435.0, 705.0, 436.0, 711.0, 435.0, 708.0, 437.0, 710.0, 440.0, 710.0, 445.0, 705.0, 455.0, 700.0, 462.0, 695.0, 470.0, 690.0, 480.0, 680.0, 490.0, 665.0, 490.0] traj1 = [Point(ts1[i:i+2][0], ts1[i:i+2][1]) for i in range(0, len(ts1), 2)] traj2 = [Point(ts2[i:i+2][0], ts2[i:i+2][1]) for i in range(0, len(ts2), 2)] traj3 = [Point(ts3[i:i+2][0], ts3[i:i+2][1]) for i in range(0, len(ts3), 2)] traj4 = [Point(ts4[i:i+2][0], ts4[i:i+2][1]) for i in range(0, len(ts4), 2)] # part 1: partition part1 = approximate_trajectory_partitioning(traj1, theta=6.0, traj_id=1) part2 = approximate_trajectory_partitioning(traj2, theta=6.0, traj_id=2) part3 = approximate_trajectory_partitioning(traj3, theta=6.0, traj_id=3) part4 = approximate_trajectory_partitioning(traj4, theta=6.0, traj_id=4) all_segs = part1 + part2 + part3 + part4 print(len(all_segs)) norm_cluster, remove_cluster = line_segment_clustering(all_segs, min_lines=3, epsilon=15.0) for k, v in remove_cluster.items(): print("remove cluster: the cluster %d, the segment number %d" % (k, len(v)))
547.0, 510.0, 534.0, 514.0, 525.0, 518.0, 517.0, 521.0, 510.0, 522.0, 505.0, 525.0, 500.0, 529.0, 496.0, 530.0, 492.0, 532.0, 486.0, 535.0, 477.0, 537.0, 471.0, 535.0, 468.0, 533.0, 463.0, 529.0, 456.0, 528.0, 453.0, 523.0, 453.0, 519.0, 454.0, 513.0, 453.0, 509.0, 445.0, 510.0, 433.0, 522.0, 432.0, 518.0, 432.0, 515.0, 428.0, 515.0, 425.0, 515.0, 423.0, 516.0, 422.0, 515.0, 424.0, 512.0, 431.0, 510.0, 433.0, 509.0, 434.0, 510.0, 428.0, 510.0, 423.0, 507.0, 420.0, 506.0, 416.0, 504.0, 405.0, 499.0, 398.0, 494.0, 391.0, 489.0, 384.0, 484.0, 387.0, 477.0, 392.0, 471.0, 399.0, 468.0, 403.0, 464.0, 403.0, 467.0, 403.0, 472.0, 403.0, 474.0, 396.0, 474.0, 392.0, 474.0, 391.0, 473.0, 390.0, 474.0, 388.0, 479.0, 387.0, 486.0, 381.0, 493.0, 376.0, 502.0, 367.0, 510.0, 360.0, 513.0, 352.0, 515.0, 342.0, 516.0, 330.0, 517.0, 319.0, 516.0, 305.0, 511.0, 298.0, 502.0, 292.0, 493.0, 293.0, 482.0, 307.0, 468.0, 320.0, 458.0, 341.0, 446.0, 359.0, 433.0 ] traj = [Point(ts[i:i + 2][0], ts[i:i + 2][1]) for i in range(0, len(ts), 2)] cost_par = segment_mdl_comp(traj, 0, 1, typed='par') cost_nopar = segment_mdl_comp(traj, 0, 1, typed='nopar') part = approximate_trajectory_partitioning(traj, theta=6.0) # print([(p.start, p.end) for p in part]) source_line_x = [p.x for p in traj] source_line_y = [p.y for p in traj] dest_line_x = [] dest_line_y = [] i = 0 for s in part: if i == 0:
# -*- coding: utf-8 -*- # -------------------------------------------------------------------------------- # file :segment_test.py # target : # # output : # author :Miller # date :2019/4/1 16:05 # log :包含修改时间、修改人、修改line及原因 # -------------------------------------------------------------------------------- from trajCluster.segment import Segment from trajCluster.point import Point s = Point(1.0, 2.0) e = Point(10.0, 2.0) se1 = Segment(s, e, traj_id=1) se2 = Segment(Point(3.0, 5.0), Point(7.0, 8.0)) x = se1.perpendicular_distance(se2) print("两个segment的垂直距离为:", x) y = se1.parallel_distance(se2) print("两个segment的长度距离为:", y) z = se1.angle_distance(se2) print("两个segment的角度距离为:", z)