使用Rasterio创建栅格数据
方法描述
代码示例
import rasterio
import numpy as np
# 读入的数据是绿,红,近红外波段的合成数据
with rasterio.open('LC08_122043_20161207.tif') as src:
raster = src.read() # 读取所有波段
# 源数据的元信息集合(使用字典结构存储了数据格式,数据类型,数据尺寸,投影定义,仿射变换参数等信息)
profile = src.profile
# 计算NDVI指数(对除0做特殊处理)
with np.errstate(divide='ignore', invalid='ignore'):
ndvi = (raster[2] - raster[1]) / (raster[2] + raster[1])
ndvi[ndvi == np.inf] = 0
ndvi = np.nan_to_num(ndvi)
# 写入数据
profile.update(
dtype=ndvi.dtype,
count=1
)
'''也可以在rasterio.open()函数中依次列出所有的参数
with rasterio.open('NDVI.tif', mode='w', driver='GTiff',
width=src.width, height=src.height, count=1,
crs=src.crs, transform=src.transform, dtype=ndvi.dtype) as dst:
'''
with rasterio.open('NDVI.tif', mode='w', **profile) as dst:
dst.write(ndvi, 1)Last updated