docker处理转换坐标

This commit is contained in:
cs 2025-06-24 16:25:56 +08:00
parent 1685f8c951
commit 6fdc73dba9
8 changed files with 111 additions and 48 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -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

7
.idea/UAV.iml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>

View File

@ -0,0 +1,46 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="26">
<item index="0" class="java.lang.String" itemvalue="geopy" />
<item index="1" class="java.lang.String" itemvalue="scipy" />
<item index="2" class="java.lang.String" itemvalue="tornado" />
<item index="3" class="java.lang.String" itemvalue="h5py" />
<item index="4" class="java.lang.String" itemvalue="scikit_learn" />
<item index="5" class="java.lang.String" itemvalue="basemap" />
<item index="6" class="java.lang.String" itemvalue="minio" />
<item index="7" class="java.lang.String" itemvalue="torch" />
<item index="8" class="java.lang.String" itemvalue="numpy" />
<item index="9" class="java.lang.String" itemvalue="mpld3" />
<item index="10" class="java.lang.String" itemvalue="pyyaml" />
<item index="11" class="java.lang.String" itemvalue="basemap_data" />
<item index="12" class="java.lang.String" itemvalue="matplotlib" />
<item index="13" class="java.lang.String" itemvalue="basemap_data_hires" />
<item index="14" class="java.lang.String" itemvalue="openpyxl" />
<item index="15" class="java.lang.String" itemvalue="pandas" />
<item index="16" class="java.lang.String" itemvalue="clickhouse_driver" />
<item index="17" class="java.lang.String" itemvalue="pytz" />
<item index="18" class="java.lang.String" itemvalue="tqdm" />
<item index="19" class="java.lang.String" itemvalue="pyinstaller" />
<item index="20" class="java.lang.String" itemvalue="fiona" />
<item index="21" class="java.lang.String" itemvalue="edt" />
<item index="22" class="java.lang.String" itemvalue="opencv-python" />
<item index="23" class="java.lang.String" itemvalue="pyproj" />
<item index="24" class="java.lang.String" itemvalue="psutil" />
<item index="25" class="java.lang.String" itemvalue="opencv_python" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="matrix_factorization.svd.*" />
</list>
</option>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="uav (2)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="uav (2)" project-jdk-type="Python SDK" />
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,6 +1,8 @@
import logging import logging
import os import os
from typing import Dict from typing import Dict
import docker
import rasterio import rasterio
from rasterio.mask import mask from rasterio.mask import mask
from rasterio.transform import Affine, rowcol from rasterio.transform import Affine, rowcol
@ -8,14 +10,12 @@ import fiona
from edt import edt from edt import edt
import numpy as np import numpy as np
import math import math
from post_pro.trans_orthophoto import TransOrthophoto
class MergeTif: class MergeTif:
def __init__(self, output_dir: str): def __init__(self, output_dir: str):
self.output_dir = output_dir self.output_dir = output_dir
self.logger = logging.getLogger('UAV_Preprocess.MergeTif') self.logger = logging.getLogger('UAV_Preprocess.MergeTif')
self.trans_orthophoto = TransOrthophoto()
def merge_orthophoto(self, grid_lt): def merge_orthophoto(self, grid_lt):
"""合并网格的正射影像""" """合并网格的正射影像"""
@ -55,13 +55,40 @@ class MergeTif:
self.output_dir, "orthophoto.tif"), orthophoto_vars) self.output_dir, "orthophoto.tif"), orthophoto_vars)
self.logger.info("所有产品合并完成") self.logger.info("所有产品合并完成")
self.trans_orthophoto.trans_to_epsg4326(os.path.join(self.output_dir, "orthophoto.tif"), os.path.join( # self.trans_orthophoto.trans_to_epsg4326(os.path.join(self.output_dir, "orthophoto.tif"), os.path.join(
self.output_dir, "orthophoto_epsg4326.tif")) # self.output_dir, "orthophoto_epsg4326.tif"))
self.process_trans2photo4326()
except Exception as e: except Exception as e:
self.logger.error(f"产品合并过程中发生错误: {str(e)}", exc_info=True) self.logger.error(f"产品合并过程中发生错误: {str(e)}", exc_info=True)
raise 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): 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): if not os.path.exists(input_raster):
print("Cannot mask raster, %s does not exist" % input_raster) print("Cannot mask raster, %s does not exist" % input_raster)

View File

@ -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")