将 .dbf 连接到 .shp - 然后计算字段脚本 - 错误处理,脚本在无连接时退出

发布于 2024-12-09 04:10:31 字数 909 浏览 3 评论 0原文

我创建了一个简单的脚本来将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も让我眼熟你 2024-12-16 04:10:31

您可以尝试下面的代码,该代码将忽略连接或计算字段函数期间的任何错误,并继续循环的下一次迭代。如果您正在编写“一次性”脚本,您可能会逃脱这一点,但是如果您打算在将来使用该脚本或打算与其他人共享它,您应该弄清楚为什么 [TAX.PARCEL_ID] 参数抛出 SQL 错误并修复它。

话虽如此,如果不查看表格和形状文件,就很难确定到底是什么导致了错误。将 Parcels.shp 和 Tax.dbf 拖入 ArcMap、加入它们,然后尝试使用计算字段工具查看参数是否正确可能会对您有所帮助。

for fc in fcs:
    print fc
    gp.MakeFeatureLayer(fc + "\\Parcels.shp", "parcs")  
    joinTable = (fc + "\\TAX.dbf")

    try:
        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]")
    except:
        print 'Join or Calculate Field did not work for %s.' % (fc)

您可能想尝试下面的语法,该语法直接来自 文档

gp.CalculateField_management("parcs", "APN2", "!TAX.PARCEL_ID!", "PYTHON")

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.

for fc in fcs:
    print fc
    gp.MakeFeatureLayer(fc + "\\Parcels.shp", "parcs")  
    joinTable = (fc + "\\TAX.dbf")

    try:
        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]")
    except:
        print 'Join or Calculate Field did not work for %s.' % (fc)

You may want to try the syntax below, which is coming directly from the docs.

gp.CalculateField_management("parcs", "APN2", "!TAX.PARCEL_ID!", "PYTHON")
如果没有你 2024-12-16 04:10:31

我无法访问带有 ArcGIS 的 Windows 计算机,因此我无法对此进行测试,但一般来说,您 捕获异常并决定如何处理它...例如,

# 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")
    try:
        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]") 
    except ExecuteError:
        print 'Could not join', fc

我上面写的正是行不通的。 ExecuteError 不是一个普通的 python 错误,它是一些 ArcGIS 特定的错误模块。您可能需要类似的内容:

except gp.ExecuteError:
    ...

但是,我对 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.

# 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")
    try:
        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]") 
    except ExecuteError:
        print 'Could not join', fc

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:

except gp.ExecuteError:
    ...

However, I'm not familiar enough with Arc to be able to help you in that regard...

雪化雨蝶 2024-12-16 04:10:31

您可以访问 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文