Ejemplo n.º 1
0
def AllwayConn(waysjsonpath):
    """
    对左右的路段进行连通性判断
    permutations(Allwayset,2) 二二组合,(A,B,C)->AB AC BA BC CA CB
    :param waysjsonpath:所有路段json文件路径
    :return:
    """
    AllwaysLists = []
    with open(waysjsonpath, 'r') as file:
        dic = json.loads(file.read())
    for key in dic.keys():
        AllwaysLists.append(key)
    Allwaysset = set(AllwaysLists)
    for twowaytuple in permutations(Allwaysset, 2):
        print(f"正在查看路段:{twowaytuple[0]}与路段:{twowaytuple[1]}的连通性......")
        connectroute = Common_Functions.InquireConn(twowaytuple[0],
                                                    twowaytuple[1],
                                                    "connects")  # 先查表
        # connectroute = -1
        if connectroute != 0 and connectroute != 1:  # 表中没有记录 再用简易导航
            connectroute = MapNavigation.waytoway(twowaytuple[0],
                                                  twowaytuple[1])  # 为列表
            if connectroute:
                print(
                    f"路段{twowaytuple[0]}---->{twowaytuple[1]}的路线:{connectroute}"
                )
            else:
                print(f"路段{twowaytuple[0]}不能到达路段{twowaytuple[1]}")
        else:
            print("数据库中已存在,跳过")
Ejemplo n.º 2
0
def AllwayConn(waysjsonpath):
    """
    对左右的路段进行连通性判断
    permutations(Allwayset,2) 二二组合,(A,B,C)->AB AC BA BC CA CB
    :param waysjsonpath:所有路段json文件路径
    :return:
    """
    AllwaysLists = []
    with open(waysjsonpath, 'r') as file:
        dic = json.loads(file.read())
    for key in dic.keys():
        AllwaysLists.append(key)
    Allwaysset = set(AllwaysLists)
    for twowaytuple in permutations(Allwaysset, 2):
        try:
            print(f"正在查看路段:{twowaytuple[0]}与路段:{twowaytuple[1]}的连通性......")
            way1_x, way1_y = Getway_startendnode_coordi(
                twowaytuple[0])  # 路段1的起点坐标
            way2_x, way2_y = Getway_startendnode_coordi(
                twowaytuple[1])  # 路段2的起始坐标
            print(way1_x, way1_y, way2_x, way2_y)
            if way1_x != 0 and way1_y != 0 and way2_x != 0 and way2_y != 0:
                waydis = Common_Functions.haversine(way1_x, way1_y, way2_x,
                                                    way2_y)
                print(waydis)
                if waydis < 3:
                    connectroute = Common_Functions.InquireConn(
                        twowaytuple[0], twowaytuple[1], "connects")  # 先查表
                    # connectroute = -1
                    if connectroute != 0 and connectroute != 1:  # 表中没有记录 再用简易导航
                        connectroute = MapNavigation.waytoway(
                            twowaytuple[0], twowaytuple[1])  # 为列表
                        if connectroute:
                            print(
                                f"路段{twowaytuple[0]}---->{twowaytuple[1]}的路线:{connectroute}"
                            )
                        else:
                            print(f"路段{twowaytuple[0]}不能到达路段{twowaytuple[1]}")
                    else:
                        print("数据库中已存在,跳过")
                else:
                    pass
        except Exception as e:
            print(e)
Ejemplo n.º 3
0
def SelectFinalRoutes(candidatewaypath, savefinalroutespath):
    """
    本函数与SelectFinalRoute功能相同,只是本函数处理完整的轨迹时,如果大于50.会重新开始计算最大连通性
    根据坐标点的候选路段选出路网的匹配路线
    保存格式为:车辆名:路线(如果不确定,可能为多条),车辆名为txt文件名
    :param candidatewaypath:  坐标点候选路段的txt文件路径,如H:\\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt
    :param savefinalroutespath: 最终路线保存路径
    :return:
    """
    #file = open("H:\GPS_Data\Road_Network\BYQBridge\FinalRoutes\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a')
    max_connect = 0  #记录计算最大连通的轨迹点数
    (tempath, tempfilename) = os.path.split(
        candidatewaypath)  # tempfilename为txt文件名(包含后缀)
    (trunkname, extension) = os.path.splitext(
        tempfilename)  # filename 为传入的csv文件名 extension为后缀
    savetxtfilename = trunkname + '.txt'
    Resetflag = 0  #标记最终路段是否重新开始计算
    file = open(os.path.join(savefinalroutespath, savetxtfilename), 'a')
    with open(candidatewaypath) as candidatewayfile:
        filelines = candidatewayfile.readlines()
        linesnum = len(filelines)
        finalline = []  #存储最终路线,可能为多条,随着坐标点的迭代,会变化,直到处理完最有一个坐标点
        for key in eval(filelines[0].strip('\n').split(">>>")[-1]).keys():
            finalline.append([key])
        #print(finalline)
        # 遍历每个坐标点的候选路段
        print("需要处理坐标数为:{}".format(linesnum))
        for lineindex in range(1, linesnum):
            print(f"正在处理第{lineindex+1}个坐标")
            if filelines[lineindex].strip('\n').split(
                    ">>>")[-1] == "BreakPoint":  #轨迹分段,分开计算
                file.write("New_Road\n")
                for sub in finalline:
                    file.write(str(sub) + "\n")
                    file.flush()
                Resetflag = 1  #标记从下一个点要重新开始匹配轨迹了
                max_connect = 0
                continue
            if max_connect == 100:
                file.write("New_Road\n")
                for sub in finalline:
                    file.write(str(sub) + "\n")
                    file.flush()
                Resetflag = 1  #标记从下一个点要重新开始匹配轨迹了
                max_connect = 0
                continue
            if Resetflag == 1:
                finalline.clear()
                for key in eval(filelines[lineindex].strip('\n').split(">>>")
                                [-1]).keys():
                    finalline.append([key])
                Resetflag = 0
                continue
            templine = []  #存储临时路线
            #print("处理坐标{}:{}".format(lineindex,eval(filelines[lineindex].strip('\n').split(">>>")[-1])))
            #print("处理路段{}".format(eval(filelines[lineindex].strip('\n').split(">>>")[-1])))
            connect_next_flag = 0  #记录下一步是否能有能走通的路段
            for subline in finalline:  #遍历每一条的候选路线
                for key in eval(filelines[lineindex].strip('\n').split(">>>")
                                [-1]).keys():  #遍历每个轨迹点的候选路段
                    #此代码块只加入key,不加入完整路线
                    print("路段:{}匹配key:{}".format(subline[-1], key))
                    # 只需要查看subline的最后一个路段与路段key是否连通即可,因为subline的连通性是通过测试的
                    connectroute = Common_Functions.InquireConn(
                        subline[-1], key, "connects")  #先查表
                    #connectroute = -1
                    if connectroute != 0 and connectroute != 1:  #表中没有记录 再用简易导航
                        connectroute = MapNavigation.waytoway(
                            subline[-1], key)  # 为列表
                    if connectroute:
                        connect_next_flag = 1
                        temsubline = copy.deepcopy(subline)
                        temsubline.append(key)  # 只加入轨迹点所属路段,而不加入这两个路段走通的路线
                        templine.append(temsubline)
                    else:
                        # 此路线不连通,舍弃当前路段key
                        pass
                    """
                    #此代码块是加入完整路线
                    #connectroute = Common_Functions.InquireConn(subline[-1], key,"connects")   #先查表
                    #connectroute = -1
                    connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key)  # 为列表
                    if subline[-1] == key:
                        temsubline = copy.deepcopy(subline)
                        templine.append(temsubline)
                    #elif connectroute !=0 and connectroute!= 1:   #表中没有记录 再用简易导航
                        #connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key)  # 为列表
                    elif connectroute:
                        #路段可连通
                        temsubline = copy.deepcopy(subline)
                        temsubline.extend(connectroute[1:])   #将走通的路线加入到子路线,扩展当前路线
                        templine.append(temsubline)
                    else:pass
                    """
            if connect_next_flag == 0:  #所有的候选路线与当前轨迹点的候选路段均不能相通,跳过此轨迹点
                pass
            else:
                finalline.clear()
                # print(templine)
                finalline = Common_Functions.DoubleDel(templine)  # 去相邻重复 再去重
                finalline = Common_Functions.Main_Auxiliary_road(
                    finalline)  # 去除头尾路段一样的候选路线,路线只有一个路段 不会处理
                # print(finalline)
                #finalline = Common_Functions.Start_End(finalline)  # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3]  去除路段多的,如果包含路段数量一致 暂不处理
                finalline = Common_Functions.Sequential_subset(
                    finalline)  # 最后去路线(至少两个及以上的其他路线是其前缀)
                max_connect += 1
        file.write("New_Road\n")
        for sub in finalline:
            file.write(str(sub) + "\n")
            file.flush()
        file.close()
Ejemplo n.º 4
0
def SelectFinalRoutes(candidatewaypath, savefinalroutespath):
    """
    本函数与SelectFinalRoute功能相同,只是本函数处理完整的轨迹时,如果大于50.会重新开始计算最大连通性
    根据坐标点的候选路段选出路网的匹配路线
    保存格式为:车辆名:路线(如果不确定,可能为多条),车辆名为txt文件名
    :param candidatewaypath:  坐标点候选路段的txt文件路径,如H:\\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt
    :param savefinalroutespath: 最终路线保存路径
    :return:
    """
    #file = open("H:\GPS_Data\Road_Network\BYQBridge\FinalRoutes\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a')
    max_connect = 0
    (tempath, tempfilename) = os.path.split(
        candidatewaypath)  # tempfilename为txt文件名(包含后缀)
    (trunkname, extension) = os.path.splitext(
        tempfilename)  # filename 为传入的csv文件名 extension为后缀
    savetxtfilename = trunkname + '.txt'
    file = open(os.path.join(savefinalroutespath, savetxtfilename), 'a')
    with open(candidatewaypath) as candidatewayfile:
        filelines = candidatewayfile.readlines()
        linesnum = len(filelines)
        finalline = []  #存储最终路线,可能为多条,随着坐标点的迭代,会变化,直到处理完最有一个坐标点
        for key in eval(filelines[0].strip('\n').split(">>>")[-1]).keys():
            finalline.append([key])
        predict = eval(filelines[0].strip('\n').split(">>>")[-1])
        # 遍历每个坐标点的候选路段
        print(f"需要处理坐标数为:{linesnum}")

        for lineindex in range(1, linesnum):
            templine = []  #存储临时路线
            curdict = eval(filelines[lineindex].strip('\n').split(">>>")[-1])
            flag = 0  #标记之前的路线与当前轨迹点的所有候选路段是否能连通,不能连通,舍弃该轨迹点
            newdic = {}
            for subline in finalline:  #遍历每一条的候选路线
                #print(f"ID{lineindex-1}:{predict},{curdict}")
                for key in curdict.keys():  #遍历每个轨迹点的候选路段
                    connectroute = Common_Functions.InquireConn(
                        subline[-1], key, "connects")  #先查表
                    if connectroute != 0:
                        #print(subline[-1], predict[subline[-1]], key, curdict[key])
                        connectroute = GridMapNavigation.CandidateWay_Connect(
                            subline[-1], predict[subline[-1]], key,
                            curdict[key])  # 双重列表
                        #print(f"next:{connectroute}")
                        if connectroute:
                            flag = 1
                            for line in connectroute:
                                temsubline = copy.deepcopy(subline)
                                temsubline.extend(line)  # 将走通的路线加入到子路线,扩展当前路线
                                templine.append(temsubline)
                                #newdic[line[-1]] = curdict[line[-1]]
                        else:
                            pass
                    else:
                        pass

            if flag == 1:
                finalline.clear()
                predict = curdict
                templine = Common_Functions.DoubleDel(templine)
                templine = Common_Functions.Main_Auxiliary_road(
                    templine)  #去除头尾路段一样的候选路线,路线只有一个路段 不会处理
                templine = Common_Functions.Start_End(
                    templine
                )  # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3]  去除路段多的,如果包含路段数量一致 暂不处理
                Common_Functions.Sequential_subset(
                    finalline)  # 最后去路线(至少两个及以上的其他路线是其前缀)
                for sub in templine:
                    finalline.append(Common_Functions.del_adjacent(sub))
            else:
                pass
            #print(f"final:{finalline}")
        print("共选出{}条路".format(len(finalline)))
        for sub in finalline:
            print(sub)
            file.write(str(sub) + "\n")
        file.close()
Ejemplo n.º 5
0
def SelectFinalRoute(candidatewaypath, savefinalroutespath):
    """
    根据坐标点的候选路段选出路网的匹配路线
    保存格式为:车辆名:路线(如果不确定,可能为多条),车辆名为txt文件名
    :param candidatewaypath:  坐标点候选路段的txt文件路径,如H:\\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt
    :param savefinalroutespath: 最终路线保存路径
    :return:
    """
    #file = open("H:\GPS_Data\Road_Network\BYQBridge\FinalRoutes\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a')

    (tempath, tempfilename) = os.path.split(
        candidatewaypath)  # tempfilename为txt文件名(包含后缀)
    (trunkname, extension) = os.path.splitext(
        tempfilename)  # filename 为传入的csv文件名 extension为后缀
    savetxtfilename = trunkname + '.txt'
    file = open(os.path.join(savefinalroutespath, savetxtfilename), 'a')
    with open(candidatewaypath) as candidatewayfile:
        filelines = candidatewayfile.readlines()
        linesnum = len(filelines)
        finalline = []  #存储最终路线,可能为多条,随着坐标点的迭代,会变化,直到处理完最有一个坐标点
        for key in eval(filelines[0].strip('\n').split(">>>")[-1]).keys():
            finalline.append([key])
        #print(finalline)
        # 遍历每个坐标点的候选路段
        print("需要处理坐标数为:{}".format(linesnum))
        for lineindex in range(1, linesnum):
            templine = []  #存储临时路线
            # 遍历到最后一行
            print(len(finalline))
            print(finalline)
            print("处理坐标{}:{}".format(
                lineindex,
                eval(filelines[lineindex].strip('\n').split(">>>")[-1])))
            #print("处理路段{}".format(eval(filelines[lineindex].strip('\n').split(">>>")[-1])))
            for subline in finalline:
                for key in eval(filelines[lineindex].strip('\n').split(">>>")
                                [-1]).keys():
                    temsubline = []

                    #此代码块只加入key,不加入完整路线
                    print("路段:{}匹配key:{}".format(subline[-1], key))
                    # 只需要查看subline的最后一个路段与路段key是否连通即可,因为subline的连通性是通过测试的
                    connectroute = Common_Functions.InquireConn(
                        subline[-1], key, "connects")  #先查表
                    #connectroute = -1
                    if connectroute != 0 and connectroute != 1:  #表中没有记录 再用简易导航
                        connectroute = MapNavigation.waytoway(
                            subline[-1], key)  # 为列表
                    if connectroute:
                        temsubline = copy.deepcopy(subline)
                        temsubline.append(key)  # 只加入轨迹点所属路段,而不加入这两个路段走通的路线
                        templine.append(temsubline)
                    else:
                        # 此路线不连通,舍弃当前路段key
                        pass
                    """
                    #此代码块是加入完整路线
                    #connectroute = Common_Functions.InquireConn(subline[-1], key,"connects")   #先查表
                    #connectroute = -1
                    connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key)  # 为列表
                    if subline[-1] == key:
                        temsubline = copy.deepcopy(subline)
                        templine.append(temsubline)
                    #elif connectroute !=0 and connectroute!= 1:   #表中没有记录 再用简易导航
                        #connectroute = MapNavigation.Nodirectionwaytoway(subline[-1], key)  # 为列表
                    elif connectroute:
                        #路段可连通
                        temsubline = copy.deepcopy(subline)
                        temsubline.extend(connectroute[1:])   #将走通的路线加入到子路线,扩展当前路线
                        templine.append(temsubline)
                    else:pass
                    """

                    # print(temsubline)
                    # print(templine)
            finalline.clear()
            #print(templine)
            finalline = Common_Functions.DoubleDel(templine)  #去相邻重复 再去重
            finalline = Common_Functions.Main_Auxiliary_road(
                finalline)  #去除头尾路段一样的候选路线,路线只有一个路段 不会处理
            #print(finalline)
            finalline = Common_Functions.Start_End(
                finalline
            )  # 对于[wayid1,wayid2,wayid3] [wayid1,wayid4,wayid5,wayid3]  去除路段多的,如果包含路段数量一致 暂不处理
            finalline = Common_Functions.Sequential_subset(
                finalline)  # 最后去路线(至少两个及以上的其他路线是其前缀)
        print("共选出{}条路".format(len(finalline)))
        for sub in finalline:
            file.write(str(sub) + "\n")
            file.flush()
        print(finalline)
        file.close()