예제 #1
0
def FindPointCandidateWay_Grid(csvfilepath, candidatewaypath,
                               candidatewayname):
    """
    :param csvfilepath: csv文件路径 例:H:\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv"
    :param candidatewaypath:  轨迹点候选路段保存路径
    :param candidatewayname: 候选路段保存的文件名

    :return:
    """
    if not os.path.isdir(candidatewaypath):
        os.mkdir(candidatewaypath)
    # 读时间 经纬度 网格编号
    #df = pd.read_csv("H:\GPS_Data\Road_Network\BYQBridge\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv",header=None, usecols=[1, 2, 3, 4, 5])
    df = pd.read_csv(csvfilepath, header=None, usecols=[1, 2, 3, 4, 5])
    points_num = df.shape[0]  # 坐标数量
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    # print(df.iloc[:,1:4])
    drop_list = []  # 要删除的索引列表
    for row in range(1, points_num):
        if row == points_num:
            break
        points_dis = Common_Functions.haversine(df.iloc[row, 1], df.iloc[row,
                                                                         2],
                                                df.iloc[row - 1, 1],
                                                df.iloc[row - 1,
                                                        2])  # 相邻坐标点之间的距离
        if points_dis < 0.01:  # 距离小于10米
            drop_list.append(row)
    newdf = df.drop(drop_list)  # 删除相邻点在10米之内的点
    newdf = newdf.reset_index(drop=True)
    txtname = candidatewayname + ".txt"
    file = open(os.path.join(candidatewaypath, txtname), 'a')

    print("本轨迹段共查找坐标点数为:{}".format(newdf.shape[0]))
    for row in range(newdf.shape[0]):
        dic = {}
        #candidatacode = BigGridCode.Neighbor(newdf.iloc[row, 1], newdf.iloc[row, 2])  # 查找附近八个区域
        # for subcode in candidatacode:
        #     if subcode:
        #         waysets = list(Common_Functions.GetWay_ByGridCode(subcode[1]))
        #         for subway in waysets:
        #             dic[subway[0]] = subcode[1]
        pointCode = BigGridCode.Encode(newdf.iloc[row, 1], newdf.iloc[row, 2])
        waysets = list(Common_Functions.GetWay_ByGridCode(pointCode[1]))
        if waysets:
            for subway in waysets:
                dic[subway[0]] = pointCode[1]

        #print(f"{newdf.iloc[row, 1]},{newdf.iloc[row, 2]}CandidateWay>>>{str(dic)}")
        if dic:
            file.write("PointID-" + str(row + 1) + "CandidateWay>>>" +
                       str(dic) + "\n")
    file.close()
예제 #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)
예제 #3
0
def TrajectorySplit(csvfilepath, processpath):
    """
    :param csvfilepath: 原始轨迹csv文件
    :param processpath: 存储原轨迹分段之后的csv文件
    :return:
    """
    (path, file) = os.path.split(csvfilepath)
    (filename, extension) = os.path.splitext(file)
    filesavepath = os.path.join(processpath, filename)  #文件分段之后保存的路径
    if not os.path.isdir(filesavepath):
        os.mkdir(filesavepath)
    df = pd.read_csv(csvfilepath,
                     header=None,
                     usecols=[1, 2, 3, 4, 5],
                     names=[0, 1, 2, 3, 4])

    df = df.sort_values(by=0)  # 时间排序
    points_num = df.shape[0]  # 坐标数量
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    drop_list = []  # 要删除的索引列表
    df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0], format="%Y%m%d ")
    for row in range(1, points_num):
        if row == points_num:
            break
        points_dis = Common_Functions.haversine(df.iloc[row, 1], df.iloc[row,
                                                                         2],
                                                df.iloc[row - 1, 1],
                                                df.iloc[row - 1,
                                                        2])  # 相邻坐标点之间的距离
        Time_difference = (df.iloc[row, 0] -
                           df.iloc[row - 1, 0]).seconds  #相邻两个坐标点的时间差,单位:秒
        if points_dis < 0.01:  # 距离小于10米
            drop_list.append(row)
        if Time_difference != 0:
            speed = points_dis / Time_difference  #速度 秒
        else:
            speed = 0
        if speed >= 0.034:
            drop_list.append(row)  #时速大于120 ,0.034时速为122

    newdf = df.drop(drop_list)  # 删除相邻点在10米之内的点和时速大于120的异常点
    newdf = newdf.reset_index(drop=True)
    #Candidate_pointName = filename + ".csv"
    #newdf.to_csv(os.path.join(filesavepath, os.path.join()), index=0,header=0, encoding='utf_8_sig')  #筛选出的被匹配的轨迹点
    #以上为处理所有的相邻点距离小于10米的坐标
    #以下循环为分段
    start_row = 0  #文件分割的起始行数
    endrow = 0  #结束行数
    part_num = 1
    part_name = "part" + str(part_num) + ".csv"
    #tem_df = pd.DataFrame(None)
    Is_Save_flag = 0  #记录该轨迹段是否保存
    for row in range(1, newdf.shape[0]):
        #Time_difference = (newdf.iloc[row, 0] - newdf.iloc[row - 1, 0]).seconds  # 相邻两个坐标点的时间差,单位:秒
        Adjacent_two_points_dis = Common_Functions.haversine(
            newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row - 1, 1],
            newdf.iloc[row - 1, 2])  # 相邻坐标点之间的距离
        if Adjacent_two_points_dis > 5:
            endrow = row - 1
            Is_Save_flag = 1
        if row == newdf.shape[0] - 1:
            #tem_df = newdf.loc[start_row:row+1, :]
            endrow = row
            Is_Save_flag = 1
        if Is_Save_flag == 1:
            newdf.loc[start_row:endrow, :].to_csv(os.path.join(
                filesavepath, part_name),
                                                  index=0,
                                                  header=0,
                                                  encoding='utf_8_sig')
            start_row = endrow + 1
            Is_Save_flag = 0
            #tem_df.drop(tem_df.index, inplace=True)
            part_num += 1
            part_name = "part" + str(part_num) + ".csv"
예제 #4
0
def FindPointCandidateWay(csvfilepath, candidatewaypath, candidatewayname):
    """
    处理北野场桥
    找出坐标点的候选路段,此部分已经通过角度(大于90)、距离(大于30米)筛除一部分候选路段
    :param csvfilepath: csv文件路径 例:H:\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv"
    :param candidatewaypath:  轨迹点候选路段保存路径
    :param candidatewayname: 候选路段保存的文件名

    :return:
    """
    if not os.path.isdir(candidatewaypath):
        os.mkdir(candidatewaypath)
    # 读时间 经纬度 网格编号
    #df = pd.read_csv("H:\GPS_Data\Road_Network\BYQBridge\TrunksArea\\334e4763-f125-425f-ae42-8028245764fe.csv",header=None, usecols=[1, 2, 3, 4, 5])
    df = pd.read_csv(csvfilepath, header=None, usecols=[1, 2, 3, 4, 5])
    points_num = df.shape[0]  # 坐标数量
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    # print(df.iloc[:,1:4])
    drop_list = []  # 要删除的索引列表
    for row in range(1, points_num):
        if row == points_num:
            break
        points_dis = Common_Functions.haversine(df.iloc[row, 1], df.iloc[row,
                                                                         2],
                                                df.iloc[row - 1, 1],
                                                df.iloc[row - 1,
                                                        2])  # 相邻坐标点之间的距离
        if points_dis < 0.01:  # 距离小于10米
            drop_list.append(row)
    # print(drop_list)
    newdf = df.drop(drop_list)  # 删除相邻点在10米之内的点
    # print(newdf.iloc[:,1:3])
    newdf = newdf.reset_index(drop=True)
    #file = open("H:\GPS_Data\Road_Network\BYQBridge\CandidateWay\\NewStrategy\\334e4763-f125-425f-ae42-8028245764fe.txt", 'a')
    txtname = candidatewayname + ".txt"
    file = open(os.path.join(candidatewaypath, txtname), 'a')
    print("本轨迹段共查找坐标点数为:{}".format(newdf.shape[0]))
    for row in range(newdf.shape[0]):
        if row == 0:
            #print("处理起始坐标点{}".format([newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4]]))
            dic = Common_Functions.Find_Candidate_Route([
                newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3],
                newdf.iloc[row, 4]
            ], [
                newdf.iloc[row + 1, 1], newdf.iloc[row + 1, 2],
                newdf.iloc[row + 1, 3], newdf.iloc[row + 1, 4]
            ],
                                                        flag=1)
            if dic:  # 有候选路段才保存
                file.write("PointID-" + str(row + 1) + "CandidateWay>>>" +
                           str(dic) + "\n")
        elif row == newdf.shape[0] - 1:
            #print("处理终点坐标点{}".format([df.iloc[row - 1, 2], df.iloc[row - 1, 3], df.iloc[row, 2], df.iloc[row, 3]]))
            dic = Common_Functions.Find_Candidate_Route([
                newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2],
                newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4]
            ], [
                newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3],
                newdf.iloc[row, 4]
            ],
                                                        flag=2)
            if dic:
                file.write("PointID-" + str(row + 1) + "CandidateWay>>>" +
                           str(dic) + "\n")

        else:
            dis1 = Common_Functions.haversine(newdf.iloc[row,
                                                         1], newdf.iloc[row,
                                                                        2],
                                              newdf.iloc[row - 1, 1],
                                              newdf.iloc[row - 1, 2])
            dis2 = Common_Functions.haversine(newdf.iloc[row,
                                                         1], newdf.iloc[row,
                                                                        2],
                                              newdf.iloc[row + 1, 1],
                                              newdf.iloc[row + 1, 2])
            # 找相邻最近的点做为轨迹方向
            if dis2 > dis1:
                #print("处理终点坐标点{}".format([newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2], newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4]]))
                dic = Common_Functions.Find_Candidate_Route([
                    newdf.iloc[row - 1, 1], newdf.iloc[row - 1, 2],
                    newdf.iloc[row - 1, 3], newdf.iloc[row - 1, 4]
                ], [
                    newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3],
                    newdf.iloc[row, 4]
                ],
                                                            flag=2)

            else:
                #print("处理起始坐标点{}".format([newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3], newdf.iloc[row, 4]]))
                dic = Common_Functions.Find_Candidate_Route([
                    newdf.iloc[row, 1], newdf.iloc[row, 2], newdf.iloc[row, 3],
                    newdf.iloc[row, 4]
                ], [
                    newdf.iloc[row + 1, 1], newdf.iloc[row + 1, 2],
                    newdf.iloc[row + 1, 3], newdf.iloc[row + 1, 4]
                ],
                                                            flag=1)
            if dic:
                file.write("PointID-" + str(row + 1) + "CandidateWay>>>" +
                           str(dic) + "\n")

    file.close()