利用双线性内插制作10mDEM。py
时间: 2024-11-12 22:42:57 浏览: 23
利用Python制作10米分辨率数字高程模型(DEM,Digital Elevation Model),通常会涉及到地理信息系统(GIS)库如GDAL或Rasterio,以及数据处理库如NumPy。双线性内插(Bilinear Interpolation)是一种常见的栅格数据插值方法,用于估算未采样点的属性值。下面是大致步骤:
1. 导入所需库:
```python
import rasterio
from rasterio.warp import reproject, Resampling
import numpy as np
```
2. 加载原始DEM数据:
```python
with rasterio.open('source_dem.tif') as src:
source_data = src.read(1) # 读取第1个波段(通常是海拔)
source_transform = src.transform
source_crs = src.crs
```
3. 定义新的坐标系统和分辨率(10米分辨率):
```python
target_transform = Affine(src_transform.a, 0, src_transform.c,
0, src_transform.e, src_transform.f)
target_width, target_height = (int(source_data.shape[1] * 10), int(source_data.shape[0] * 10))
```
4. 创建目标数组,并填充边缘值以进行插值:
```python
target_data = np.zeros((target_height, target_width))
# 可能需要设置边界条件(例如使用src nodata 或者周围像素的平均值等)
```
5. 执行双线性内插:
```python
reproject(
source_data,
target_data,
src_transform=source_transform,
src_crs=source_crs,
dst_transform=target_transform,
dst_crs=source_crs,
resampling=Resampling.bilinear,
)
```
6. 将结果保存到新的tif文件:
```python
with rasterio.open('output_10mDEM.tif', 'w', driver='GTiff',
height=target_height, width=target_width,
count=1, dtype=np.float32,
crs=source_crs, transform=target_transform) as dst:
dst.write(target_data, 1)
```
阅读全文