|
|
import os
|
|
|
import numpy as np
|
|
|
from scipy import ndimage
|
|
|
|
|
|
from osgeo import gdal, ogr, osr
|
|
|
|
|
|
|
|
|
|
|
|
dem_path = 'E:/bang/KUNKUN/data/water_depth/Dem_True/Dem11.tif'
|
|
|
area_dir = 'E:/bang/KUNKUN/data/water_depth/Area'
|
|
|
output_dir = 'E:/bang/KUNKUN/data/water_depth/Results/'
|
|
|
|
|
|
|
|
|
dem_ds = gdal.Open(dem_path)
|
|
|
|
|
|
|
|
|
dem_array = dem_ds.GetRasterBand(1).ReadAsArray()
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
|
for filename in os.listdir(area_dir):
|
|
|
if filename.startswith('S1B_') and filename.endswith('.tif'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parts = filename.split('_')
|
|
|
year = parts[1]
|
|
|
month = parts[2]
|
|
|
day = parts[3].split('.')[0]
|
|
|
date = f"{year}-{month}-{day}"
|
|
|
print(f"日期:{date}")
|
|
|
|
|
|
area_ds = gdal.Open(os.path.join(area_dir, filename))
|
|
|
area_array = area_ds.GetRasterBand(1).ReadAsArray()
|
|
|
|
|
|
labeled_array, num_features = ndimage.label(area_array)
|
|
|
print(f"湖泊数量:{num_features}")
|
|
|
|
|
|
min_area_pixels = int(1000 * 1000 / 88.36)
|
|
|
removed_count = 0
|
|
|
for i in range(1, num_features + 1):
|
|
|
size = np.sum(labeled_array == i)
|
|
|
if size < min_area_pixels:
|
|
|
labeled_array[labeled_array == i] = 0
|
|
|
removed_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
remaining_labels = np.unique(labeled_array)
|
|
|
remaining_count = len(remaining_labels) - 1
|
|
|
print(f"去除小湖数量:{removed_count}")
|
|
|
print(f"剩余湖泊数量:{remaining_count}")
|
|
|
|
|
|
unique_labels = np.unique(labeled_array)
|
|
|
unique_labels = unique_labels[unique_labels != 0]
|
|
|
relabeled_array = np.zeros_like(labeled_array)
|
|
|
new_label = 1
|
|
|
for old_label in unique_labels:
|
|
|
size = np.sum(labeled_array == old_label)
|
|
|
if size >= min_area_pixels:
|
|
|
relabeled_array[labeled_array == old_label] = new_label
|
|
|
new_label += 1
|
|
|
print(f"重新编号:{np.unique(relabeled_array[relabeled_array != 0])}")
|
|
|
|
|
|
|
|
|
for label in range(1, new_label):
|
|
|
mask = relabeled_array == label
|
|
|
if np.sum(mask) == 0:
|
|
|
continue
|
|
|
|
|
|
|
|
|
structure = ndimage.generate_binary_structure(2, 2)
|
|
|
eroded_mask = ndimage.binary_erosion(mask, structure=structure)
|
|
|
boundary_mask = mask & ~eroded_mask
|
|
|
|
|
|
|
|
|
y_indices, x_indices = np.where(boundary_mask)
|
|
|
if len(y_indices) == 0 or len(x_indices) == 0:
|
|
|
continue
|
|
|
|
|
|
|
|
|
transform = area_ds.GetGeoTransform()
|
|
|
x_coords = transform[0] + x_indices * transform[1]
|
|
|
y_coords = transform[3] + y_indices * transform[5]
|
|
|
|
|
|
|
|
|
dem_transform = dem_ds.GetGeoTransform()
|
|
|
dem_xs = ((x_coords - dem_transform[0]) / dem_transform[1]).astype(int)
|
|
|
dem_ys = ((y_coords - dem_transform[3]) / dem_transform[5]).astype(int)
|
|
|
|
|
|
|
|
|
boundary_depths = []
|
|
|
for dem_x, dem_y in zip(dem_xs, dem_ys):
|
|
|
if 0 <= dem_x < dem_array.shape[1] and 0 <= dem_y < dem_array.shape[0]:
|
|
|
boundary_depths.append(dem_array[dem_y, dem_x])
|
|
|
|
|
|
|
|
|
if len(boundary_depths) > 0:
|
|
|
median_boundary_depth = np.nanmedian(boundary_depths)
|
|
|
else:
|
|
|
median_boundary_depth = np.nan
|
|
|
|
|
|
|
|
|
results.append({
|
|
|
'date': date,
|
|
|
'lake_id': label,
|
|
|
'longitude': np.nanmean(x_coords),
|
|
|
'latitude': np.nanmean(y_coords),
|
|
|
'depth': median_boundary_depth
|
|
|
})
|
|
|
print(f"结果:{results}")
|
|
|
|
|
|
|
|
|
|
|
|
nodata_value = -9999
|
|
|
output_array = np.full_like(relabeled_array, nodata_value, dtype=np.float32)
|
|
|
|
|
|
|
|
|
for result in results:
|
|
|
label = result['lake_id']
|
|
|
median_depth = result['depth']
|
|
|
mask = relabeled_array == label
|
|
|
output_array[mask] = median_depth
|
|
|
|
|
|
|
|
|
gdal.SetConfigOption('GTIFF_SRS_SOURCE', 'EPSG')
|
|
|
|
|
|
|
|
|
driver = gdal.GetDriverByName('GTiff')
|
|
|
output_path = os.path.join(output_dir, f'{date}_processed_B.tif')
|
|
|
|
|
|
output_ds = driver.Create(output_path, relabeled_array.shape[1], relabeled_array.shape[0], 1, gdal.GDT_Float32)
|
|
|
|
|
|
output_ds.SetGeoTransform(area_ds.GetGeoTransform())
|
|
|
output_ds.SetProjection(area_ds.GetProjection())
|
|
|
|
|
|
output_band = output_ds.GetRasterBand(1)
|
|
|
output_band.WriteArray(output_array)
|
|
|
|
|
|
output_band.SetNoDataValue(nodata_value)
|
|
|
|
|
|
output_ds.FlushCache()
|
|
|
|
|
|
output_ds = None
|
|
|
print(f"结果已写入:{output_path}")
|
|
|
|