1223西站坐标更新

2023-12-23 23:56:26

1223 西站坐标更新

1.Update for the station’s location

    def initial_out_map_indoor_points(self):
        '''
            Load the indoor data and update both the wall_matrix and the ditch_matrix.
        '''
        # Initialize the wall_matrix
        # List of coordinates
        coordinates = [
            (417, 287, 417, 290),
            (414, 254, 414, 257),
            (412, 222, 412, 225),
            (411, 209, 411, 211),
            (411, 203, 411, 205),
            (567, 275, 567, 276),
            (566, 268, 566, 270),
            (566, 261, 566, 263),
            (565, 247, 565, 249),
            (563, 215, 563, 218),
            (561, 189, 561, 192),
            (407, 238, 407, 245),
            (570, 226, 570, 234),
            (464, 291, 466, 292),
            (518, 288, 521, 288),
            (457, 187, 459, 187),
            (511, 183, 513, 183)
        ]
        coordinates = self.sort_segments_clockwise(coordinates)

        last_pointx,last_pointy=0,0
        temp_no=0

        # Fill in the wall_matrix
        for x1, y1, x2, y2 in coordinates:
            # apply the process of wall's calculation
            points = bresenham_line(x1, y1, x2, y2)  # find all the points within the straight line
            for x, y in points:
                if 0 <= x < len(self.wall_matrix) and 0 <= y < len(
                        self.wall_matrix[0]):
                    self.wall_matrix[int(x), int(y)] = 1  # Remember the location of the wall.
            if temp_no>=1 and calculate_distance(last_pointx,last_pointy,x1,y1)<=1000:
                points=bresenham_line(last_pointx,last_pointy,x1,y1)
                for x, y in points:
                    if 0 <= x < len(self.wall_matrix) and 0 <= y < len(
                            self.wall_matrix[0]):
                        self.wall_matrix[int(x), int(y)] = 1  # Remember the location of the wall.
            # if calculate_distance(last_pointx,last_pointy,x1,y1)>100:
            #     print(f'Out of range:(x1:{x},y1:{y})')
            temp_no=temp_no+1
            last_pointx,last_pointy=x2,y2
        begin_x1,begin_y1,begin_x2,begin_y2=coordinates[0]
        print(f'begin_x1:{begin_x1},begin_y1:{begin_y1},begin_x2:{begin_x2},begin_y2:{begin_y2}')
        points = bresenham_line(begin_x1, begin_y1, x2, y2)  # find all the points within the straight line
        for x, y in points:
            if 0 <= x < len(self.wall_matrix) and 0 <= y < len(
                    self.wall_matrix[0]):
                self.wall_matrix[int(x), int(y)] = 1  # Remember the location of the wall.

        self.wall_matrix = self.fill_area(self.wall_matrix, target_value=1)
        # Update the location to the overall matrix
        self.outdoor_label[self.wall_matrix == 1] = 1

        df=pd.DataFrame(self.wall_matrix)
        df.to_csv('G:/HZXZ/Hws-Mirror-City/water_indoor/Model2_data/outdoor_data/temp_data/wall_matrix.csv', index=False)

        # label the location of the door
        current_dir = os.path.dirname(os.path.abspath(__file__))
        data_path = os.path.join(current_dir, 'Model2_data/outdoor_data/out_in_map_points.xlsx')
        data = pd.read_excel(data_path)
        for _, row in data.iterrows():
            # load the door coordinates and finish the transfer
            id, x1, y1, x2, y2 = row['id'], row['x1'], row['y1'], row['x2'], row['y2']
            x1, y1 = self.indoor_transfer.cad2ue(x1, y1)
            x2, y2 = self.indoor_transfer.cad2ue(x2, y2)
            index_x1, index_y1 = self.outdoor_transer.ue2index_model2(x1, y1,self.scaled_width,self.scaled_height)
            index_x2, index_y2 = self.outdoor_transer.ue2index_model2(x2, y2,self.scaled_width,self.scaled_height)
            index_x1, index_y1, index_x2, index_y2 = int(index_x1), int(index_y1), int(index_x2), int(index_y2)
            if index_y1 > index_y2:
                tmp = index_y1
                index_y1 = index_y2
                index_y2 = tmp
            # print(f'x1:{index_x1}, x2:{index_x2}, y1:{index_y1}, y2:{index_y2}')

            # label the location of the indoor doors
            if index_x1 == index_x2:
                self.outdoor_label[index_x1, index_y1:index_y2] = 5
            elif index_y1 == index_y2:
                self.outdoor_label[index_x1:index_x2, index_y1] = 5
            else:
                self.outdoor_label[index_x1:index_x2, index_y1:index_y2] = 5
        # self.wall_matrix = self.fill_area(self.wall_matrix, target_value=1) # fill the circled area

新增函数

    def calculate_midpoint(self,segment):
        return ((segment[0] + segment[2]) / 2, (segment[1] + segment[3]) / 2)

    def calculate_centroid(self,segments):
        x_sum, y_sum = 0, 0
        for segment in segments:
            midpoint = self.calculate_midpoint(segment)
            x_sum += midpoint[0]
            y_sum += midpoint[1]
        return x_sum / len(segments), y_sum / len(segments)

    def calculate_angle(self,centroid, point):
        return math.atan2(point[1] - centroid[1], point[0] - centroid[0])

    def sort_segments_clockwise(self,segments):
        centroid = self.calculate_centroid(segments)
        return sorted(segments, key=lambda segment: self.calculate_angle(centroid, self.calculate_midpoint(segment)))

更细的函数

    def showOutdoorImg(self, outdoor_acc_water_matrix):
        """
        opencv可视化降雨量矩阵结果
        input: rainfall_matrix
        output: null
        """
        # 获取矩阵中的最大值和对应的下标
        max_value = np.max(outdoor_acc_water_matrix)
        max_index = np.argmax(outdoor_acc_water_matrix)

        # 将一维下标转换为二维下标
        max_index_2d = np.unravel_index(max_index, outdoor_acc_water_matrix.shape)
        print(f'the largest water logging is {max_value}, the index is {max_index_2d}')

        mat = np.transpose(np.copy(outdoor_acc_water_matrix))[::-1]

        # 归一化矩阵
        # mat = cv2.normalize(mat, None, 200, 250, cv2.NORM_MINMAX, dtype=cv2.CV_8UC3)
        mat = mat / 300 * 250
        mat[mat > 250] = 250
        mat[mat < 30] = 30
        mat = mat.astype(np.uint8)
        mat = cv2.cvtColor(mat, cv2.COLOR_RGB2BGR)
        # 自定义颜色映射表
        custom_colormap = create_custom_colormap()

        # 将矩阵映射到蓝色色域
        image = cv2.applyColorMap(mat, custom_colormap)

        label = np.transpose(self.outdoor_label)[::-1]
        outdoor_acc_water_matrix = np.transpose(outdoor_acc_water_matrix)[::-1]
        image[outdoor_acc_water_matrix < 2] = [255, 255, 255]  # 积水为0设置为白色
        image[label == 1] = [0,255,255]  # 墙体设置为黄色
        image[label == 2] = 0  # 路面设置为黑色
        image[label == 3] = [0, 0, 255]  # 河流部分设置为(30,144,255)
        image[label == 5] = [0, 255, 0]  # 河流部分设置为(30,144,255)
        # image[label == 4] = [0, 255, 0]
        image[label == 9] = [25, 74, 230]

        image = cv2.resize(image, None, None, 1, 1, cv2.INTER_NEAREST)
        # 插入西站图片
        # west_station_img = cv2.imread('Model2_data/outdoor_data/west_station.png')
        # x, y = int(108*self.resize_scale), int(54*self.resize_scale)
        # west_station_img = cv2.resize(west_station_img, None, None, self.resize_scale, self.resize_scale, cv2.INTER_NEAREST)
        # image[y:y + west_station_img.shape[0], x:x + west_station_img.shape[1]] = west_station_img
        cv2.imwrite(f'result/outdoor_imgs/time_stamp{self.time_stamp}.png', image)
        cv2.imwrite(f'Model2_data/outdoor_data/temp_data/time_stamp{self.time_stamp}.png', image)
        if self.__debug_mode:
            # 显示图像
            cv2.imshow("OutdoorImage", image)
            cv2.waitKey(500)
            # cv2.destroyAllWindows()
    def initialMatrix(self):
        '''
        水体系统,初步生成室外降水 渗透 地势三个matrix
            0.土壤
            1.墙体
            2.道路
            3.河流
            4.沟渠
            5.室内外映射点位
            9.易涝点
        初步生成室内地势matrix
            1.ditch
            2.wall
            3.Stair
            4.RoadStair
        '''
        # 初始化outdoor_label矩阵
        soil_label = 0
        walls_label = 1
        roads_label = 2
        river_label = 3
        ditch_label = 4
        infinite = np.inf  # 设置较大的值代表河流的渗透是无穷的
        infiltration_standard = 2  # 渗透量国标
        # 初始化地势图中各处的高度
        walls_height = np.inf
        roads_height = 0
        river_height = -1
        ditch_height = -10
        # 室内初始化
        self.indoor_topographic_matrix[self.indoor_label == 1] = ditch_height
        self.indoor_topographic_matrix[self.indoor_label == 2] = infinite

        # path = 'Model2_data/outdoor_data/'
        # np.save(path+'road_matrix_test.npy', self.road_matrix)
        # road_matrix = np.load(path+'road_matrix_test.npy')
        # 室外初始化
        # 对标签矩阵作转换,转换到UE矩阵中去
        # self.wall_matrix = self.outdoor_transer.four_point_transform(self.wall_matrix)
        # self.river_matrix = self.outdoor_transer.four_point_transform(self.river_matrix)
        # self.ditch_matrix = self.outdoor_transer.four_point_transform(self.ditch_matrix)
        # self.road_matrix = self.outdoor_transer.four_point_transform(self.road_matrix)
        # 初始化降雨矩阵
        soil_mask = (self.wall_matrix == 0) & (self.road_matrix == 0) & (self.river_matrix == 0) & (self.ditch_matrix == 0)
        self.rainfall_matrix[self.wall_matrix == 1] = 0
        self.rainfall_matrix[self.wall_matrix == 0] = 1

        # UE转为地势矩阵&初始化特殊地势矩阵
        self.initial_topographic_matrix()
        # print(f'topographic_matrix non zero points num is {np.count_nonzero(self.outdoor_topographic_matrix)}')
        self.outdoor_topographic_matrix[self.wall_matrix == 1] = walls_height
        # self.outdoor_topographic_matrix[self.road_matrix == 1] = roads_height
        # self.outdoor_topographic_matrix[self.river_matrix == 1] = river_height
        self.outdoor_topographic_matrix[self.ditch_matrix == 1] = ditch_height

        # 西站坐标初始化

        # random_values = np.random.randint(-10, 10, size=np.count_nonzero(soil_mask))
        # self.outdoor_topographic_matrix[soil_mask] = random_values

        # 初始化标签
        self.outdoor_label[soil_mask] = soil_label
        # Updated way of labeling both the station and the ditch
        self.initial_out_map_indoor_points()
        self.outdoor_label[self.road_matrix == 1] = roads_label
        # self.outdoor_label[self.river_matrix == 1] = river_label
        self.outdoor_label[self.ditch_matrix == 1] = ditch_label
        # self.outdoor_label[self.wall_matrix == 1] = walls_label
        # Realize this function.
        self.initial_river()
        df=pd.DataFrame(self.outdoor_label)
        df.to_csv('G:/HZXZ/Hws-Mirror-City/water_indoor/Model2_data/outdoor_data/temp_data/outdoor_label.csv', index=False)
        print(f'temporary numbers: {np.unique(self.outdoor_label)}')
        self.initial_prone_waterlogging_points()

        # 室内外映射点位初始化
        # for index in range(len(self.outdoor_map_indoor) // 2):
        #     for (i, j) in self.outdoor_map_indoor[f'outdoor_point{index + 1}']:
        #         self.outdoor_topographic_matrix[i][j] = 0
        #         self.outdoor_label[i][j] = 5
        pass

Final Output

在这里插入图片描述

文章来源:https://blog.csdn.net/m0_51952128/article/details/135175894
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。