幸运的辣条条 2022-12-16 19:28 采纳率: 100%
浏览 119
已结题

python实现sen-mk

本人利用python对20年NDVI栅格数据进行sen-mk检验,总文件45G,在读取栅格数据时出现内存溢出错误。在此想请较大家如何修改读取栅格语句,循环释放内存?
for file in os.listdir(path1):
filepath1=os.path.join(path1,file)
filepaths.append(filepath1)

#获取影像数量
num_images=len(filepaths)
#读取影像数据
img1=ras.open(filepaths[0])
#获取影像的投影,高度和宽度
transform1=img1.transform
height1=img1.height
width1=img1.width 
array1=img1.read()
img1.close()

#读取所有影像
for path1 in filepaths[1:]:
    if path1[-3:]=='tif':
        print(path1)
        img2=ras.open(path1)
        array2=img2.read()
        array1=np.vstack((array1,array2))
        img2.close()

array1=np.vstack((array1,array2))
File "<__array_function__ internals>", line 180, in vstack
File "E:\pychram\lib\site-packages\numpy\core\shape_base.py", line 282, in vstack
return _nx.concatenate(arrs, 0)
File "<__array_function__ internals>", line 180, in concatenate
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 17.1 GiB for an array with shape (8, 20354, 28112) a

  • 写回答

3条回答 默认 最新

  • yy64ll826 2022-12-16 20:57
    关注

    批处理,类似这也的试试

    # -*- coding: utf-8 -*-
    import os
    import arcpy
    from arcpy import env
    from arcpy.sa import *
    
    arcpy.env.workspace = "e:/MODISNDVI/BYNDVI"
    rasters = arcpy.ListRasters("*","tif")
    out_path = "e:/MODISNDVI/BYNDVI_cl/"
    for raster in rasters:
        
        (filepath, fullname) = os.path.split(raster)
        (prename, suffix) = os.path.splitext(fullname)
        print(prename)
        arcpy.CheckOutExtension("ImageAnalyst") #检查许可
        arcpy.CheckOutExtension("spatial") #检查许可
        whereClause = "VALUE = -3000" #无效值
        outSetNull = SetNull(raster, raster, whereClause) * 0.0001 #去除无效值并乘以0.0001
        #outname=r"E:\MODISNDVI\BYNDVI\try1.tif" #输出路径
        outSetNull.save(out_path + prename + '_ndvi_qcwxz.tif') #保存数据
        print('over')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月16日