|  | @@ -0,0 +1,88 @@
 | 
	
		
			
				|  |  | +# -*- coding: utf-8 -*-
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +__author__ = 'wanger'
 | 
	
		
			
				|  |  | +__description__ = '批量生成栅格数据元数据表格 EXCEL'
 | 
	
		
			
				|  |  | +__date__ = '2024-11-25'
 | 
	
		
			
				|  |  | +__copyright__ = '(C) 2024 by siwei'
 | 
	
		
			
				|  |  | +__revision__ = '1.0'
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from osgeo import gdal, osr
 | 
	
		
			
				|  |  | +import openpyxl
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 读取 .img 文件
 | 
	
		
			
				|  |  | +filename = 'E:\\projects\\遥感技术部需求\\批量生成元数据\\数据\\分幅成果\\DEM成果\\DEM\\NH48G051053DEMK.img'
 | 
	
		
			
				|  |  | +dataset = gdal.Open(filename)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +if dataset:
 | 
	
		
			
				|  |  | +    # 获取栅格数据的基本信息
 | 
	
		
			
				|  |  | +    print(f"Driver: {dataset.GetDriver().ShortName}")
 | 
	
		
			
				|  |  | +    print(f"Raster Size: {dataset.RasterXSize} x {dataset.RasterYSize}")
 | 
	
		
			
				|  |  | +    print(f"Number of Bands: {dataset.RasterCount}")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 获取地理信息
 | 
	
		
			
				|  |  | +    geo_transform = dataset.GetGeoTransform()
 | 
	
		
			
				|  |  | +    if geo_transform:
 | 
	
		
			
				|  |  | +        print(f"GeoTransform: {geo_transform}")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 获取投影信息
 | 
	
		
			
				|  |  | +    projection = dataset.GetProjection()
 | 
	
		
			
				|  |  | +    print(f"Projection: {projection}")
 | 
	
		
			
				|  |  | +    # 获取栅格数据的投影信息
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 创建 SpatialReference 对象并加载投影信息
 | 
	
		
			
				|  |  | +    spatial_ref = osr.SpatialReference()
 | 
	
		
			
				|  |  | +    spatial_ref.ImportFromWkt(projection)  # 或者使用 ImportFromProj4(projection)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 检查投影是否为 UTM 类型,UTM 投影的中央子午线位于 PROJ.4 中的 +lon_0 参数
 | 
	
		
			
				|  |  | +    if spatial_ref.IsProjected():
 | 
	
		
			
				|  |  | +        proj4 = spatial_ref.ExportToProj4()
 | 
	
		
			
				|  |  | +        print(f"Projection PROJ.4: {proj4}")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # 寻找中央子午线 (lon_0)
 | 
	
		
			
				|  |  | +        central_meridian = None
 | 
	
		
			
				|  |  | +        for param in proj4.split():
 | 
	
		
			
				|  |  | +            if param.startswith('+lon_0'):
 | 
	
		
			
				|  |  | +                central_meridian = param.split('=')[1]
 | 
	
		
			
				|  |  | +                break
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if central_meridian:
 | 
	
		
			
				|  |  | +            print(f"Central Meridian: {central_meridian}")
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            print("No central meridian found.")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 获取栅格数据的各个波段元数据
 | 
	
		
			
				|  |  | +    for band_index in range(1, dataset.RasterCount + 1):
 | 
	
		
			
				|  |  | +        band = dataset.GetRasterBand(band_index)
 | 
	
		
			
				|  |  | +        print(f"\nBand {band_index}:")
 | 
	
		
			
				|  |  | +        print(f"Data type: {gdal.GetDataTypeName(band.DataType)}")
 | 
	
		
			
				|  |  | +        print(f"Band min: {band.GetMinimum()}, Band max: {band.GetMaximum()}")
 | 
	
		
			
				|  |  | +        print(f"Band NoData Value: {band.GetNoDataValue()}")
 | 
	
		
			
				|  |  | +        print(f"Band metadata: {band.GetMetadata()}")
 | 
	
		
			
				|  |  | +else:
 | 
	
		
			
				|  |  | +    print("Failed to load image.")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#操作excel
 | 
	
		
			
				|  |  | +# filename = 'example.xlsx'
 | 
	
		
			
				|  |  | +# workbook = openpyxl.load_workbook(filename)
 | 
	
		
			
				|  |  | +# sheet = workbook.active
 | 
	
		
			
				|  |  | +# # 定义替换数据的字典
 | 
	
		
			
				|  |  | +# replacements = {
 | 
	
		
			
				|  |  | +#     "{{name}}": "Bob",
 | 
	
		
			
				|  |  | +#     "{{age}}": "40",
 | 
	
		
			
				|  |  | +#     "{{address}}": "Paris"
 | 
	
		
			
				|  |  | +# }
 | 
	
		
			
				|  |  | +# # 遍历单元格,替换占位符
 | 
	
		
			
				|  |  | +# for row in sheet.iter_rows():
 | 
	
		
			
				|  |  | +#     for cell in row:
 | 
	
		
			
				|  |  | +#         if isinstance(cell.value, str):  # 确保单元格内容是字符串
 | 
	
		
			
				|  |  | +#             for placeholder, replacement in replacements.items():
 | 
	
		
			
				|  |  | +#                 if placeholder in cell.value:
 | 
	
		
			
				|  |  | +#                     # 替换占位符
 | 
	
		
			
				|  |  | +#                     cell.value = cell.value.replace(placeholder, replacement)
 | 
	
		
			
				|  |  | +# # 保存修改后的文件
 | 
	
		
			
				|  |  | +# workbook.save('modified_example.xlsx')
 |