diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/UAV.iml b/.idea/UAV.iml new file mode 100644 index 0000000..ec63674 --- /dev/null +++ b/.idea/UAV.iml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..b4f9ed1 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,46 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1ec743e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/post_pro/merge_tif.py b/post_pro/merge_tif.py index 9ed3a8f..daa2b20 100644 --- a/post_pro/merge_tif.py +++ b/post_pro/merge_tif.py @@ -1,6 +1,8 @@ import logging import os from typing import Dict + +import docker import rasterio from rasterio.mask import mask from rasterio.transform import Affine, rowcol @@ -8,14 +10,12 @@ import fiona from edt import edt import numpy as np import math -from post_pro.trans_orthophoto import TransOrthophoto class MergeTif: def __init__(self, output_dir: str): self.output_dir = output_dir self.logger = logging.getLogger('UAV_Preprocess.MergeTif') - self.trans_orthophoto = TransOrthophoto() def merge_orthophoto(self, grid_lt): """合并网格的正射影像""" @@ -55,13 +55,40 @@ class MergeTif: self.output_dir, "orthophoto.tif"), orthophoto_vars) self.logger.info("所有产品合并完成") - self.trans_orthophoto.trans_to_epsg4326(os.path.join(self.output_dir, "orthophoto.tif"), os.path.join( - self.output_dir, "orthophoto_epsg4326.tif")) + # self.trans_orthophoto.trans_to_epsg4326(os.path.join(self.output_dir, "orthophoto.tif"), os.path.join( + # self.output_dir, "orthophoto_epsg4326.tif")) + + self.process_trans2photo4326() except Exception as e: self.logger.error(f"产品合并过程中发生错误: {str(e)}", exc_info=True) raise + def process_trans2photo4326(self): + success = True + try: + client = docker.from_env() + container = client.containers.create( + image='trans_docker', + volumes={ + f"{self.output_dir}": {'bind': '/temp', 'mode': 'rw'} + }, + ) + container.start() + container.wait() + logs = container.logs(tail=50).decode('utf-8') + print(logs) + except Exception as e: + print(f"转换失败, 原因: {e}") + success = False + finally: + try: + container.remove(force=True) + except Exception: + pass # 避免因remove失败导致程序崩溃 + if success: + print("正射影像转换完成") + def compute_mask_raster(self, input_raster, vector_mask, output_raster, blend_distance=20, only_max_coords_feature=False): if not os.path.exists(input_raster): print("Cannot mask raster, %s does not exist" % input_raster) diff --git a/post_pro/trans_orthophoto.py b/post_pro/trans_orthophoto.py deleted file mode 100644 index 7d4297b..0000000 --- a/post_pro/trans_orthophoto.py +++ /dev/null @@ -1,44 +0,0 @@ -import logging -import rasterio -from rasterio.warp import calculate_default_transform, reproject, Resampling - - -class TransOrthophoto: - def __init__(self): - self.logger = logging.getLogger('UAV_Preprocess.TransOrthophoto') - - def trans_to_epsg4326(self, ori_orthophoto_path: str, output_path: str): - # 打开原始文件 - with rasterio.open(ori_orthophoto_path) as src: - # 定义目标 CRS - dst_crs = 'EPSG:4326' - - # 计算目标变换和元数据 - transform, width, height = calculate_default_transform( - src.crs, dst_crs, src.width, src.height, *src.bounds) - kwargs = src.meta.copy() - kwargs.update({ - 'crs': dst_crs, - 'transform': transform, - 'width': width, - 'height': height - }) - - # 创建重投影后的文件 - with rasterio.open(output_path, 'w', **kwargs) as dst: - for i in range(1, src.count + 1): - reproject( - source=rasterio.band(src, i), - destination=rasterio.band(dst, i), - src_transform=src.transform, - src_crs=src.crs, - dst_transform=transform, - dst_crs=dst_crs, - resampling=Resampling.nearest - ) - - self.logger.info(f"文件已成功重投影") - -if __name__ == "__main__": - trans = TransOrthophoto() - trans.trans_to_epsg4326(r"G:\ODM_output\test\orthophoto.tif", r"G:\ODM_output\test\orthophoto_epsg4326.tif") \ No newline at end of file