python rasterio库使用
打开tif图像:rasterDataset = rasterio.open(path)
关闭图像:rasterDataset.close()
注:文件的打开可以使用with as格式
数据相关属性
数据格式:rasterDataset .driver
数据的元信息集合:rasterDataset .profile
数据的波段数目:rasterDataset .count
数据的影像宽度:rasterDataset .width
数据的影像高度:rasterDataset .height
数据的地理范围:rasterDataset .bounds
数据的仿射变换参数(六参数模型):rasterDataset .transform
数据的投影:rasterDataset .crs
数据相关函数
波段读取:band = rasterDataset .read(index)
index数从1开始,不能超过数据的波段数。如果read()函数不加参数,则得到所有波段(第一个维度是波段)。band为numpy.ndarray类型。
根据地理坐标获取对应的行列坐标:row, col = rasterDataset .index(x, y)
根据行列坐标获取对应的地理坐标(像元中心):x, y = rasterDataset .xy(row, col)
根据行列坐标获取对应的地理坐标(像元左上角):x, y = (row, col) * rasterDataset .transform
数据的元信息
数据的元信息集合profile
为字典形式,key
内容如下:
driver:驱动,例如‘GTiff’
dtype:图像数据类型,例如'float64'
nodata:图像无效值
width:图像宽
height:图像高
count:图像波段数
crs:图像坐标系
transform:仿射变换参数
blockxsize:图像瓦片长度
blockysize:图像瓦片高度
tiled:是否使用瓦片
compress:例如’lzw‘
interleave:例如’band‘
生成profile
profile = dict()
profile['driver'] = 'GTiff'
profile['dtype'] = 'float64'
profile['nodata'] = 65535
profile['width'] = 79
profile['height'] = 75
profile['count'] = 1
profile['crs'] = rasterio.crs.CRS.from_epsg(32650)
profile['transform'] = affine.Affine(1000.0, 0.0, 439384.7649892941, 0.0, -1000.0, 4506138.982869381)
profile['blockxsize'] = 128
profile['blockysize'] = 128
profile['tiled'] = True
profile['compress'] = 'lzw'
profile['interleave'] = 'band'
数据存储
with rasterio.open(outPath, mode='w', **profile) as dst:
dst.write(data, 1)