import gdal
import numpy as np
from scipy import ndimage as nd
file = r"D:\微信公众号\数据填补\处理前.tif"
outfile = r"D:\微信公众号\数据填补\处理后.tif"
ds = gdal.Open(file)
cols = ds.RasterXSize
rows = ds.RasterYSize
geo = ds.GetGeoTransform()
proj = ds.GetProjection()
band = ds.GetRasterBand(1)
d_type = band.DataType
nodata = band.GetNoDataValue()
data = (ds.ReadAsArray()).astype(np.float32)
data[data ==nodata] = np.nan
mask = np.isnan(data)
ind = nd.distance_transform_edt(mask,
return_distances=False,
return_indices=True)
data = data[tuple(ind)]
driver = gdal.GetDriverByName("GTiff")
outds = driver.Create(outfile, cols, rows, 1, d_type)
outds.SetGeoTransform(geo)
outds.SetProjection(proj)
outband = outds.GetRasterBand(1)
outband.WriteArray(data)
outband.SetNoDataValue(nodata)
测试数据:
百度网盘:https://pan.baidu.com/s/1ZqAlIe4vGBF7SvTMNhhfrg
密 码:e3pd
关注我的个人WX_GZH:小Rser