Why does python suck?: Discontinuity between codes and tools

2015
24
Jump to solution
03-01-2012 09:04 AM
RichardThurau
Occasional Contributor
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 arcpy.sa 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. trees06.save("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.

Thanks!

Rich
Tags (2)
0 Kudos
24 Replies
curtvprice
MVP Esteemed Contributor
Yes, I actually ended using the Append command.


Good deal.

arcpy object, like an attribute table, into an R object like a matrix, table, or dataframe. This is the missing link for me, since I feel pretty comfortable with R, but not so much with Python. It would be awesome to take a list of attribute tables, move them to R, manipulate the tables, then save them back as attribute tables again.


Python has a nice standard library (csv) that may be the easiest way to move data back and forth. DBase files may also a way to go, as I'm sure R has good utilities to read and write .dbf, but it as you may have experienced it has a lot of limitations and gotchas. The nice thing about Python (and R) is you can build a function, get it working, and then easily reuse it.
0 Kudos
StacyRendall1
Occasional Contributor III
Richard,

I think you need to look into search cursors and update cursors. I use these all the time to extract data from Arc objects, such as attribute tables, do stuff, then write back to the same/a new table... I would like to get something up on my blog about this kind of stuff, but it won't be for a little while yet...
0 Kudos
RichardThurau
Occasional Contributor
Latest example/installment:

Trying to use a list to loop through simple tools. First, I tried this list in project features. The script failed. So, I opened the "Batch Project" tool, dropped the same list into the tool, and it ran without an issue.

My example here, is trying to send the same list to a Feature to Raster command:

LcFcList= arcpy.ListFeatureClasses("*1_utm", "")
print LcFcList
work3=arcpy.env.workspace=r"X:\DATA\Raster_LC.gdb"
for fc in LcFcList:
    print str(fc)
    arcpy.FeatureToRaster_conversion(fc, "Ras", (work3 + "\\" + os.path.basename(fc) + "_ras"), 1)


Output failure:
[u'Water1_utm', u'BldgPly1_utm', u'RoadPly1_utm', u'SidewalkPly1_utm', u'Parking_Lots1_utm', u'Wetlands1_utm', u'DC_Boundary_100m_Buf1_utm', u'Mall_Veg1_utm', u'GC_Fairways1_utm', u'RecFields1_utm', u'Trees_Merged_4_10_1_utm', u'IA_Final_4_3_12_small_remove_1_utm', u'Final_Veg_Soil_4_3_12_1_utm', u'Small_trees_1_utm']
Water1_utm

Traceback (most recent call last):
  File "X:\DATA\LC_Create_040912.py", line 109, in <module>
    arcpy.FeatureToRaster_conversion(fc, "Ras", (work3 + "\\" + os.path.basename(fc) + "_ras"), 1)    ### 1 Meter Processing Cell Size
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\conversion.py", line 1686, in FeatureToRaster
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input features: Water1_utm does not exist.
ERROR 001000: Field: Field Ras does not exist
Failed to execute (FeatureToRaster).


The list looks great, all my layers are there. When the first layer comes up "Water1_utm", I get the error message above. I tried skipping the first layer, same error. I would bet lunch that I can run each of the layers in my list trough the conversion tool without any issues.

So, my diplomatic, respectful of Python question is: What am I doing wrong? Is it in the output file command? The error makes it seem it's from the input.

Any thoughts?

Thanks

Rich
0 Kudos
curtvprice
MVP Esteemed Contributor
So, my diplomatic, respectful of Python question is: What am I doing wrong? Is it in the output file command? The error makes it seem it's from the input.


Your script is changing arcpy.env.workspace to the output location and your feature classes are not there -- they are in the workspace set when you ran arcpy.listFeatureClasses().  Don't change the workspace and your fc's should be found fine, since you are specifying the full output path, you should be good to go!

Or, specify the full paths for the input feature classes:

inFC = os.path.join(inWS,fc)
0 Kudos
RichardThurau
Occasional Contributor
Curtis,
Thank you. I guess this threw me since Python actually prints the list, and the individual dataset. It was the same issue for the projections, since I was creating a list from one workspace, the changing the workspace for the output, but using a list built on the previous workspace. Seems to me it shouldn't matter, but I am not a programmer...enough of one I guess.

I do enjoy learning however. Thanks for that.

Rich
0 Kudos