Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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]
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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]
Ejemplo n.º 5
0
def max_Distance(points):
    # 'track_points':[{'track_location':{'type':'Point', 'coordinates':[point["lat"],point["lon"]]}, 'time':point["time"]}for point in seg_act_note["trackPoints"]] if "trackPoints" in seg_act_note else []}
    num_pts=len(points)
    max_d=0
    for i in range(num_pts):
        for j in range(i+1,num_pts):
            max_d=max(max_d,calDistance(points[i]['track_location']['coordinates'], points[j]['track_location']['coordinates']))
    return max_d
Ejemplo n.º 6
0
def calSpeed(trackpoint1, trackpoint2):
  from dateutil import parser
  distanceDelta = calDistance(trackpoint1['track_location']['coordinates'],
                              trackpoint2['track_location']['coordinates'])
  timeDelta = parser.parse(trackpoint2['time']) - parser.parse(trackpoint1['time'])
  logging.debug("while calculating speed form %s -> %s, distanceDelta = %s, timeDelta = %s" %
                (trackpoint1, trackpoint2, distanceDelta, timeDelta))
  if timeDelta.total_seconds() != 0:
    return distanceDelta / timeDelta.total_seconds()
  else:
    return None
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def max_Distance(points):
    # 'track_points':[{'track_location':{'type':'Point', 'coordinates':[point["lat"],point["lon"]]}, 'time':point["time"]}for point in seg_act_note["trackPoints"]] if "trackPoints" in seg_act_note else []}
    num_pts = len(points)
    max_d = 0
    for i in range(num_pts):
        for j in range(i + 1, num_pts):
            max_d = max(
                max_d,
                calDistance(points[i]['track_location']['coordinates'],
                            points[j]['track_location']['coordinates']))
    return max_d
Ejemplo n.º 9
0
def searchTrip(user, period, startpoint, endpoint, mode, option):
    user_id = UUID(user)
    user_home = detect_home(user_id)
    gmap = pygmaps.maps(user_home[1], user_home[0], 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[1], point_end[0], COLOR[1])
            gmap.addpoint(point_start[1], point_start[0], COLOR[1])
        drawSection(section, option, gmap)
    gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' +
              str(end)[:10] + '_' + user + '.html')
    print sectionList
Ejemplo n.º 10
0
def calSpeed(trackpoint1, trackpoint2):
    from dateutil import parser
    distanceDelta = calDistance(trackpoint1['track_location']['coordinates'],
                                trackpoint2['track_location']['coordinates'])
    timeDelta = parser.parse(trackpoint2['time']) - parser.parse(
        trackpoint1['time'])
    logging.debug(
        "while calculating speed form %s -> %s, distanceDelta = %s, timeDelta = %s"
        % (trackpoint1, trackpoint2, distanceDelta, timeDelta))
    if timeDelta.total_seconds() != 0:
        return distanceDelta / timeDelta.total_seconds()
    else:
        return None
Ejemplo n.º 11
0
def searchTrip(user, period, startpoint, endpoint, mode, option):
    user_id = UUID(user)
    user_home = detect_home(user_id)
    gmap = pygmaps.maps(user_home[1], user_home[0], 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[1], point_end[0], COLOR[1])
            gmap.addpoint(point_start[1], point_start[0], COLOR[1])
        drawSection(section, option, gmap)
    gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' + str(end)[:10] + '_' + user + '.html')
    print sectionList
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def _check_zip_validity(user_home, user):
    if user_home != "N/A" and detect_home_from_db(
            user) != "N/A" and calDistance(
                user_home, detect_home_from_db(user)) < TOLERANCE:
        return True
    return False