def init_input_output(self, input_sp_file_name, directed_import_count_list_name, directed_import_count_matrix_name, directed_export_count_list_name, directed_export_count_matrix_name, directed_import_weight_list_name, directed_import_weight_matrix_name, directed_export_weight_list_name, directed_export_weight_matrix_name, undirected_import_count_list_name, undirected_import_count_matrix_name, undirected_export_count_list_name, undirected_export_count_matrix_name, undirected_import_weight_list_name, undirected_import_weight_matrix_name, undirected_export_weight_list_name, undirected_export_weight_matrix_name, header): self.source_sp_center = AISReader(input_sp_file_name, sp_index, sp_offset, label_index=sp_label_index) self.source_sp_center.start_fetch_data() self.directed_import_count_network.init_output_saver(directed_import_count_list_name, directed_import_count_matrix_name, header, ) self.directed_export_count_network.init_output_saver(directed_export_count_list_name, directed_export_count_matrix_name, header, ) self.directed_import_weight_network.init_output_saver(directed_import_weight_list_name, directed_import_weight_matrix_name, header, ) self.directed_export_weight_network.init_output_saver(directed_export_weight_list_name, directed_export_weight_matrix_name, header, ) self.undirected_import_count_network.init_output_saver(undirected_import_count_list_name, undirected_import_count_matrix_name, header, ) self.undirected_export_count_network.init_output_saver(undirected_export_count_list_name, undirected_export_count_matrix_name, header, ) self.undirected_import_weight_network.init_output_saver(undirected_import_weight_list_name, undirected_import_weight_matrix_name, header, ) self.undirected_export_weight_network.init_output_saver(undirected_export_weight_list_name, undirected_export_weight_matrix_name, header, )
def init_input_output( self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, sp_header, output_rp_file_name, rp_header, ): # input self.source_sp_area = AISReader( input_sp_file_name, sp_index, sp_offset, ) self.source_trajectory = AISReader( input_trajectory_file_name, trajectory_index, trajectory_offset, ) self.source_sp_area.start_fetch_data() self.source_trajectory.start_fetch_data() self.target_route_point.init_output_saver(output_sp_file_name, sp_header, output_rp_file_name, rp_header)
def init_input_output(self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, output_sp_header, output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, ): # 打开输入输出 self.source_sp_area = AISReader(input_sp_file_name, sp_index, sp_offset, sp_mark, ) self.source_trajectory = AISReader(input_trajectory_file_name, trajectory_index, trajectory_offset, ) self.source_sp_area.start_fetch_data() self.source_trajectory.start_fetch_data() self.target_sp_area.init_output_saver(output_sp_file_name, output_sp_header, ) self.target_trajectory.init_output_saver(output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, output_trajectory_point_name, output_trajectory_point_header)
def init_input_output( self, input_sp_file_name, input_rp_file_name, directed_info_file, directed_line_file, undirected_info_file, undirected_line_file, info_header, ): # input self.source_sp_center = AISReader(input_sp_file_name, sp_index, sp_offset, label_index=sp_label_index) self.source_rp_center = AISReader(input_rp_file_name, rp_index, rp_offset, label_index=rp_label_index) self.source_sp_center.start_fetch_data() self.source_rp_center.start_fetch_data() self.target_directed_trajectory.init_output_saver( directed_info_file, info_header, directed_line_file, None, None, ) self.target_undirected_trajectory.init_output_saver( undirected_info_file, info_header, undirected_line_file, None, None, )
class FlowNetworkService(object): def __init__(self): self.source_sp_center = None self.directed_import_count_network = FlowNetwork() self.directed_export_count_network = FlowNetwork() self.directed_import_weight_network = FlowNetwork() self.directed_export_weight_network = FlowNetwork() self.undirected_import_count_network = FlowNetwork() self.undirected_export_count_network = FlowNetwork() self.undirected_import_weight_network = FlowNetwork() self.undirected_export_weight_network = FlowNetwork() self.mark_dict = {} self.import_weight_matrix = None self.export_weight_matrix = None self.import_count_matrix = None self.export_count_matrix = None self.port_name_list = [] def form_flow_network(self, input_sp_file_name, directed_import_count_list_name, directed_import_count_matrix_name, directed_export_count_list_name, directed_export_count_matrix_name, directed_import_weight_list_name, directed_import_weight_matrix_name, directed_export_weight_list_name, directed_export_weight_matrix_name, undirected_import_count_list_name, undirected_import_count_matrix_name, undirected_export_count_list_name, undirected_export_count_matrix_name, undirected_import_weight_list_name, undirected_import_weight_matrix_name, undirected_export_weight_list_name, undirected_export_weight_matrix_name, header): self.init_input_output(input_sp_file_name, directed_import_count_list_name, directed_import_count_matrix_name, directed_export_count_list_name, directed_export_count_matrix_name, directed_import_weight_list_name, directed_import_weight_matrix_name, directed_export_weight_list_name, directed_export_weight_matrix_name, undirected_import_count_list_name, undirected_import_count_matrix_name, undirected_export_count_list_name, undirected_export_count_matrix_name, undirected_import_weight_list_name, undirected_import_weight_matrix_name, undirected_export_weight_list_name, undirected_export_weight_matrix_name, header) self.form_flow_network_transaction() self.finish_deal() def init_input_output(self, input_sp_file_name, directed_import_count_list_name, directed_import_count_matrix_name, directed_export_count_list_name, directed_export_count_matrix_name, directed_import_weight_list_name, directed_import_weight_matrix_name, directed_export_weight_list_name, directed_export_weight_matrix_name, undirected_import_count_list_name, undirected_import_count_matrix_name, undirected_export_count_list_name, undirected_export_count_matrix_name, undirected_import_weight_list_name, undirected_import_weight_matrix_name, undirected_export_weight_list_name, undirected_export_weight_matrix_name, header): self.source_sp_center = AISReader(input_sp_file_name, sp_index, sp_offset, label_index=sp_label_index) self.source_sp_center.start_fetch_data() self.directed_import_count_network.init_output_saver(directed_import_count_list_name, directed_import_count_matrix_name, header, ) self.directed_export_count_network.init_output_saver(directed_export_count_list_name, directed_export_count_matrix_name, header, ) self.directed_import_weight_network.init_output_saver(directed_import_weight_list_name, directed_import_weight_matrix_name, header, ) self.directed_export_weight_network.init_output_saver(directed_export_weight_list_name, directed_export_weight_matrix_name, header, ) self.undirected_import_count_network.init_output_saver(undirected_import_count_list_name, undirected_import_count_matrix_name, header, ) self.undirected_export_count_network.init_output_saver(undirected_export_count_list_name, undirected_export_count_matrix_name, header, ) self.undirected_import_weight_network.init_output_saver(undirected_import_weight_list_name, undirected_import_weight_matrix_name, header, ) self.undirected_export_weight_network.init_output_saver(undirected_export_weight_list_name, undirected_export_weight_matrix_name, header, ) def form_flow_network_transaction(self): self.init_matrix() self.fill_matrix() self.export_matrix() def init_matrix(self): self.mark_dict = {} self.mark_dict, offset = self.source_sp_center.fetch_unique_mark(self.mark_dict, 0) count = len(self.mark_dict.keys()) self.import_weight_matrix = np.zeros((count, count)) self.export_weight_matrix = np.zeros((count, count)) self.import_count_matrix = np.zeros((count, count)) self.export_count_matrix = np.zeros((count, count)) self.get_port_name_list(count) def get_port_name_list(self, count): self.port_name_list = [0 for _ in range(count)] for key, value in self.mark_dict.items(): self.port_name_list[value] = key def fill_matrix(self): while self.source_sp_center.has_next_data(): print("deal the trajectory with index: {}".format(self.source_sp_center.index)) port_list, _ = self.source_sp_center.fetch_data() self.fill_matrix_of_single_trajectory(port_list) def fill_matrix_of_single_trajectory(self, port_list): start_point, start_label = port_list[0] end_point, end_label = port_list[-1] # change_weight = calculate_change_dead_weight(start_point, end_point) change_weight = 1000 if math.fabs(change_weight) > change_weight_threshold: start_index, end_index = self.mark_dict[start_label], self.mark_dict[end_label] if change_weight < 0: self.import_weight_matrix[start_index, end_index] += change_weight self.import_count_matrix[start_index, end_index] += 1 else: self.export_weight_matrix[start_index, end_index] += -change_weight self.export_count_matrix[start_index, end_index] += 1 def export_matrix(self): self.directed_import_count_network.export_info(self.import_count_matrix, self.port_name_list) self.directed_import_count_network.export_matrix(self.import_count_matrix, self.port_name_list) self.directed_export_count_network.export_info(self.export_count_matrix, self.port_name_list) self.directed_export_count_network.export_matrix(self.export_count_matrix, self.port_name_list) self.directed_import_weight_network.export_info(self.import_weight_matrix, self.port_name_list) self.directed_import_weight_network.export_matrix(self.import_weight_matrix, self.port_name_list) self.directed_export_weight_network.export_info(self.export_weight_matrix, self.port_name_list) self.directed_export_weight_network.export_matrix(self.export_weight_matrix, self.port_name_list) self.form_undirected_matrix() self.undirected_import_count_network.export_info(self.import_count_matrix, self.port_name_list) self.undirected_import_count_network.export_matrix(self.import_count_matrix, self.port_name_list) self.undirected_export_count_network.export_info(self.export_count_matrix, self.port_name_list) self.undirected_export_count_network.export_matrix(self.export_count_matrix, self.port_name_list) self.undirected_import_weight_network.export_info(self.import_weight_matrix, self.port_name_list) self.undirected_import_weight_network.export_matrix(self.import_weight_matrix, self.port_name_list) self.undirected_export_weight_network.export_info(self.export_weight_matrix, self.port_name_list) self.undirected_export_weight_network.export_matrix(self.export_weight_matrix, self.port_name_list) def finish_deal(self): self.source_sp_center.close() self.directed_import_count_network.close() self.directed_export_count_network.close() self.directed_import_weight_network.close() self.directed_export_weight_network.close() self.undirected_import_count_network.close() self.undirected_export_count_network.close() self.undirected_import_weight_network.close() self.undirected_export_weight_network.close() def form_undirected_matrix(self): height, width = self.import_weight_matrix.shape for i in range(height): for j in range(i, width): self.import_weight_matrix[i, j] += self.import_weight_matrix[j, i] self.import_weight_matrix[j, i] = 0 self.import_count_matrix[i, j] += self.import_count_matrix[j, i] self.import_count_matrix[j, i] = 0
class AISPointMore(object): def __init__(self): self.target_sp_area = StillPointArea() self.target_trajectory = Trajectory() self.source_sp_area = None self.source_trajectory = None def join_trajectory(self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, output_sp_header, output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, ): self.init_input_output(input_sp_file_name, input_trajectory_file_name, output_sp_file_name, output_sp_header, output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, ) self.deal_with_join_trajectory() self.finish_deal() def init_input_output(self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, output_sp_header, output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, ): # 打开输入输出 self.source_sp_area = AISReader(input_sp_file_name, sp_index, sp_offset, sp_mark, ) self.source_trajectory = AISReader(input_trajectory_file_name, trajectory_index, trajectory_offset, ) self.source_sp_area.start_fetch_data() self.source_trajectory.start_fetch_data() self.target_sp_area.init_output_saver(output_sp_file_name, output_sp_header, ) self.target_trajectory.init_output_saver(output_trajectory_file_name, output_trajectory_header, output_trajectory_txt_name, output_trajectory_point_name, output_trajectory_point_header) def deal_with_join_trajectory(self): print("deal the ship with mmsi: {}".format(self.source_sp_area.ais_point.mmsi)) while self.source_sp_area.has_next_data() and self.source_trajectory.has_next_data(): compare = self.source_trajectory.index - self.source_sp_area.index if compare < 0: print("!!!!!!!!!!!!!!!it would not happened in right situation") trajectory, _ = self.source_trajectory.fetch_data() elif compare > 0: self.deal_with_last_point() print("deal the ship with mmsi: {}".format(self.source_sp_area.ais_point.mmsi)) else: self.deal_the_same_ship() while self.source_sp_area.has_next_data(): self.deal_with_last_point() def deal_with_last_point(self): sp_area, is_suitable = self.source_sp_area.fetch_data() if is_suitable: self.target_trajectory.export_temp_trajectory_point(sp_area[0], self.target_sp_area.index - 1) self.target_sp_area.temp_still_point_set = sp_area self.target_sp_area.export_temp_still_point_set() self.target_sp_area.init_value() self.target_trajectory.init_value() def deal_the_same_ship(self): sp_area, is_suitable = self.source_sp_area.fetch_data() trajectory, _ = self.source_trajectory.fetch_data() self.target_trajectory.point_set = [sp_area[-1]] + trajectory self.target_sp_area.temp_still_point_set = sp_area if is_suitable: self.target_trajectory.export_temp_trajectory_point(sp_area[0], self.target_sp_area.index - 1) self.target_sp_area.export_temp_still_point_set() self.target_trajectory.is_ship_beginning = False else: self.target_trajectory.update_temp_trajectory_point(self.target_sp_area) def finish_deal(self): self.source_sp_area.close() self.source_trajectory.close()
class TrajectoryService(object): def __init__(self): self.source_sp_center = None self.source_rp_center = None self.target_directed_trajectory = Trajectory() self.target_undirected_trajectory = Trajectory() self.mark_dict = {} self.position_array = None self.line_matrix = None self.port_name_list = None def form_trajectory( self, input_sp_file_name, input_rp_file_name, directed_info_file, directed_line_file, undirected_info_file, undirected_line_file, info_header, ): self.init_input_output( input_sp_file_name, input_rp_file_name, directed_info_file, directed_line_file, undirected_info_file, undirected_line_file, info_header, ) self.form_trajectory_transaction() self.finish_deal() def init_input_output( self, input_sp_file_name, input_rp_file_name, directed_info_file, directed_line_file, undirected_info_file, undirected_line_file, info_header, ): # input self.source_sp_center = AISReader(input_sp_file_name, sp_index, sp_offset, label_index=sp_label_index) self.source_rp_center = AISReader(input_rp_file_name, rp_index, rp_offset, label_index=rp_label_index) self.source_sp_center.start_fetch_data() self.source_rp_center.start_fetch_data() self.target_directed_trajectory.init_output_saver( directed_info_file, info_header, directed_line_file, None, None, ) self.target_undirected_trajectory.init_output_saver( undirected_info_file, info_header, undirected_line_file, None, None, ) def form_trajectory_transaction(self): self.init_matrix() self.fill_matrix() self.export_matrix() def init_matrix(self): self.mark_dict = {} self.mark_dict, offset = self.source_sp_center.fetch_unique_mark( self.mark_dict, 0) self.mark_dict, _ = self.source_rp_center.fetch_unique_mark( self.mark_dict, offset) count = len(self.mark_dict.keys()) self.position_array = np.zeros((count, 3)) self.line_matrix = np.zeros((count, count)) self.get_port_name_list(count) def fill_matrix(self): while self.source_sp_center.has_next_data( ) and self.source_rp_center.has_next_data(): print("deal the trajectory with index: {}".format( self.source_sp_center.index)) compare = self.source_rp_center.index - self.source_sp_center.index if compare < 0: print( "!!!!!!!!!!!!!!!it would not happened in right situation") self.source_rp_center.fetch_data() elif compare > 0: self.connect_sp_point() else: self.connect_sp_rp_point() while self.source_sp_center.has_next_data(): print("deal the trajectory with index: {}".format( self.source_sp_center.index)) self.connect_sp_point() self.average_position_array() def connect_sp_point(self): port_list, _ = self.source_sp_center.fetch_data() self.update_matrix_by_point_list(port_list) def connect_sp_rp_point(self): port_list, _ = self.source_sp_center.fetch_data() route_list, _ = self.source_rp_center.fetch_data() self.update_matrix_by_point_list([port_list[0]] + route_list + [port_list[-1]]) def update_matrix_by_point_list(self, point_list): for i in range(len(point_list) - 1): self.update_line_matrix(point_list[i], point_list[i + 1]) self.update_position_array(point_list[i]) self.update_position_array(point_list[-1]) def update_line_matrix(self, start_info, end_info): start_label, end_label = start_info[1], end_info[1] start_index, end_index = self.mark_dict[start_label], self.mark_dict[ end_label] self.line_matrix[start_index, end_index] += 1 def update_position_array(self, row_info): ship_point, label = row_info index = self.mark_dict[label] self.position_array[index, 0] += ship_point.ship_position.X self.position_array[index, 1] += ship_point.ship_position.Y self.position_array[index, 2] += 1 def average_position_array(self): for i in range(len(self.position_array)): self.position_array[i][0] /= self.position_array[i][2] self.position_array[i][1] /= self.position_array[i][2] def export_matrix(self): self.export_directed_trajectory() self.export_undirected_matrix() def export_directed_trajectory(self): height, width = self.line_matrix.shape for i in range(height): for j in range(width): self.export_single_line(i, j, self.target_directed_trajectory) def finish_deal(self): self.source_rp_center.close() self.source_sp_center.close() def export_undirected_matrix(self): height, width = self.line_matrix.shape for i in range(height): for j in range(i, width): self.line_matrix[i, j] += self.line_matrix[j, i] self.line_matrix[j, i] = 0 self.export_single_line(i, j, self.target_undirected_trajectory) def export_single_line(self, i, j, target_trajectory): if i != j and self.line_matrix[i, j] > line_count_threshold: target_trajectory.line_file.write("{} 0\n".format( target_trajectory.index)) longitude, latitude, _ = self.position_array[i] target_trajectory.line_file.write( "0 {} {} 1.#QNAN 1.#QNAN\n".format(i, longitude, latitude)) longitude, latitude, _ = self.position_array[j] target_trajectory.line_file.write( "1 {} {} 1.#QNAN 1.#QNAN\n".format(i, longitude, latitude)) target_trajectory.info_saver.writerow([ target_trajectory.index, self.port_name_list[i], self.port_name_list[j], self.line_matrix[i, j] ]) target_trajectory.index += 1 def get_port_name_list(self, count): self.port_name_list = [0 for _ in range(count)] for key, value in self.mark_dict.items(): self.port_name_list[value] = key
class RoutePointService(object): def __init__(self): self.target_route_point = RoutePoint() self.ais_state = Const.MOVING self.before_point, self.middle_point, self.after_point = [ None, None, None ] self.source_sp_area = None self.source_trajectory = None def extract_route_point( self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, sp_header, output_rp_file_name, rp_header, port_service, ): self.init_input_output( input_sp_file_name, input_trajectory_file_name, output_sp_file_name, sp_header, output_rp_file_name, rp_header, ) self.deal_with_trajectory_construction(port_service) self.finish_deal() def init_input_output( self, input_sp_file_name, input_trajectory_file_name, output_sp_file_name, sp_header, output_rp_file_name, rp_header, ): # input self.source_sp_area = AISReader( input_sp_file_name, sp_index, sp_offset, ) self.source_trajectory = AISReader( input_trajectory_file_name, trajectory_index, trajectory_offset, ) self.source_sp_area.start_fetch_data() self.source_trajectory.start_fetch_data() self.target_route_point.init_output_saver(output_sp_file_name, sp_header, output_rp_file_name, rp_header) def deal_with_trajectory_construction(self, port_service): print("deal the ship with mmsi: {}".format( self.source_sp_area.ais_point.mmsi)) while self.source_sp_area.has_next_data( ) and self.source_trajectory.has_next_data(): compare = self.source_trajectory.index - self.source_sp_area.index if compare < 0: print( "!!!!!!!!!!!!!!!it would not happened in right situation") trajectory, _ = self.source_trajectory.fetch_data() elif compare > 0: self.deal_with_last_point(port_service) print("deal the ship with mmsi: {}".format( self.source_sp_area.ais_point.mmsi)) else: self.deal_with_same_ship(port_service) while self.source_sp_area.has_next_data(): self.deal_with_last_point(port_service) def deal_with_last_point(self, port_service): sp_area, _ = self.source_sp_area.fetch_data() if self.target_route_point.export_sp_center( sp_area, port_service, port_search_distance_threshold): self.fetch_route_point_and_export() self.target_route_point.temp_trajectory_set = [] self.target_route_point.temp_still_point_info = [] def deal_with_same_ship(self, port_service): sp_area, _ = self.source_sp_area.fetch_data() trajectory_set, _ = self.source_trajectory.fetch_data() if self.target_route_point.export_sp_center( sp_area, port_service, port_search_distance_threshold): self.fetch_route_point_and_export() self.target_route_point.temp_trajectory_set = trajectory_set else: self.target_route_point.temp_trajectory_set = [] # region fetch route point def fetch_route_point_and_export(self, ): trajectory_set = self.target_route_point.fetch_temp_trajectory_set() self.target_route_point.route_index = 0 self.target_route_point.route_point_set = [] for i in range(1, len(trajectory_set) - 1): self.before_point, self.middle_point, self.after_point = trajectory_set[ i - 1:i + 2] speed_change = self.get_speed_change() heading_change = self.get_heading_change() if self.enter_route_process(speed_change, heading_change): self.deal_with_situation_of_enter( speed_change, heading_change, ) elif self.out_route_process(speed_change): self.deal_with_situation_of_quit() else: self.deal_with_default_situation( speed_change, heading_change, ) def get_speed_change(self): after_speed = self.after_point.calculate_avg_speed_to_item( self.middle_point) middle_speed = self.middle_point.calculate_avg_speed_to_item( self.before_point) return after_speed - middle_speed def get_heading_change(self, ): after_heading = self.after_point.calculate_heading_to_item( self.middle_point) middle_point = self.before_point.calculate_heading_to_item( self.before_point) return math.fabs(after_heading - middle_point) def enter_route_process(self, speed_change, heading_change): return 0 - speed_change > rp_speed_threshold and heading_change > rp_heading_threshold def deal_with_situation_of_enter( self, speed_change, heading_change, ): if self.ais_state == Const.MOVING: self.deal_moving_to_route_situation( speed_change, heading_change, ) else: self.deal_route_situation( speed_change, heading_change, ) def out_route_process(self, speed_change): return speed_change > rp_speed_threshold def deal_with_situation_of_quit(self): if self.ais_state == Const.ROUTE: self.deal_route_to_moving_situation() else: self.deal_moving_situation() def deal_with_default_situation( self, speed_change, heading_change, ): if self.ais_state == Const.ROUTE: self.deal_route_situation( speed_change, heading_change, ) else: self.deal_moving_situation() def deal_moving_to_route_situation( self, speed_change, heading_change, ): self.target_route_point.route_point_set = [[ self.middle_point, speed_change / math.fabs(self.after_point.utc - self.middle_point.utc), heading_change ]] self.ais_state = Const.ROUTE def deal_route_situation( self, speed_change, heading_change, ): self.target_route_point.route_point_set.append([ self.middle_point, speed_change / math.fabs(self.after_point.utc - self.middle_point.utc), heading_change ]) def deal_route_to_moving_situation(self): self.target_route_point.export_route_center(rp_point_threshold, rp_acceleration_threshold, rp_avg_heading_threshold) self.ais_state = Const.MOVING def deal_moving_situation(self): pass # endregion def finish_deal(self): self.source_sp_area.close() self.source_trajectory.close() self.target_route_point.close()