UAV/trans_orthophoto.py
2025-06-12 17:09:43 +08:00

53 lines
1.9 KiB
Python

import logging
import argparse
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling
class TransOrthophoto:
def __init__(self):
self.logger = logging.getLogger('UAV_Preprocess.TransOrthophoto')
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
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"文件已成功重投影: {output_path}")
parser = argparse.ArgumentParser(description="正射影像投影转换为EPSG:4326")
parser.add_argument('--input_img', required=True, help='输入影像路径')
parser.add_argument('--output_img', required=True, help='输出影像路径')
args = parser.parse_args()
trans = TransOrthophoto()
trans.trans_to_epsg4326(args.input_img, args.output_img)