将 .dbf 连接到 .shp - 然后计算字段脚本 - 错误处理,脚本在无连接时退出
我创建了一个简单的脚本来将 .dbf 连接到 .shp,然后计算一些字段。该脚本工作得很好,但如果由于某种原因没有连接,我会收到以下错误,该错误会在尝试工作目录中的其余 .dbf 到 .shp 连接之前关闭脚本。如何告诉脚本忽略未加入的 .shps,并继续处理目录中其余的 .shps?
第 20 行,在 gp.CalculateField_management("parcs", "APN2", "[TAX.PARCEL_ID]") 执行错误:错误 999999:执行函数时出错。使用了无效的 SQL 语句。 使用了无效的 SQL 语句。
以下是脚本:
# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True
# Set the workspace. List all of the folders within
gp.Workspace = "C:\JoinCalculateBatch"
fcs = gp.ListWorkspaces("*","Folder")
for fc in fcs:
print fc
gp.MakeFeatureLayer(fc + "\\Parcels.shp", "parcs")
joinTable = (fc + "\\TAX.dbf")
gp.AddJoin_management("parcs", "APN", joinTable, "PARCEL_NUM")
gp.CalculateField_management("parcs", "APN2", "[TAX.PARCEL_ID]")
gp.CalculateField_management("parcs", "SIT_FULL_S", "[TAX.SITADDRESS]")
I have created a simple script to join a .dbf to a .shp, then calculate a few fields. The script works great, but if for some reason there is no join, I get the following error which shuts down the script before trying the rest of the .dbf to .shp joins in my working directory. How can I tell the script to ignore the .shps that don't join, and keep working on the rest of the .shps in the directory ?
line 20, in
gp.CalculateField_management("parcs", "APN2", "[TAX.PARCEL_ID]")
ExecuteError: ERROR 999999: Error executing function.An invalid SQL statement was used.
An invalid SQL statement was used.
Here is the script:
# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True
# Set the workspace. List all of the folders within
gp.Workspace = "C:\JoinCalculateBatch"
fcs = gp.ListWorkspaces("*","Folder")
for fc in fcs:
print fc
gp.MakeFeatureLayer(fc + "\\Parcels.shp", "parcs")
joinTable = (fc + "\\TAX.dbf")
gp.AddJoin_management("parcs", "APN", joinTable, "PARCEL_NUM")
gp.CalculateField_management("parcs", "APN2", "[TAX.PARCEL_ID]")
gp.CalculateField_management("parcs", "SIT_FULL_S", "[TAX.SITADDRESS]")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试下面的代码,该代码将忽略连接或计算字段函数期间的任何错误,并继续循环的下一次迭代。如果您正在编写“一次性”脚本,您可能会逃脱这一点,但是如果您打算在将来使用该脚本或打算与其他人共享它,您应该弄清楚为什么
[TAX.PARCEL_ID]
参数抛出 SQL 错误并修复它。话虽如此,如果不查看表格和形状文件,就很难确定到底是什么导致了错误。将 Parcels.shp 和 Tax.dbf 拖入 ArcMap、加入它们,然后尝试使用计算字段工具查看参数是否正确可能会对您有所帮助。
您可能想尝试下面的语法,该语法直接来自 文档。
You can try the code below which will simply ignore any error during your Join or Calculating the Field functions and continue to the next iteration of the loop. You may get away with this if you are writing a 'one-off' script, however if you intend the use the script in the future or intend to share it with others you should figure out why the
[TAX.PARCEL_ID]
parameter is throwing the SQL error and fix it.With that being said, without seeing your tables and shapefiles it is hard to identify what exactly is causing your error. It may be helpful for you to pull your Parcels.shp and Tax.dbf into ArcMap, join them, and then try using the Calculate Field tool to see if your parameters are correct.
You may want to try the syntax below, which is coming directly from the docs.
我无法访问带有 ArcGIS 的 Windows 计算机,因此我无法对此进行测试,但一般来说,您 捕获异常并决定如何处理它...例如,
我上面写的正是行不通的。
ExecuteError
不是一个普通的 python 错误,它是一些 ArcGIS 特定的错误模块。您可能需要类似的内容:但是,我对 Arc 不太熟悉,无法在这方面为您提供帮助......
I don't have access to a windows machine with ArcGIS, so I can't test this, but generally speaking, you catch an exception and decide what to do with it... E.g.
Exactly what I wrote above won't work.
ExecuteError
is not a normal python error, it's some ArcGIS specific error module. You'll probably need something like:However, I'm not familiar enough with Arc to be able to help you in that regard...
您可以访问 arcpy (arcGIS 10) 吗?
我知道在 arcGIS 10 中,arcGIS 工具箱中有一个 Join_Field 工具,您可以使用它根据某种类型的唯一 ID 字段将 dbf 的所有字段连接到 shp。
Do you have access to arcpy (arcGIS 10)?
I know in arcGIS 10 there is a Join_Field tool in the arcGIS Toolbox with which you could use to join all of the fields of the dbf to the shp based on a Unique ID field of some type.