tif文件转csv文件

tif文件转csv文件

import os
import csv
import pandas as pd
from osgeo import gdal

file_chdir=os.path.join(os.getcwd(),'data','data_tif')
for root,dirs,files in os.walk(file_chdir):
    for file in files:
        suffix_name=os.path.splitext(file)[1]
        if suffix_name=='.tif':
            name,year=file.split('.')[0].split('_')
            file_name='data/data_tif/'+file # tif文件路径
            open_name='data/data_tif_csv/'+file.split('.')[0]+'.csv'# 保存文件路径
            print(file_name)
            f=open(open_name, 'w', newline='',encoding='utf-8')   
            writer = csv.writer(f)
            writer.writerow(['date','lon', 'lat', 'grid_col','grid_row',name])
            dataset = gdal.Open(file_name)
            band=dataset.RasterCount # 通道数
            col = dataset.RasterXSize # 列数
            row = dataset.RasterYSize # 行数
            geo_information = dataset.GetGeoTransform()  
            for day in range(1,band+1):  
                dem = dataset.GetRasterBand(day).ReadAsArray() # 遍历每个通道
                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]
                            writer.writerow([date,lon, lat, y, x, dem[y][x]])

你可能感兴趣的:(其他,python,开发语言,tif文件,文件转换)