开发者

Join .dbf to .shp -Then Calculate Fields Script - ERROR handling, script quits on no join

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",开发者_如何学Go "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]") 


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")


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜