feat(utils): 重构传统GeoTIFF转COG格式的工具函数.
This commit is contained in:
parent
32d629aede
commit
539b6a387b
@ -7,8 +7,8 @@ COG (Cloud Optimized GeoTIFF) 是一种优化设计的 GeoTIFF 文件格式, 特
|
|||||||
的存储和访问. 自带分块存储功能与影像金字塔, 能够在请求时按需加载, 实现了高效的压缩和传输.
|
的存储和访问. 自带分块存储功能与影像金字塔, 能够在请求时按需加载, 实现了高效的压缩和传输.
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Authors: Hong Xie
|
Authors: CVEO Team
|
||||||
Last Updated: 2025-10-20
|
Last Updated: 2026-01-08
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -31,10 +31,10 @@ gdal.SetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS")
|
|||||||
gdal.SetConfigOption("GDAL_CACHEMAX", "1024")
|
gdal.SetConfigOption("GDAL_CACHEMAX", "1024")
|
||||||
|
|
||||||
|
|
||||||
def tif2cog(
|
def tif_to_cog(
|
||||||
input_path: str,
|
input_path: str,
|
||||||
output_path: str,
|
output_path: str,
|
||||||
output_type: gdal.GDT_Float32 = gdal.GDT_Float32,
|
output_type: gdal.GDT_DataType = gdal.GDT_Float32,
|
||||||
no_data: float = np.nan,
|
no_data: float = np.nan,
|
||||||
compress: str = "DEFLATE",
|
compress: str = "DEFLATE",
|
||||||
scaleParams: list = None,
|
scaleParams: list = None,
|
||||||
@ -48,7 +48,7 @@ def tif2cog(
|
|||||||
输入 GeoTIFF 文件路径
|
输入 GeoTIFF 文件路径
|
||||||
output_path : str
|
output_path : str
|
||||||
输出 COG 文件路径
|
输出 COG 文件路径
|
||||||
output_type : gdal.GDT_Float32, optional
|
output_type : gdal.GDT_DataType, optional
|
||||||
输出数据类型, 默认 float32, by default gdal.GDT_Float32
|
输出数据类型, 默认 float32, by default gdal.GDT_Float32
|
||||||
no_data : float, optional
|
no_data : float, optional
|
||||||
无效值, 默认 np.nan, by default np.nan
|
无效值, 默认 np.nan, by default np.nan
|
||||||
@ -60,6 +60,23 @@ def tif2cog(
|
|||||||
if ds is None:
|
if ds is None:
|
||||||
logging.error(f"无法打开输入文件: {input_path}")
|
logging.error(f"无法打开输入文件: {input_path}")
|
||||||
return
|
return
|
||||||
|
# 查看原数据是否已设置无效值
|
||||||
|
band = ds.GetRasterBand(1)
|
||||||
|
original_no_data = band.GetNoDataValue()
|
||||||
|
original_data_type = band.DataType
|
||||||
|
if np.isnan(no_data):
|
||||||
|
if original_no_data is None:
|
||||||
|
# 根据输出数据类型设置无效值
|
||||||
|
if output_type == gdal.GDT_Int16:
|
||||||
|
no_data = -32768
|
||||||
|
elif output_type == gdal.GDT_Float32:
|
||||||
|
no_data = -9999
|
||||||
|
else:
|
||||||
|
no_data = np.nan
|
||||||
|
logging.warning(f"原数据未设无效值, 现已设为: {no_data}")
|
||||||
|
else:
|
||||||
|
no_data = original_no_data
|
||||||
|
logging.info(f"原数据无效值为: {no_data}")
|
||||||
translate_options = gdal.TranslateOptions(
|
translate_options = gdal.TranslateOptions(
|
||||||
format="COG", # 输出为 COG 格式, 自动构建金字塔
|
format="COG", # 输出为 COG 格式, 自动构建金字塔
|
||||||
outputType=output_type,
|
outputType=output_type,
|
||||||
@ -92,7 +109,7 @@ def main(input_dir, file_name, output_path):
|
|||||||
output_path = output_dir / file_name
|
output_path = output_dir / file_name
|
||||||
# 关于无效值
|
# 关于无效值
|
||||||
# Float32 类型可以设为 -9999; 8bit 类型可以设为 0
|
# Float32 类型可以设为 -9999; 8bit 类型可以设为 0
|
||||||
tif2cog(str(input_path), str(output_path), no_data=-9999)
|
tif_to_cog(str(input_path), str(output_path), no_data=-9999)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
Loading…
x
Reference in New Issue
Block a user