def lcs(a, b, radiusBound): lengths = [[0 for j in range(len(b) + 1)] for i in range(len(a) + 1)] # row 0 and column 0 are initialized to 0 already for i, x in enumerate(a): for j, y in enumerate(b): if calDistance(x, y) <= radiusBound: lengths[i + 1][j + 1] = lengths[i][j] + 1 else: lengths[i+1][j+1] = \ max(lengths[i+1][j], lengths[i][j+1]) # read the substring out from the matrix # result = [] # x, y = len(a), len(b) # while x != 0 and y != 0: # print('aa') # if lengths[x][y] == lengths[x-1][y]: # x -= 1 # elif lengths[x][y] == lengths[x][y-1]: # y -= 1 # else: # assert calDistance(a[x-1],b[y-1])<=radiusBound # result.append(a[x-1]) # x -= 1 # y -= 1 # print(result) return lengths[-1][-1]
def find_near(lst, pnt, radius): near = [] for i in range(len(lst)): # print(calDistance(lst[i],pnt)) if calDistance(lst[i], pnt) < radius: near.append(i) return near
def Include_place_2(lst, place, radius): # list of tracking points count = 0 for pnt in lst: count = count + (1 if calDistance(pnt, place) <= radius else 0) if count > 0: return True else: return False
def find_nearest(lst, pnt): nearest = lst[0] dis = 99999999 for i in range(len(lst)): # print(calDistance(lst[i],pnt)) new_dis = calDistance(lst[i], pnt) if new_dis < dis: dis = new_dis nearest = lst[i] print(dis) return nearest
def searchTrip(user, period, startpoint, endpoint, mode, option): user_id = UUID(user) user_home = detect_home(user_id) gmap = pygmaps.maps(user_home[0], user_home[1], 14) start, end = Date(period) sectionList = [] startpoint = Geocoder.geocode(startpoint)[0].coordinates endpoint = Geocoder.geocode(endpoint)[0].coordinates for section in get_section_db().find({ "$and": [{ "mode": mode }, { "mode": { "$ne": 'airplane' } }, { "mode": { "$ne": 7 } }, { "section_start_point": { "$ne": None } }, { "section_end_point": { "$ne": None } }] }): point_start = section['section_start_point']['coordinates'] point_end = section['section_end_point']['coordinates'] if calDistance(startpoint, point_start) < 100 and calDistance( endpoint, point_end) < 100: sectionList.append(section['_id']) gmap.addpoint(point_end[0], point_end[1], COLOR[1]) gmap.addpoint(point_start[0], point_start[1], COLOR[1]) drawSection(section, option, gmap) gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' + str(end)[:10] + '_' + user + '.html') print sectionList
def most_common_2(lst, radius): # list of tracking points max_pnts = 0 most_common_pnt = lst[0] for pnt in lst: count = 0 for pnt2 in lst: if calDistance(pnt, pnt2) <= radius: count = count + 1 if max_pnts <= count: max_pnts = count most_common_pnt = pnt return most_common_pnt
def most_common(lst, radius): # list of tracking points max_pnts = 0 most_common_pnt = lst[0] for pnt in lst: count = 0 for pnt2 in lst: if calDistance(pnt['track_location']['coordinates'], pnt2['track_location']['coordinates']) <= radius: count = count + 1 if max_pnts <= count: max_pnts = count most_common_pnt = pnt return most_common_pnt['track_location']['coordinates']
def getDistance(sectionList): distance_list = [] for section in sectionList: points = section['track_points'] if len(points) >= 2: point_curr = points[0]['track_location']['coordinates'] distance = 0 for i in range(len(points) - 1): point_next = points[i + 1]['track_location']['coordinates'] distance = distance + calDistance(point_curr, point_next) point_curr = point_next distance_list.append(distance) else: distance_list.append(0) return distance_list
def Frechet(R1, R2, varargin=None): # get path point length L1 = len(R1) L2 = len(R2) frechet1 = np.zeros((L1, L2)) # print(frechet1) # calculate frechet distance matrix for i in range(L1): for j in range(L2): # frechet1[i,j]=math.sqrt(math.pow(R1[i][0]-R2[j][0],2)+math.pow(R1[i][1]-R2[j][1],2)) frechet1[i, j] = calDistance(R1[i], R2[j]) fmin = frechet1.min() fmax = frechet1.max() # print(fmin) # print(fmax) # handle resolution if varargin == None: varargin = 1000 # print(frechet1<=3) # print(varargin) # compute frechet distance # print(np.linspace(fmin,fmax,varargin)) for q3 in np.linspace(fmin, fmax, varargin): # print(q3) im1 = np.asarray(measurements.label(frechet1 <= q3))[0] # print(im1.shape) # print(im1[-1,-1]) # get region number of beginning and end points if im1[0, 0] != 0 and im1[0, 0] == im1[-1, -1]: f = q3 break return f # R1=[] # R2=[] # for aa in range(0,51): # # R1.append([(2*math.cos(aa/5)+3-math.pow(aa,2)/200)/2,2*math.sin(aa/5)+3]) # # R2.append([(2*math.cos(aa/4)+2-math.pow(aa,2)/200)/2,2*math.sin(aa/5)+3]) # R1.append([aa,aa]) # R2.append([aa,aa+8]) # # print(R1) # # print(R2) # print(Frechet(R1,R2))
def get_static_pnts(lst): # parameter interval = 600 new_lst = [] new_lst.extend(lst) for i in range(len(lst) - 1): loc1 = lst[i]['track_location']['coordinates'] loc2 = lst[i + 1]['track_location']['coordinates'] if Is_sameday( lst[i]['time'], lst[i + 1]['time']) == True and calDistance(loc1, loc2) <= 100: duration = travel_time(lst[i]['time'], lst[i + 1]['time']) for j in range(int(duration / interval / 2)): new_lst.append(lst[i]) new_lst.append(lst[i + 1]) new_lst = sorted(new_lst, key=lambda k: parser.parse(k['time'])) return new_lst
def refineRoute(lst1, step): # print(len(lst1)) lst1_extended = [] for i in range(len(lst1) - 1): dis = calDistance(lst1[i], lst1[i + 1]) num_inter = int(round(dis / step)) if num_inter == 0: lst1_extended.append(lst1[i]) else: lat_list = np.linspace(lst1[i][0], lst1[i + 1][0], num_inter, False) lon_list = np.linspace(lst1[i][1], lst1[i + 1][1], num_inter, False) for j in range(len(lat_list)): lst1_extended.append([lat_list[j], lon_list[j]]) lst1_extended.append(lst1[-1]) # print(len(lst1)) # print(len(lst1_extended)) return lst1_extended
def route_matching(lst1, lst2, step, radius, len_match, min_score): # input 2 lists of tracking points, each tracking points is geojson format # the two lists must have at least two tracking points if len(lst1) < 2 or len(lst2) < 2: return False start_pnt1 = lst1[0] end_pnt1 = lst1[-1] start_pnt2 = lst2[0] end_pnt2 = lst2[-1] # Case 1, lst2 is part of lst1: lst1_extended = [] for i in range(len(lst1) - 1): dis = calDistance(lst1[i]['track_location']['coordinates'], lst1[i + 1]['track_location']['coordinates']) num_inter = int(round(dis / step)) if num_inter == 0: lst1_extended.append(lst1[i]['track_location']['coordinates']) else: lat_list = np.linspace( lst1[i]['track_location']['coordinates'][0], lst1[i + 1]['track_location']['coordinates'][0], num_inter, False) lon_list = np.linspace( lst1[i]['track_location']['coordinates'][1], lst1[i + 1]['track_location']['coordinates'][1], num_inter, False) for j in range(len(lat_list)): lst1_extended.append([lat_list[j], lon_list[j]]) lst1_extended.append(end_pnt1['track_location']['coordinates']) lst2_extended = [] for i in range(len(lst2) - 1): dis = calDistance(lst2[i]['track_location']['coordinates'], lst2[i + 1]['track_location']['coordinates']) num_inter = int(round(dis / step)) if num_inter == 0: lst2_extended.append(lst2[i]['track_location']['coordinates']) else: lat_list = np.linspace( lst2[i]['track_location']['coordinates'][0], lst2[i + 1]['track_location']['coordinates'][0], num_inter, False) lon_list = np.linspace( lst2[i]['track_location']['coordinates'][1], lst2[i + 1]['track_location']['coordinates'][1], num_inter, False) for j in range(len(lat_list)): lst2_extended.append([lat_list[j], lon_list[j]]) lst2_extended.append(end_pnt2['track_location']['coordinates']) # print(len(lst1_extended)) # print(len(lst2_extended)) near_start2 = find_near(lst1_extended, start_pnt2['track_location']['coordinates'], radius) near_end2 = find_near(lst1_extended, end_pnt2['track_location']['coordinates'], radius) near_start1 = find_near(lst2_extended, start_pnt1['track_location']['coordinates'], radius) near_end1 = find_near(lst2_extended, end_pnt1['track_location']['coordinates'], radius) # print(near_start2) # print(near_end2) # print(near_start1) # print(near_end1) best_score = [] if len(near_start2) > 0 and len(near_end2) > 0: print("start of case 1") for near_s in near_start2: for near_e in near_end2: if min(abs(near_e - near_s) + 1, len(lst2_extended)) / max( abs(near_e - near_s) + 1, len(lst2_extended)) >= len_match: print("possible near_s is %s" % near_s) print("possible near_e is %s" % near_e) if near_e > near_s: print("start index is %d" % near_s) print("end index is %d" % near_e) route1 = lst1_extended[near_s:near_e + 1:1] route2 = lst2_extended print("route1 is %s" % route1) print("route2 is %s" % route2) else: print("start index is %d" % near_s) print("end index is %d" % near_e) route1 = lst1_extended[near_e:near_s + 1:1][::-1] route2 = lst2_extended print("route1 is %s" % route1) print("route2 is %s" % route2) best_score.append( cal_matching_score(route1, route2, radius)) if len(near_start1) > 0 and len(near_end1) > 0: print("start of case 2") for near_s in near_start1: for near_e in near_end1: if min(abs(near_e - near_s) + 1, len(lst1_extended)) / max( abs(near_e - near_s) + 1, len(lst1_extended)) >= len_match: if near_e > near_s: print("start index is %d" % near_s) print("end index is %d" % near_e) route1 = lst1_extended route2 = lst2_extended[near_s:near_e + 1:1] print("route1 is %s" % route1) print("route2 is %s" % route2) else: route1 = lst1_extended route2 = lst2_extended[near_e:near_s + 1:1][::-1] best_score.append( cal_matching_score(route1, route2, radius)) print(best_score) if len(best_score) > 0 and max(best_score) > min_score: return True else: return False
def route_matching_2(lst1, lst2, step, radius, min_score): # input 2 lists of tracking points, each tracking points is geojson format # the two lists must have at least two tracking points if len(lst1) < 2 or len(lst2) < 2: return False start_pnt1 = lst1[0] end_pnt1 = lst1[-1] start_pnt2 = lst2[0] end_pnt2 = lst2[-1] # Case 1, lst2 is part of lst1: lst1_extended = [] for i in range(len(lst1) - 1): dis = calDistance(lst1[i]['track_location']['coordinates'], lst1[i + 1]['track_location']['coordinates']) num_inter = int(round(dis / step)) if num_inter == 0: lst1_extended.append(lst1[i]['track_location']['coordinates']) else: lat_list = np.linspace( lst1[i]['track_location']['coordinates'][0], lst1[i + 1]['track_location']['coordinates'][0], num_inter, False) lon_list = np.linspace( lst1[i]['track_location']['coordinates'][1], lst1[i + 1]['track_location']['coordinates'][1], num_inter, False) for j in range(len(lat_list)): lst1_extended.append([lat_list[j], lon_list[j]]) lst1_extended.append(end_pnt1['track_location']['coordinates']) lst2_extended = [] for i in range(len(lst2) - 1): dis = calDistance(lst2[i]['track_location']['coordinates'], lst2[i + 1]['track_location']['coordinates']) num_inter = int(round(dis / step)) if num_inter == 0: lst2_extended.append(lst2[i]['track_location']['coordinates']) else: lat_list = np.linspace( lst2[i]['track_location']['coordinates'][0], lst2[i + 1]['track_location']['coordinates'][0], num_inter, False) lon_list = np.linspace( lst2[i]['track_location']['coordinates'][1], lst2[i + 1]['track_location']['coordinates'][1], num_inter, False) for j in range(len(lat_list)): lst2_extended.append([lat_list[j], lon_list[j]]) lst2_extended.append(end_pnt2['track_location']['coordinates']) # print(len(lst1_extended)) # print(len(lst2_extended)) best_score = [] score_2_in_1 = 0 for point2 in lst2: if Include_place_2(lst1_extended, point2['track_location']['coordinates'], radius): score_2_in_1 += 1 best_score.append(score_2_in_1 / len(lst2)) score_1_in_2 = 0 for point1 in lst1: if Include_place_2(lst2_extended, point1['track_location']['coordinates'], radius): score_1_in_2 += 1 best_score.append(score_1_in_2 / len(lst1)) print(best_score) if max(best_score) > min_score: return True else: return False
def Is_place_2(place1, place, radius): # print(place) if calDistance(place1, place) < radius: return True else: return False
def Is_place(place1, place, radius): # print(place) if calDistance(place1['coordinates'], place) < radius: return True else: return False