UAV/post_pro/trans_orthophoto.py
2025-05-07 15:34:49 +08:00

41 lines
1.4 KiB
Python

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"文件已成功重投影")