gdal读取TIFF数据计算风速

1. gdal读取TIFF

使用10米的u和v分量数据。u为正,表示西风,从西边吹来的风。v为正,表示南风,从南边吹来的风。

方法一:

# 读取TIFF
u_component_of_wind_10m_ = gdal.Open("./output_data/wind_speed.tif")
# u_component_of_wind_10m_ = gdal.Open("./data/u_component_of_wind_10m.tif")
u_component_of_wind_10m__col = u_component_of_wind_10m_.RasterXSize  # 列数 宽 经度范围
u_component_of_wind_10m__row = u_component_of_wind_10m_.RasterYSize  # 行数
print(u_component_of_wind_10m__col)
print(u_component_of_wind_10m__row)
# 获取TIFF波段数量
u_component_of_wind_10m__bands_count = u_component_of_wind_10m_.RasterCount
print(u_component_of_wind_10m__bands_count)

# 转为numpy格式获取栅格像元值
u_component_of_wind_10m__data = u_component_of_wind_10m_.ReadAsArray()
print(u_component_of_wind_10m__data)

# 读取投影
u_component_of_wind_10m__proj = u_component_of_wind_10m_.GetProjection()
print(u_component_of_wind_10m__proj)

# 获取投影转换信息
u_component_of_wind_10m__geo = u_component_of_wind_10m_.GetGeoTransform()
print(u_component_of_wind_10m__geo)


方法二:

u_component_of_wind_10m = "./data/u_component_of_wind_10m.tif"
v_component_of_wind_10m = "./data/v_component_of_wind_10m.tif"

# 获取TIFF信息
def readtif(filepath):
    dataset = gdal.Open(filepath)
    col = dataset.RasterXSize  # 获取TIFF的行列数
    row = dataset.RasterYSize
    geotrans = dataset.GetGeoTransform()  # 获取投影转换信息仿射变换
    proj = dataset.GetProjection()  # 获取地图投影信息
    data = dataset.ReadAsArray()  # 获取栅格像元值
    # return [datasat,col, row, geotrans, proj, data]
    return dataset, data

2. 计算风速

# 定义一个函数将UV分量转换成风速和风向
def transUV(u, v):
    data_u = units.Quantity(u, 'm/s')
    data_v = units.Quantity(v, 'm/s')
    # data_dir = mpcalc.wind_direction(data_u, data_v)  # 风向
    data_speed = mpcalc.wind_speed(data_u, data_v, )  # 风速
    return np.array(data_speed)

3. gdal对风速重分类

gdal读取TIFF数据计算风速_第1张图片


# 计算获取风速的像素值
u_datasat, u_data = readtif(u_component_of_wind_10m)
v_datasat, v_data = readtif(v_component_of_wind_10m)
wind_speed_data = transUV(u_data, v_data)
print("wind_speed_data:\n", wind_speed_data)

# 风速重分类
re_wind_speed = wind_speed_data
re_wind_speed[17.2 < re_wind_speed] = 31
re_wind_speed[(14.0 < re_wind_speed) & (re_wind_speed <= 17.2)] = 27
re_wind_speed[(10.9 < re_wind_speed) & (re_wind_speed <= 14.0)] = 23
re_wind_speed[(8.1 < re_wind_speed) & (re_wind_speed <= 10.9)] = 19
re_wind_speed[(5.6 < re_wind_speed) & (re_wind_speed <= 8.1)] = 15
re_wind_speed[(3.5 < re_wind_speed) & (re_wind_speed <= 5.6)] = 12
re_wind_speed[(1.5 < re_wind_speed) & (re_wind_speed <= 3.5)] = 8
re_wind_speed[re_wind_speed <= 1.5] = 4
print("re_wind_speed:\n", re_wind_speed)

4. gdal输出影像

# 输出影像
def wirte_tiff(dataset, im_data, path):
    im_proj = dataset.GetProjection()
    im_geo = dataset.GetGeoTransform()
    if len(im_data) == 3:
        im_bands, im_height, im_width = im_data.shape
    else:
        im_height, im_width = im_data.shape
        im_bands = 1
    driver = gdal.GetDriverByName('GTiff')
    new_dataset = driver.Create(path, im_width, im_height, im_bands, gdal.GDT_Float64)

    if new_dataset is not None:
        new_dataset.SetGeoTransform(im_geo)
        new_dataset.SetProjection(im_proj)

    if im_bands == 1:
        new_dataset.GetRasterBand(1).WriteArray(im_data)
    else:
        for i in range(im_bands):
            new_dataset.GetRasterBand(i + 1).WriteArray(im_data[i])
    del new_dataset


# 输出风速tiff数据
output_path = r"./output_data/wind_speed.tif"
wirte_tiff(u_datasat, wind_speed_data, output_path)

导出tiff在arcgis或envi查看即可

你可能感兴趣的:(Python学习,numpy,python,开发语言)