使用python读取tiff文件中的经纬度,并将数据以excel表的形式输出(详细步骤)

近日,因为某任务,需要批量的读取tiff文件里的经度、纬度和高程,最后生成excel。

步骤如下:

1.安装gdal包,一般"pip install gdal"都会报错,解决办法见地址

2.获取tif对象

filePath = 'tif_k36a/K36TIFF.tif'  # tif文件路径
dataset = gdal.Open(filePath)  # 打开tif

3.获取地理信息、行数、列数和波段,这里我的band输出是“1”.

geo_information = dataset.GetGeoTransform()
col = dataset.RasterXSize  # 438
row = dataset.RasterYSize  # 671
band = dataset.RasterCount
dem = dataset.GetRasterBand(1).ReadAsArray()

其中:

geo_information(0):左上像素左上角的x坐标。
geo_information(1):w - e像素分辨率 / 像素宽度。
geo_information(2):行旋转(通常为零)。
geo_information(3):左上像素左上角的y坐标。
geo_information(4):列旋转(通常为零)。
geo_information(5):n - s像素分辨率 / 像素高度(北半球上图像为负值)

4.获取经纬度和高程,以及在原来tif栅格中的行列坐标位置。放数据放在cols中。

    cols = []
    for y in range(row):  # 行
        rows = []
        for x in range(col):  # 列
            # 有效高程
            if dem[y][x] > 0:
                # 输出经纬度
                lon = geo_information[0] + x * geo_information[1] + y * geo_information[2]
                lat = geo_information[3] + x * geo_information[4] + y * geo_information[5]
                child = [lon, lat, dem[y][x], y, x]
                rows.append(child)
        cols.append(rows)

5.生成excel表

    ws = work.active
    ws['A1'] = '经度'
    ws['B1'] = '纬度'
    ws['C1'] = '高程'
    ws['D1'] = '所在栅格行'
    ws['E1'] = '所在栅格列'
    for i in range(len(data)):
        rows = []
        row_length = len(data[i])
        if row_length != 0:
            for j in range(row_length):
                rows.append(data[i][j])
                ws.append(rows[j])
        print(rows)
    work.save(name)

6.整体代码

# coding UTF-8
# author:huangZengli
from osgeo import gdal
from pylab import *  # 支持中文

mpl.rcParams['font.sans-serif'] = ['SimHei']
from openpyxl import Workbook

# 创建一个Workbook对象
work = Workbook()

def out(data, name):
    ws = work.active
    ws['A1'] = '经度'
    ws['B1'] = '纬度'
    ws['C1'] = '高程'
    ws['D1'] = '所在栅格行'
    ws['E1'] = '所在栅格列'
    for i in range(len(data)):
        rows = []
        row_length = len(data[i])
        if row_length != 0:
            for j in range(row_length):
                rows.append(data[i][j])
                ws.append(rows[j])
        print(rows)
    work.save(name)

if __name__ == "__main__":
    filePath = 'tif_k82a/K82A.tif'  # tif文件路径
    dataset = gdal.Open(filePath)  # 打开tif

    # 获取行数列数和地理信息
    # geo_information(0):左上像素左上角的x坐标。
    # geo_information(1):w - e像素分辨率 / 像素宽度。
    # geo_information(2):行旋转(通常为零)。
    # geo_information(3):左上像素左上角的y坐标。
    # geo_information(4):列旋转(通常为零)。
    # geo_information(5):n - s像素分辨率 / 像素高度(北半球上图像为负值)
    geo_information = dataset.GetGeoTransform()
    col = dataset.RasterXSize  # 438
    row = dataset.RasterYSize  # 671
    band = dataset.RasterCount
    dem = dataset.GetRasterBand(1).ReadAsArray()
    # 获取行列数,对应其经纬度,j对于x坐标
    cols = []
    for y in range(row):  # 行
        rows = []
        for x in range(col):  # 列
            # 有效高程
            if dem[y][x] > 0:
                # 输出经纬度
                lon = geo_information[0] + x * geo_information[1] + y * geo_information[2]
                lat = geo_information[3] + x * geo_information[4] + y * geo_information[5]
                child = [lon, lat, dem[y][x], y, x]
                rows.append(child)
        cols.append(rows)
    out(cols, '从8标渣场tif提取的数据(待验证).xlsx')
    print('表已经生成')

感谢各位大佬阅读!如有错误请望指正!

  • 17
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值