def get_single_year_oil_tanker(source_path, source_table, target_db, ais_table, ais_rol_list, create_table=True, is_need_cleaning=False, clean_speed_threshold=0, clean_draft_threshold=0): Utils.check_file_path(target_db) ais = AISService(target_db) # 数据导入 ais.import_data_from_path( source_path, source_table, ais_table, ais_rol_list, create_table=create_table, filter_query="WHERE Vessel_type_sub='Crude Oil Tanker'") # 数据清理 if is_need_cleaning: ais.clean_dirty_data(ais_table, clean_speed_threshold, clean_draft_threshold)
def dirty_trajectory_clean( self, clean_file_name, time_threshold, distance_threshold, ): Utils.check_file_path(clean_file_name) clean_file = open(clean_file_name, 'wb') file_writer = csv.writer(clean_file) file_writer.writerow(next(self.trajectory_reader)) # write head trajectory_first = self.trajectory_reader.next() trajectory_list = [trajectory_first] for after_point in self.trajectory_reader: if not self.judge_same_line(trajectory_first, after_point): if not self.judge_dirty(trajectory_list, time_threshold, distance_threshold): self.export_clean_trajecotry(trajectory_list, file_writer) trajectory_first = after_point trajectory_list = [trajectory_first] trajectory_list.append(after_point) if not self.judge_dirty(trajectory_list, time_threshold, distance_threshold): self.export_clean_trajecotry(trajectory_list, file_writer) clean_file.close()
def get_draft_count_and_load_identify(source_db, ais_table, target_db, draft_table, draft_rol_list, draft_state_table, draft_state_rol_list): Utils.check_file_path(target_db) draft = DraftDB(target_db) draft.import_data(source_db, ais_table, draft_table, draft_rol_list) draft.ships_draft_state_identify(draft_table, draft_state_table, draft_state_rol_list) return
def extract_country_relation_trajectory(port_shp_file, source_trajectory_file, country_trajectory_file, trajectory_header, country_name): Utils.check_file_path(country_trajectory_file) # 1. 获取港口 port_service = PortService(port_shp_file) Utils.extract_country_trajectories(port_service, source_trajectory_file, country_trajectory_file, trajectory_header, country_name)
def get_trajectory_info(source_csv_file, static_info_file_header, static_info_file_for_analysis, deadweight_db, deadweight_table, port_shp_file): # ['line_index', 'mmsi', 'mark', 'imo', 'vessel_name', 'vessel_type', 'length', 'width', 'deadweight', # 'start_time', 'arrive_time', 'source_port', 'target_port', 'load_state', 'input_or_output'] Utils.check_file_path(static_info_file_for_analysis) # 1.acquire deadweight info and port info ships_deadweight = DeadweightDB(deadweight_db) ships_deadweight.init_ships_deadweight(deadweight_table) port_service = PortService(port_shp_file) static_info_file = open(static_info_file_for_analysis, 'wb') static_info_file_writer = csv.writer(static_info_file) static_info_file_writer.writerow(static_info_file_header) with open(source_csv_file) as source_file: source_reader = csv.reader(source_file) next(source_reader) before_line = source_reader.next() print(before_line) trajectory_info = start_init(before_line, ships_deadweight, port_service) for line in source_reader: if line[0] == before_line[0]: # print("!!!!!!!!!!!!!!1") pass elif line[1] == before_line[1]: # print(line) trajectory_info = same_ship_deal(before_line, line, trajectory_info, port_service, static_info_file_writer, ships_deadweight, True) else: print(line) trajectory_info = same_ship_deal(before_line, line, trajectory_info, port_service, static_info_file_writer, ships_deadweight, True) before_line = line final_deal(before_line, trajectory_info, port_service, static_info_file_writer)
def get_for_analysis( deadweight_db, deadweight_table, source_csv_file, static_info_file_for_analysis, ): Utils.check_file_path(static_info_file_for_analysis) # get trajectory _info deadweights = DeadweightDB(deadweight_db) deadweights.init_ships_deadweight(deadweight_table) Utils.get_trajectory_static_info_file(source_csv_file, deadweights, static_info_file_for_analysis, static_info_file_header, Const.LINE_NO_SPLIT_DEGREE) return
def split_ship_trajectory(target_db, draft_db, draft_state_table, ais_table, port_name, trajectory_output_file, trajectory_output_header, search_distance, trajectory_distance_threshold, trajectory_speed_threshold, outliers_output_file): Utils.check_file_path(trajectory_output_file) Utils.check_file_path(outliers_output_file) # 1. 读取数据库 draft = DraftDB(draft_db) ais = AISService(target_db) port_service = PortService(port_name) trajectory_file = open(trajectory_output_file, 'wb') trajectory_writer = csv.writer(trajectory_file) trajectory_writer.writerow(trajectory_output_header) outliers_file = open(outliers_output_file, 'wb') outliers_writer = csv.writer(outliers_file) outliers_writer.writerow(['mmsi', 'mark', 'count']) # 2. 开始读取 draft_state_dict = draft.start_fetch_transaction(draft_state_table) ais.start_fetch_data_transaction(ais_table) line_index = 0 while draft.has_next_draft_state() and ais.has_next_ais_ship(): compare_result = Utils.compare_mmsi(ais.ais_point, draft_state_dict) mmsi = ais.ais_point.mmsi mark = ais.ais_point.mark if compare_result < 0: ais.skip_useless_trajectory() elif compare_result > 0: draft_state_dict = draft.fetch_draft_state() else: line_index, outliers_count = ais.form_trajectory( draft_state_dict, trajectory_writer, line_index, port_service, search_distance, trajectory_distance_threshold, trajectory_speed_threshold) outliers_writer.writerow([mmsi, mark, outliers_count]) # 3. 关闭 trajectory_file.close() draft.close() ais.close()
def extract_country_crude_oil(self, port_shp_name, deadweight_db, deadweight_table, import_file_name, export_file_name, file_header, need_full_load): Utils.check_file_path(import_file_name) Utils.check_file_path(export_file_name) import_file = open(import_file_name, "wb") import_writer = csv.writer(import_file) import_writer.writerow(file_header) export_file = open(export_file_name, "wb") export_writer = csv.writer(export_file) export_writer.writerow(file_header) port_service = PortService(port_shp_name) # get trajectory _info deadweights = DeadweightDB(deadweight_db) deadweights.init_ships_deadweight(deadweight_table) # skip header next(self.trajectory_reader) before = self.trajectory_reader.next() source_port_name = before[TrajectoryStep.SOURCE_PORT_INDEX] self.export_country_info(before, source_port_name, port_service, deadweights, export_writer, need_full_load) for after in self.trajectory_reader: if not self.judge_same_line(before, after): target_port_name = before[TrajectoryStep.TARGET_PORT_INDEX] self.export_country_info(before, target_port_name, port_service, deadweights, import_writer, need_full_load) source_port_name = after[TrajectoryStep.SOURCE_PORT_INDEX] self.export_country_info(after, source_port_name, port_service, deadweights, export_writer, need_full_load) before = after target_port_name = before[TrajectoryStep.TARGET_PORT_INDEX] self.export_country_info(before, target_port_name, port_service, deadweights, import_writer, need_full_load)
def shared_mmsi_identify(source_db, source_table, speed_threshold, distance_threshold, point_percent, output_ais_csv, output_header): """ identify the situation of the shared MMSI, consider the speed, distance threshold, and delete the the ship which point is less than the point_threshold :param source_db: the database file of the original data :param source_table: the table name of the original data in the database file :param speed_threshold: the threshold of speed to identify whether the same ship :param distance_threshold: the threshold of distance to identify whether the same ship :param point_percent: delete the ship with the point less than the percent threshold :param output_ais_csv: the .csv file of the output data :param output_header: the header in the .csv file :return: None """ Utils.check_file_path(output_ais_csv) ais = AISService(source_db) result_file = open(output_ais_csv, 'wb') csv_writer = csv.writer(result_file) csv_writer.writerow(output_header) ais.start_fetch_original_data_transaction(source_table) while ais.has_next_ais_ship(): print(ais.ais_point.mmsi) ais.same_mmsi_identify( csv_writer, speed_threshold, distance_threshold, point_percent, ) result_file.close() ais.close() return