利用python将所有时序NDVI影像转换为DataFrame数据并进行归一化


代码如下:

# coding:utf-8
import gdal
import pandas as pd
from pandas import DataFrame
#首先读取tif文件
path = r"D:\NDVI.tif"
dataset = gdal.Open(path)
im_width = dataset.RasterXSize;#获取宽度
im_height = dataset.RasterYSize;#获取长度
data = dataset.ReadAsArray(0,0,im_width,im_height);//#读取ndvi时序数据,三维矩阵
dict = {}#构造字典
for i in range(data.shape[1]):
  for j in range(data.shape[2]):
    tempDS = []#定义一个链表,存储每个像素位置的数据
    for k in range(data.shape[0]):
        tempDS.append(data[k,i,j])
    index = str.format('{0}-{1}',i,j)#构建索引    
    dict[index] = tempDS

#上面循环完毕之后,所有的时序NDVI数据在dict中,下面根据dict构建DataFrame
myIndex = ["年-月-日"...]
df = pd.DataFrame(dict,index=myIndex )


#构建df完毕后可以存储,也可以选择进行归一化,NDVI的范围为[-1,1],所以可以直接归一化
for x in df.columns:
    for i in range(len(df[x])):
        df[x,i] = (df[x,i]+1)/2;

#归一化之后存储数据
df.to_csv(r"D:\ndviTimeSeries.csv")

转载自:https://blog.csdn.net/t46414704152abc/article/details/78468820

You may also like...