import gdal
import numpy as np
class Dataset:
def __init__(self, in_file):
self.in_file = in_file # Tiff或者ENVI文件
dataset = gdal.Open(self.in_file)
self.XSize = dataset.RasterXSize # 网格的X轴像素数量
self.YSize = dataset.RasterYSize # 网格的Y轴像素数量
self.GeoTransform = dataset.GetGeoTransform() # 投影转换信息
self.ProjectionInfo = dataset.GetProjection() # 投影信息
def get_data(self, band):
"""
band: 读取第几个通道的数据
"""
dataset = gdal.Open(self.in_file)
band = dataset.GetRasterBand(band)
data = band.ReadAsArray()
return data
def get_lon_lat(self):
"""
获取经纬度信息
"""
gtf = self.GeoTransform
x_range = range(0, self.XSize)
y_range = range(0, self.YSize)
x, y = np.meshgrid(x_range, y_range)
lon = gtf[0] + x * gtf[1] + y * gtf[2]
lat = gtf[3] + x * gtf[4] + y * gtf[5]
return lon, lat
使用上述读取类的样例代码
# 以下代码演示读取E:/data/dataset.tif的第一个通道的数据,并且获取经纬度信息
dir_path = r"E:/data"
filename = "dataset.tif"
file_path = os.path.join(dir_path, filename)
dataset = Dataset(file_path)
band = 1
data = dataset.get_data(band) # 获取第一个通道的数据
longitude, latitude = dataset.get_lon_lat() # 获取经纬度信息
https://blog.csdn.net/weixin_40450867/article/details/81097566
https://blog.csdn.net/zyf19930610/article/details/43487473