php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 568|回复: 0

批量NDVI时序

[复制链接]

2632

主题

2639

帖子

9357

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6603
贡献
0
注册时间
2021-4-14
最后登录
2024-4-30
在线时间
668 小时
QQ
发表于 2022-6-6 22:54:01 | 显示全部楼层 |阅读模式
[mw_shl_code=python,true]import os
from PIL import Image
import numpy as np
from osgeo import gdal
import glob
import cv2
list_tif = glob.glob('D:/BaiduNetdiskDownload/Shangyu/20210605/files/*.tif')
out_path = 'D:/NDVI/20210605/'
print(list_tif)
#记录异常值
count = 0

for tif in list_tif:
    in_ds = gdal.Open(tif)
    # 获取⽂件所在路径以及不带后缀的⽂件名
    (filepath, fullname) = os.path.split(tif)
    (prename, suffix) = os.path.splitext(fullname)
    if in_ds is None:
        print('Could not open the file ' + tif)
    else:
        # 读取波段数据
        red = in_ds.GetRasterBand(1).ReadAsArray()*0.0001
        nir = in_ds.GetRasterBand(4).ReadAsArray()*0.0001
        np.seterr(divide='ignore', invalid='ignore')
        ndvi = (nir - red) / (nir + red)
        # 将NAN转化为0值,如nodata转为0
        nan_index = np.isnan(ndvi)
        ndvi[nan_index] = 0
        ndvi = ndvi.astype(np.float32)
        #将计算好的NDVI保存为GeoTiff⽂件
        gtiff_driver = gdal.GetDriverByName('GTiff')
        # 批量处理需要注意⽂件名是变量,这⾥截取对应原始⽂件的不带后缀的⽂件名
        out_ds = gtiff_driver.Create(out_path + prename + 'NDVI.tif',
                         ndvi.shape[1], ndvi.shape[0], 1, gdal.GDT_Float32)
        # 将NDVI数据坐标投影设置为原始坐标投影
        out_ds.SetProjection(in_ds.GetProjection())
        out_ds.SetGeoTransform(in_ds.GetGeoTransform())
        out_band = out_ds.GetRasterBand(1)
        out_band.WriteArray(ndvi)
        out_band.FlushCache()
[/mw_shl_code]





上一篇:创建数据库、表、修改和删除表
下一篇:基于tkinter、ttkbootstrap的Python图片识别文字
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-30 15:51 , Processed in 0.189259 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表