Python遥感开发之GDAL读写遥感影像
- 1 读取tif信息方法一
- 2 读取tif信息方法二
- 3 自己封装读取tif的方法(推荐)
- 4 对读取的tif数据进行简单运算
- 5 写出tif影像(推荐)
前言:主要介绍了使用GDAL读写遥感影像数据的操作,包括读取行、列、投影、值以及数据的简单运算和生成新的tif影像。
1 读取tif信息方法一
from osgeo import gdal
import numpy as np
if __name__ == '__main__':
dataset = gdal.Open("lucc.tif")
col = dataset.RasterXSize
print("col:",col)
row = dataset.RasterYSize
print("row:", row)
geotrans = dataset.GetGeoTransform()
print("geotrans:", geotrans)
proj = dataset.GetProjection()
print("proj:", proj)
data = dataset.ReadAsArray()
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
print("data:", data)
for i in range(0,row):
print(i,data[i])
for i in range(0,row):
for j in range(0,col):
if not np.isnan(data[i][j]):
print(data[i][j])
2 读取tif信息方法二
from osgeo import gdalnumeric
import numpy as np
if __name__ == '__main__':
data = gdalnumeric.LoadFile("lucc.tif")
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
for d in data:
print(d)
for d in data:
for s in d:
print(s)
3 自己封装读取tif的方法(推荐)
import numpy as np
from osgeo import gdal,gdalnumeric
def read_tif01(filepath):
dataset = gdal.Open(filepath)
col = dataset.RasterXSize
row = dataset.RasterYSize
geotrans = dataset.GetGeoTransform()
proj = dataset.GetProjection()
data = dataset.ReadAsArray()
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
return [col, row, geotrans, proj, data]
def read_tif02(filepath):
data = gdalnumeric.LoadFile(filepath)
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
return data
if __name__ == '__main__':
col, row, geotrans, proj, data = read_tif01("lucc.tif")
data2 = read_tif02("lucc.tif")
4 对读取的tif数据进行简单运算
import numpy as np
from osgeo import gdal
def read_tif01(filepath):
dataset = gdal.Open(filepath)
col = dataset.RasterXSize
row = dataset.RasterYSize
geotrans = dataset.GetGeoTransform()
proj = dataset.GetProjection()
data = dataset.ReadAsArray()
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
return [col, row, geotrans, proj, data]
if __name__ == '__main__':
col, row, geotrans, proj, data = read_tif01("lucc.tif")
print(data[1])
data = data*2
print(data[1])
data[data==6] = 10
print(data[1])
5 写出tif影像(推荐)
import numpy as np
from osgeo import gdal
def read_tif01(filepath):
dataset = gdal.Open(filepath)
col = dataset.RasterXSize
row = dataset.RasterYSize
geotrans = dataset.GetGeoTransform()
proj = dataset.GetProjection()
data = dataset.ReadAsArray()
data = data.astype(np.float32)
a = data[0][0]
data[data == a] = np.nan
return [col, row, geotrans, proj, data]
def save_tif(data, file, output):
ds = gdal.Open(file)
shape = data.shape
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(output, shape[1], shape[0], 1, gdal.GDT_Float32)
dataset.SetGeoTransform(ds.GetGeoTransform())
dataset.SetProjection(ds.GetProjection())
dataset.GetRasterBand(1).WriteArray(data)
if __name__ == '__main__':
col, row, geotrans, proj, data = read_tif01("lucc.tif")
data = data*2
save_tif(data,"lucc.tif","new_lucc.tif")