如何解决ArcPy脚本中CopyRaster工具的ExecuteError:表名无效问题?
解决ArcPy CopyRaster工具的ExecuteError(ERROR 999999)问题
从你的报错信息和代码来看,这个问题主要出在输出栅格的命名格式以及潜在的逻辑漏洞上,我来一步步帮你梳理解决:
1. 核心报错原因:文件地理数据库不支持带扩展名的栅格名称
你的输出位置out_loc是一个文件地理数据库(.gdb),但你在调用CopyRaster_management时,直接拼接了带.tif扩展名的latest_raster文件名。文件地理数据库中的栅格数据集是没有文件扩展名的,带.tif的名称会被识别为无效的表名,这就是报错里“表名无效”的直接诱因。
2. 具体修复步骤
步骤1:移除输出文件名的.tif后缀
在生成输出路径时,需要剥离文件名的扩展名。推荐用os.path.splitext()来处理,同时用os.path.join()代替字符串拼接,避免路径分隔符的兼容问题:
# 提取不带扩展名的文件名 output_raster_name = os.path.splitext(latest_raster)[0] # 拼接正确的输出路径 arcpy.CopyRaster_management(latest_raster, os.path.join(out_loc, output_raster_name), "DEFAULTS","","","","","")
步骤2:修正latest_edition的逻辑漏洞
当前代码中,latest_edition在循环外初始化,当遍历第二个索引(比如css)时,如果它的版本号比之前aqi的版本小,latest_raster会保留aqi的结果——这大概率不是你想要的(你应该是想为每个索引单独复制最新版本的栅格)。需要把latest_edition和latest_raster的初始化放到循环内部:
for ind in index_list: latest_edition = 0 # 每个索引单独初始化版本变量 latest_raster = "" search_string = ind + "*_i.tif" print(search_string) raster_list = arcpy.ListRasters(search_string,"ALL") for i, obj in enumerate(raster_list): # 这里的切片依赖文件名格式,确保obj是类似aqi_19_q4_i.tif的结构 edition = int(obj[4:6] + obj[8:9]) if edition > latest_edition: latest_edition = edition print(latest_edition) latest_raster = obj # 确保找到有效栅格后再执行复制 if latest_raster: output_raster_name = os.path.splitext(latest_raster)[0] arcpy.CopyRaster_management(latest_raster, os.path.join(out_loc, output_raster_name), "DEFAULTS","","","","","")
步骤3:额外的稳定性检查
- 可以先打印
raster_list,确认ArcPy确实获取到了目标路径下的栅格文件; - 检查输出地理数据库是否存在,若不存在可以提前创建:
if not arcpy.Exists(out_loc): gdb_dir = os.path.dirname(out_loc) gdb_name = os.path.basename(out_loc) arcpy.CreateFileGDB_management(gdb_dir, gdb_name)
修改后的完整代码
# 导入系统模块 import arcpy import os # 检出ArcGIS Spatial Analyst扩展许可 arcpy.CheckOutExtension("Spatial") from arcpy.sa import * # 设置输入和输出路径 in_loc = "S://gisdata/maintained/software_versions" print(in_loc) out_loc = "S://gisdata/maintained/current/Platform_indices_test.gdb" print(out_loc) # 检查输出GDB是否存在,不存在则创建 if not arcpy.Exists(out_loc): gdb_dir = os.path.dirname(out_loc) gdb_name = os.path.basename(out_loc) arcpy.CreateFileGDB_management(gdb_dir, gdb_name) # 设置工作空间 arcpy.env.workspace = in_loc index_list =['aqi', 'css'] for ind in index_list: latest_edition = 0 latest_raster = "" search_string = ind + "*_i.tif" print(search_string) raster_list = arcpy.ListRasters(search_string,"ALL") # 检查是否找到匹配的栅格 if not raster_list: print(f"未找到符合条件的栅格:{search_string}") continue for i, obj in enumerate(raster_list): # 增加版本号提取的异常处理,避免文件名格式不符导致报错 try: edition = int(obj[4:6] + obj[8:9]) except ValueError: print(f"文件名格式不符合预期:{obj},跳过该文件") continue if edition > latest_edition: latest_edition = edition print(f"找到最新版本:{latest_edition}") latest_raster = obj # 执行复制操作 if latest_raster: output_raster_name = os.path.splitext(latest_raster)[0] output_path = os.path.join(out_loc, output_raster_name) print(f"正在复制栅格到:{output_path}") arcpy.CopyRaster_management(latest_raster, output_path, "DEFAULTS","","","","","") else: print(f"未找到{ind}的有效栅格文件")
内容的提问来源于stack exchange,提问作者ChristineF
相关产品推荐
相关产品推荐

