AnsweredAssumed Answered

Why does python suck?: Discontinuity between codes and tools

Question asked by rgthurau on Mar 1, 2012
Latest reply on Apr 11, 2012 by rgthurau
Hi Community:
Why is it certain tasks fail in python, but plug the same data, same parameters into a tool, and it runs without issue? I'm partially looking for answers to some specific questions in this post, and partially looking for developers to improve this product.

I'm trying to find some answers on this code I wrote this yesterday to do some basic tabulations.
I've commented to the right where in this code, python crashed.
In the essence of trying to find all the answers, I'll keep my code as messy as I wrote it (although I shortened pathnames-which might be an issue sometimes?):

### System Tools import arcpy, os from import * from arcpy import env, sa arcpy.CheckOutExtension("Spatial")  ### Declarations------------------------------- arcpy.env.overwriteOutput = True ### Definitions------------------------------------- ## areaTab1 = "X:\\DATA\\Temp.gdb\\trees11_ward_tab" areaTab2 = "X:\\DATA\\Temp.gdb\\trees06_ward_tab"  geog1_sub= "X:\\DATA\\Target_Geog.gdb\\WardPly" geog1= "X:\\DATA\\Temp.gdb\\WardPly_utm" cs= "C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\NAD 1983\\NAD 1983 UTM Zone 18N.prj"  Lc06 = "X:\\DATA\\C_2006\\LandCover_2006.img" trees06 = "X:\\DATA\\trees_06"  trees_in= "X:\\DATA\\Tree.gdb\\Tree_merged_022912" trees_vec= "X:\\DATA\\Tree.gdb\\Tree_merged_022912_1" trees_ras =  "X:\\DATA\\Tree.gdb\\Tree_raster_022912"   ##Processes trees06 = Reclassify(Lc06, "Value", RemapValue([[0, "NODATA"], [1, 10], [2, "NODATA"], [3,"NODATA"], [4,"NODATA"], [5,"NODATA"],[6,"NODATA"], [7,"NODATA"]])) ##FAILED: Assuming code doesn't like .img- But, ran perfectly using Reclassify tool."X:\\DATA\\Working\\trees_06")   arcpy.Project_management(geog1_sub, geog1, cs) # Ran successfully arcpy.FeatureClassToFeatureClass_conversion(trees_in, os.path.dirname(str(trees_in)), trees_vec) ## Making a backup copy of data: Failed. Ran in python after adding actual pathname. arcpy.AddField_management(trees_vec, "Ras", "SHORT") #Success arcpy.CalculateField_management(trees_vec, "Ras", 1) # Success arcpy.FeatureToRaster_conversion(trees_vec, "Ras", trees_ras, 1) # Success  TabulateArea(geog1, "OBJECTID", trees_in, "Value", areaTab1, 1) #FAILED: "Field OBJECTID does not exist". Um, yes it does, and tool ran fine using exact parameters from Tabulate Area tool. print "Tab 1 done" TabulateArea(geog1, "OBJECTID", trees06, "Value", areaTab2, 1)#FAILED: "Field OBJECTID does not exist". Um, yes it does, and tool ran fine using exact parameters from Tabulate Area tool. ## arcpy.JoinField_management(geog1, "OBJECTID", areaTab1, "OBJECTID", "VALUE_1") #This line failed first time through, then passed after restart. (?) arcpy.JoinField_management(geog1, "OBJECTID", areaTab2, "OBJECTID", "VALUE_10")# Succes ## FieldList = ["UTC11_Acres", "UTC11_Pct", "UTC06_Acres", "UTC06_Pct", "Chng_Acres", "Chng_Pct"] ## for field in FieldList:     arcpy.AddField_management(geog1, field, "DOUBLE")  rows = arcpy.UpdateCursor(geog1) for row in rows:     row.UTC11_Acres = (row.VALUE_1 * 0.0002471054)     rows.updateRow(row)     row.UTC11_Pct = (row.VALUE_1 / row.Shape_Area) * 100     rows.updateRow(row)     row.UTC06_Acres = (row.VALUE_10 * 0.0002471054)     rows.updateRow(row)     row.UTC06_Pct = (row.VALUE_10 / row.Shape_Area) * 100     rows.updateRow(row)     row.Chng_Acres = (row.UTC11_Acres - row.UTC06_Acres)     rows.updateRow(row)     row.Chng_Pct = (row.UTC11_Pct - row.UTC06_Pct)     rows.updateRow(row)  arcpy.DeleteField_management(geog1, ["VALUE_1", "VALUE_10"])

Most of the issues I'm having and have had in the past are dealing with rasters. I've received some good feedback on how to deal with some issues, but are there things I can be doing differently to ease my woes?

I'm trying to convince my boss that python is a lot more efficient than clicking or modeling in ModelBuilder. It's a hard sell when i have to restart a script 8 times to get it to run.

General comments and specific resolutions are much appreciated.