使用 arcpy 列出栅格非常慢
Very slow to list rasters with arcpy
我使用的是简单的 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 分钟...这怎么可能?我是否遗漏了代码中的某些内容?是因为栅格的大小吗?我可以检查一些 "hidden" 选项或 "trick" 吗?我在带有 16Go RAM 的 i7 计算机上使用 Win 7 64。
感谢任何可以减少此处理时间的想法..!
根据我的经验,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
根据栅格的格式,每个栅格可能包含多个文件。如果是这样,您也必须将其合并到代码中。
祝你好运!
汤姆
我更喜欢使用 glob
来列出栅格等数据。与 arcpy.ListRasters()
方法相比,您会发现该操作非常快。我修改了您的示例以使用 glob
——这里我假设您使用的是 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"))
我使用的是简单的 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 分钟...这怎么可能?我是否遗漏了代码中的某些内容?是因为栅格的大小吗?我可以检查一些 "hidden" 选项或 "trick" 吗?我在带有 16Go RAM 的 i7 计算机上使用 Win 7 64。
感谢任何可以减少此处理时间的想法..!
根据我的经验,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
根据栅格的格式,每个栅格可能包含多个文件。如果是这样,您也必须将其合并到代码中。
祝你好运!
汤姆
我更喜欢使用 glob
来列出栅格等数据。与 arcpy.ListRasters()
方法相比,您会发现该操作非常快。我修改了您的示例以使用 glob
——这里我假设您使用的是 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"))