代码之家  ›  专栏  ›  技术社区  ›  Marc

使用arcpy列出光栅非常慢

  •  1
  • Marc  · 技术社区  · 11 年前

    我使用的是简单的arcpy ListRasters 函数,脚本执行速度非常慢。

    我将它们与一个包含大约100个光栅(5个不同类别的20年历史数据)的文件夹一起使用。每个光栅约为800Mo,因此总共约为80Go。我使用通配符将它们列在5个不同的列表中:

    import arcpy, os, sys
    from arcpy import env
    from arcpy import *
    from arcpy.sa import *
    
    arcpy.CheckOutExtension("Spatial")
    
    hist_data_path = "D:/Data/GIS/hist_data"
    
    arcpy.env.workspace = hist_data_path
    
    hist_pop_urban = arcpy.ListRasters("*pop_urb*")
    hist_pop_rural = arcpy.ListRasters("*pop_rur*")
    hist_ppc_urban = arcpy.ListRasters("*ppc_urb*")
    hist_ppc_rural = arcpy.ListRasters("*ppc_rur*")
    hist_ww_int = arcpy.ListRasters("*ww_int*")
    
    [...]
    

    列出每组20个光栅大约需要10分钟……所以列出所有光栅大约需要50分钟……这是怎么可能的?我错过了代码中的某些内容吗?是因为光栅的大小吗?是否有一些“隐藏”选项或“技巧”可以检查?我在i7电脑上使用Win 7 64,带有16Go RAM。

    感谢您提出任何可以缩短处理时间的建议。。!

    2 回复  |  直到 11 年前
        1
  •  1
  •   TomAdair    11 年前

    根据我的经验,arcpy通常很慢,所以我尽量避免它。当然,可能有一些方法可以优化arcpy。ListRasters函数,如果有人知道它,我很乐意听到它。

    以下是arcpy.ListRasters的开箱即用Python替代方案:

    import os
    
    directory = r"D:\Data\GIS\hist_data"
    extension_list = [".tif", ".tiff"]
    hist_pop_urban = []
    hist_pop_rural = []
    #etc.
    
    for file in os.listdir(directory):
        for extension in extension_list:
            if file.endswith(extension):
                if "pop_urb" in file:
                    hist_pop_urban.append(file)
                elif "pop_rur" in file:
                    hist_pop_rural.append(file)
                #etc.

    您可以基于此网页的内容和您所处理的特定文件类型的知识构建extension_list:

    http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009t0000000q000000

    根据光栅的格式,每个光栅可能包含多个文件。如果是这样的话,您也必须将其合并到代码中。

    祝你好运

    汤姆

        2
  •  1
  •   Borealis    10 年前

    我更喜欢使用 glob 用于列出诸如光栅的数据。你会发现与 arcpy.ListRasters() 方法我修改了您的示例以使用 地球仪 --这里我假设您使用的是tif格式的光栅数据(如果需要,请更改)。

    import glob
    
    inws = r'C:\path\to\your\workspace'
    
    hist_pop_urban = glob.glob(os.path.join(inws, "*pop_urb*.tif"))
    hist_pop_rural = glob.glob(os.path.join(inws, "*pop_rur*.tif"))
    hist_ppc_urban = glob.glob(os.path.join(inws, "*ppc_urb*.tif"))
    hist_ppc_rural = glob.glob(os.path.join(inws, "*ppc_rur*.tif"))
    hist_ww_int =    glob.glob(os.path.join(inws, "*ww_int*.tif"))