【发布时间】:2016-08-18 07:46:39
【问题描述】:
我正在使用arcpy 对栅格进行一些数字运算,并希望使用multiprocessing 包来加快速度。基本上,我需要遍历一个元组列表,使用每个元组进行一些栅格计算并将一些输出写入文件。我的输入包括一个数据栅格(测深)、一个定义区域的栅格和一个由两个浮点数组成的元组(水面高程、深度)。我的程序包含一个函数computeplane,它接受一个元组并运行一系列栅格计算以生成五个栅格(总、沿海、表面、次表面、深部),然后为每个这些栅格调用函数processtable使用arcpy.sa.ZonalStatisticsAsTable将值写入dbf,使用arcpy.AddField_management添加一些字段,使用arcpy.TableToTable_conversion将dbf转换为csv,最后使用arcpy.Delete_management删除dbf文件。
基于一些other SO posts,我已经将我的代码封装在main() 中,这样multiprocessing.Pool 应该会很好玩。我使用main() 创建元组集,使用pool.map 进行多处理。我使用tempfile 包为dbf 文件选择名称以避免名称冲突;保证 csv 文件名不会与其他线程冲突。
我已经用 for 循环测试了我的代码,它工作正常,但是当我尝试使用 pool.map 时,我得到了
RuntimeError:错误 010240:无法将栅格数据集保存到 C:\Users\Michael\AppData\Local\ESRI\Desktop10.4\SpatialAnalyst\lesst_ras 输出格式为 GRID。
这里发生了什么?此错误不会出现在代码的非多进程版本中,而且我没有在任何地方写出光栅 --- 再说一遍,我没有不知道arcpy 如何处理中间栅格(我当然认为它不会将它们保存在内存中——它们太大了)。我是否需要告诉arcpy 它处理光栅计算以使多处理工作的方式?我在下面包含了我的 python 文件。
import arcpy
arcpy.CheckOutExtension("Spatial")
import arcpy.sa
import numpy
import multiprocessing
import tempfile
bathymetry_path = r'C:/GIS workspace/RRE/habitat.gdb/bathymetry_NGVD_meters'
zones_path = r'C:/GIS workspace/RRE/habitat.gdb/markerzones_meters'
table_folder = r'C:/GIS workspace/RRE/zonetables'
bathymetry = arcpy.sa.Raster(bathymetry_path)
zones = arcpy.sa.Raster(zones_path)
def processtable(raster_obj, zone_file, w, z, out_folder, out_file):
temp_name = "/" + next(tempfile._get_candidate_names()) + ".dbf"
arcpy.sa.ZonalStatisticsAsTable(zone_file, 'VALUE', raster_obj, out_folder + temp_name, "DATA", "SUM")
arcpy.AddField_management(out_folder + temp_name, "wse", 'TEXT')
arcpy.AddField_management(out_folder + temp_name, "z", 'TEXT')
arcpy.CalculateField_management(out_folder + temp_name, "wse", "'" + str(w) + "'", "PYTHON")
arcpy.CalculateField_management(out_folder + temp_name, "z", "'" + str(z) + "'", "PYTHON")
arcpy.TableToTable_conversion(out_folder + temp_name, out_folder, out_file)
arcpy.Delete_management(out_folder + temp_name)
def computeplane(wsedepth):
wse = wsedepth[0]
depth = wsedepth[1]
total = bathymetry < depth
littoral = total & ((wse - bathymetry) < 2)
surface = total & ~(littoral) & ((total + wse - depth) < (total + 2))
profundal = total & ((total + wse - depth) > (total + 5))
subsurface = total & ~(profundal | surface | littoral)
# zonal statistics table names
total_name = 'total_w' + str(wse) + '_z' + str(depth) + '.csv'
littoral_name = 'littoral_w' + str(wse) + '_z' + str(depth) + '.csv'
surface_name = 'surface_w' + str(wse) + '_z' + str(depth) + '.csv'
subsurface_name = 'subsurface_w' + str(wse) + '_z' + str(depth) + '.csv'
profundal_name = 'profundal_w' + str(wse) + '_z' + str(depth) + '.csv'
# compute zonal statistics
processtable(total, zones, wse, depth, table_folder, total_name)
processtable(littoral, zones, wse, depth, table_folder, littoral_name)
processtable(surface, zones, wse, depth, table_folder, surface_name)
processtable(profundal, zones, wse, depth, table_folder, profundal_name)
processtable(subsurface, zones, wse, depth, table_folder, subsurface_name)
def main():
watersurface = numpy.arange(-15.8, 2.7, 0.1)
# take small subset of the tuples: watersurface[33:34]
wsedepths = [(watersurface[x], watersurface[y]) for x in range(watersurface.size)[33:34] for y in range(watersurface[0:x+1].size)]
pool = multiprocessing.Pool()
pool.map(computeplane, wsedepths)
pool.close()
pool.join()
if __name__ == '__main__':
main()
更新
更多调查表明,这与其说是multiprocessing 问题,不如说是 ArcGIS 进行栅格处理的方式的问题。栅格代数结果被写入默认工作空间中的文件;就我而言,我没有指定文件夹,所以arcpy 正在将栅格写入某个 AppData 文件夹。 ArcGIS 根据您的代数表达式使用基本名称,例如 Lessth、Lessth_1 等。由于我没有指定工作区,所有multiprocessing 线程都在写入此文件夹。虽然单个 arcpy 进程可以跟踪名称,但多个进程都尝试写入相同的栅格名称并撞到其他进程的锁。
我尝试在computeplane开头创建一个随机工作区(文件gdb),然后在最后删除它,但是arcpy通常不会及时释放它的锁并且进程在删除时崩溃陈述。所以我不确定如何继续。
【问题讨论】:
标签: python python-multiprocessing arcpy