添加正射影像坐标转换

This commit is contained in:
weixin_46229132 2025-05-07 15:34:49 +08:00
parent 5549b15244
commit 4dabfa0545
2 changed files with 46 additions and 1 deletions

View File

@ -8,12 +8,14 @@ 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):
"""合并网格的正射影像"""
@ -51,8 +53,11 @@ class MergeTif:
}
self.merge(all_orthos_and_ortho_cuts, os.path.join(
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"))
except Exception as e:
self.logger.error(f"产品合并过程中发生错误: {str(e)}", exc_info=True)
raise

View File

@ -0,0 +1,40 @@
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"文件已成功重投影")