gdal 遥感影像水体数据提取
时间: 2023-07-15 10:12:16 浏览: 76
要使用GDAL提取遥感影像中的水体数据,可以采用以下步骤:
1. 打开遥感影像文件:
```python
from osgeo import gdal
# 打开遥感影像文件
ds = gdal.Open("path/to/image.tif")
if ds is None:
print("无法打开遥感影像文件")
```
2. 获取地理信息和投影信息:
```python
# 获取地理信息和投影信息
geotransform = ds.GetGeoTransform()
proj = ds.GetProjection()
```
3. 读取影像数据:
```python
# 读取影像数据
band = ds.GetRasterBand(1)
data = band.ReadAsArray()
```
4. 对影像数据进行处理,提取水体:
```python
import numpy as np
# 提取水体
water_mask = np.zeros_like(data)
water_mask[data <= threshold] = 1
```
其中,threshold是一个阈值,可以根据不同遥感影像的特点进行调整。
5. 导出水体数据:
```python
# 创建输出文件
driver = gdal.GetDriverByName("GTiff")
out_ds = driver.Create("path/to/water_mask.tif", ds.RasterXSize, ds.RasterYSize, 1, gdal.GDT_Byte)
# 设置地理信息和投影信息
out_ds.SetGeoTransform(geotransform)
out_ds.SetProjection(proj)
# 写入数据
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(water_mask)
# 释放资源
out_band.FlushCache()
out_band = None
out_ds = None
```