def create_shp_with_info(port_shp_file, deadweight_db, deadweight_table, source_csv_file, target_path, split_csv_file, format_txt_file, static_info_file_for_shp, shp_file, split_trajectory_degree): split_count = split_source_csv_file(source_csv_file, target_path, split_csv_file) port_service = PortService(port_shp_file) deadweights = DeadweightDB(deadweight_db) deadweights.init_ships_deadweight(deadweight_table) for i in range(split_count): os.chdir(os.path.join(target_path, str(i))) Utils.convert_csv_to_format_txt(split_csv_file, format_txt_file, port_service, split_trajectory_degree) Utils.get_trajectory_static_info_file(split_csv_file, deadweights, static_info_file_for_shp, static_info_file_header, split_trajectory_degree) Utils.create_shp(format_txt_file, shp_file) return
def step3_format_line(): # 1. 读取数据库 draft = DraftDB(Config.target_db) ais = AISService(Config.target_db) trajectory_file = open(Config.trajectory_output_file, 'wb') trajectory_writer = csv.writer(trajectory_file) trajectory_writer.writerow(Config.trajectory_output_header) port_service = PortService(Config.port_name) # port_service = '' # 2. 开始读取 draft_state_dict = draft.start_fetch_transaction(Config.draft_state_table) ais.start_fetch_data_transaction(Config.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) if compare_result < 0: ais.skip_useless_trajectory() elif compare_result > 0: draft_state_dict = draft.fetch_draft_state() else: line_index = ais.form_trajectory( draft_state_dict, trajectory_writer, line_index, port_service, Config.search_distance, Config.trajectory_distance_threshold, Config.trajectory_speed_threshold) # 3. 关闭 trajectory_file.close() draft.close() ais.close()
def step4_extract_china_line(): # 1. 获取港口 port_service = PortService(Config.port_name) Utils.extract_country_trajectories( port_service, Config.trajectory_output_file, Config.china_trajectory_output_file, Config.trajectory_output_header + ['inputOrOutput'], Const.CHINA_NAME)
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 check_file_by_creating_trajectory(): mmsi = 215153000 filename = r"D:\graduation\data\step_result\check\step4\trajectory_{}.csv".format( mmsi) target_file_name = r"D:\graduation\data\step_result\check\step4\trajectory_{}.txt".format( mmsi) port_name = r"D:\GeoData\Port\WPI.shp" degree_threshold = Const.LINE_NO_SPLIT_DEGREE port_service = PortService(port_name) Utils.convert_csv_to_format_txt(filename, target_file_name, port_service, degree_threshold) shp_file_name = r"D:\graduation\data\step_result\check\step4\trajectory_{}.shp".format( mmsi) Utils.create_shp(target_file_name, shp_file_name)
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 step5_convert_csv_to_format_txt(file_name, degree_threshold): port_service = PortService(Config.port_name) Utils.convert_csv_to_format_txt(file_name, file_name.replace('.csv', '.txt'), port_service, degree_threshold)