1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
''' 将相同分辨率的单波段影像进行融合
目录结构 / ---20220824/ ---R10m/T50RLT_20220906T025541_B02_10m.tif、T50RMS_20220906T025541_B02_10m.tif ---R20m/ ---R60m/
同一文件夹内,文件的前6个字符相同,进行波段组合 '''
import os from osgeo import gdal import numpy as np
def band_classify(band_names): ''' 按照区域进行分类 :param band_name: :return: ''' classified_strings = {} for string in band_names: key = string[:6] if key in classified_strings: classified_strings[key].append(string) else: classified_strings[key] = [string] return classified_strings
def bandcombination(input_path,out_path): for date in os.listdir(input_path): print('日期',date) r10m_dir = input_path + '/' + date + '/' + 'R10m' r20m_dir = input_path + '/' + date + '/' + 'R20m' r60m_dir = input_path + '/' + date + '/' + 'R60m'
r10m_bands = os.listdir(r10m_dir) r20m_bands = os.listdir(r20m_dir) r60m_bands = os.listdir(r60m_dir) r10m_bands = [x for x in r10m_bands if os.path.splitext(x)[1] == '.tif'] r20m_bands = [x for x in r20m_bands if os.path.splitext(x)[1] == '.tif'] r60m_bands = [x for x in r60m_bands if os.path.splitext(x)[1] == '.tif']
r10m_bands = band_classify(r10m_bands) r20m_bands = band_classify(r20m_bands) r60m_bands = band_classify(r60m_bands)
os.makedirs(out_path + '/' + date + '/R10m') os.makedirs(out_path + '/' + date + '/R20m') os.makedirs(out_path + '/' + date + '/R60m')
for key,bands in r10m_bands.items(): print('10m---区域代码',key) print('10m---组合波段',bands) out_bandpath = out_path+'/'+date+'/R10m/'+key+'.tif' ds = gdal.Open(r10m_dir+'/'+bands[0]) ds_width = ds.RasterXSize ds_height = ds.RasterYSize ds_geo = ds.GetGeoTransform() ds_prj = ds.GetProjection() driver = gdal.GetDriverByName('GTiff') ds_result = driver.Create(out_bandpath, ds_width, ds_height, bands=len(bands), eType=gdal.GDT_UInt16) ds_result.SetGeoTransform(ds_geo) ds_result.SetProjection(ds_prj) del ds i = 1 for band in bands: band_img = gdal.Open(r10m_dir+'/'+band) band_data = band_img.GetRasterBand(1).ReadAsArray(0, 0, ds_width, ds_height) ds_result.GetRasterBand(i).WriteArray(band_data) i = i+1 del ds_result
for key,bands in r20m_bands.items(): print('20m---区域代码',key) print('20m---组合波段',bands) out_bandpath = out_path+'/'+date+'/R20m/'+key+'.tif' ds = gdal.Open(r20m_dir+'/'+bands[0]) ds_width = ds.RasterXSize ds_height = ds.RasterYSize ds_geo = ds.GetGeoTransform() ds_prj = ds.GetProjection() driver = gdal.GetDriverByName('GTiff') ds_result = driver.Create(out_bandpath, ds_width, ds_height, bands=len(bands), eType=gdal.GDT_UInt16) ds_result.SetGeoTransform(ds_geo) ds_result.SetProjection(ds_prj) del ds i = 1 for band in bands: band_img = gdal.Open(r20m_dir+'/'+band) band_data = band_img.GetRasterBand(1).ReadAsArray(0, 0, ds_width, ds_height) ds_result.GetRasterBand(i).WriteArray(band_data) i = i+1 del ds_result
for key,bands in r60m_bands.items(): print('60m---区域代码',key) print('60m---组合波段',bands) out_bandpath = out_path+'/'+date+'/R60m/'+key+'.tif' ds = gdal.Open(r60m_dir+'/'+bands[0]) ds_width = ds.RasterXSize ds_height = ds.RasterYSize ds_geo = ds.GetGeoTransform() ds_prj = ds.GetProjection() driver = gdal.GetDriverByName('GTiff') ds_result = driver.Create(out_bandpath, ds_width, ds_height, bands=len(bands), eType=gdal.GDT_UInt16) ds_result.SetGeoTransform(ds_geo) ds_result.SetProjection(ds_prj) del ds i = 1 for band in bands: band_img = gdal.Open(r60m_dir+'/'+band) band_data = band_img.GetRasterBand(1).ReadAsArray(0, 0, ds_width, ds_height) ds_result.GetRasterBand(i).WriteArray(band_data) i = i+1 del ds_result
if __name__ == '__main__': input_path = 'F:/PoyangLake/poyanghu_data/test/tif' out_path = 'F:/PoyangLake/poyanghu_data/test/bandcombination' bandcombination(input_path, out_path)
|