Why does python suck?: Discontinuity between codes and tools

2014
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
1 Solution

Accepted Solutions
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

View solution in original post

0 Kudos
24 Replies
ChrisMathers
Occasional Contributor III
Run the Tabulate Area tool from ArcMap and in the results window right click and choose "Copy as python snippit." This will give you "perfect" syntax.
0 Kudos
RichardThurau
Occasional Contributor
All the code here has been used successfully in other cases. Sometimes success comes from the Fail, save, restart, Pass cycle.
I haven't used the snippet option very often.

I wonder why the snippet code looks so different than the way Help describes how to write? For example:

Snippet:
arcpy.Reclassify_sa(Lc06, "Value","0 NODATA;1 10;2 NODATA;3 NODATA;4 NODATA;5 NODATA;6 NODATA;7 NODATA", trees06,"DATA")


Code written from Help:
trees06 = Reclassify(Lc06, "Value", RemapValue([[0, "NODATA"], [1, 10], [2, "NODATA"], [3,"NODATA"], [4,"NODATA"], [5,"NODATA"],[6,"NODATA"], [7,"NODATA"]])) 
trees06.save("X:\\DATA\\Working\\trees_06")


The snippet code fails, btw: "AttributeError: 'module' object has no attribute 'Reclassify_sa'" - I guess that's why the Help articles are there.

I'll try using the suggestion in other cases though.

Thanks

Rich
0 Kudos
DanPatterson_Retired
MVP Emeritus
within the help file code there is one salient line

from arcpy.sa import *

was that included in your failed script?  Otherwise you may have to checkoutExtension if you aren't using an ArcInfo license
0 Kudos
curtvprice
MVP Esteemed Contributor
I wonder why the snippet code looks so different than the way Help describes how to write?


The snippet example used the string representation of the argument (this is what is returned BTW by getParameterAsText()), the example in the help uses python structures like lists. Both argument formats are supported.

In the tool and modelbuilder, the GUI usally handles all this text-object conversion for you -- when you're scripting you need to be very careful how you format arguments so the tool will see the correct object representation.

The snippet code fails, btw: "AttributeError: 'module' object has no attribute 'Reclassify_sa'"  


I think this may be a bug in the copy as python snippet - as this syntax worked with 9.3 but won't work with 10.0. Most spatial analyst tools must be accessed using" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/... Python map algebra in 10.0:

arcpy.CheckOutExtension("spatial")
outgrid = arcpy.sa.Reclassify(...)


or

arcpy.CheckOutExtension("spatial")
from arcpy.sa import *
...
outgrid = Reclassify(..,)



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.


IMHO modelbuilder is a great macro tool for your work, but when you need to build a tool that needs full validation and documentation and error messages, python is the way to go.
0 Kudos
RichardThurau
Occasional Contributor
Yes Dan. I just commented out the reclassify line from the code I posted above and tried the snippet. arcpy.CheckOutExtension("Spatial") is listed too.

Sounds like it is possible to write a reclassify in such a way? Seems to make more sense than the Help method, having to declare RemapRange or RemapValue and then saving the output.

Thanks

RT
0 Kudos
MikeMacRae
Occasional Contributor III
I was having the same issue a week or so ago. One of the tests I ran was to print out my parameters which included path names. I had something similar to your path for the .img in that I had a folder called '2009' which contains integer values. I was getting weird print lines where the folder was, so I could tell that the path wasn't being read properly. I changed all my pathway parameters to raw string and they printed out fine, so in your case:

Lc06 = r"X:\DATA\C_2006\LandCover_2006.img"


I think when I set it up as a parameter, I set it to arcpy.GetparameterAsText() it was doing the raw string conversion for me, whereas you have to set it in idle. When I ran from idle, it crapped out until I changed to raw. This might be your issue. You might want to change the rest of your parameters to raw string as well.

Cheers,
Mike
0 Kudos
RichardThurau
Occasional Contributor
Everyone,
Honestly, I was pretty disgruntled when I started this post, but thanks to some great offerings I feel like I've learned a bit about what's happening. I'll be applying your suggestions a bit later today and report on the result.

For now, I just want to say Thanks for all your input!

Rich
0 Kudos
StacyRendall1
Occasional Contributor III
Richard,

It's not Python that you should be angry at - it's Arcpy (i.e. the 'site-package' developed by ESRI that lets you use Python to access Arc functionality). Usually with programming languages if something goes wrong, it's the programmers fault (for making silly mistakes, etc.). However with Arcpy there are continual issues with data locks, odd/variable syntax, inconsistent function name capitalization, etc., etc., etc. These make it particularly hard to learn, and the documentation is not always the greatest...
curtvprice
MVP Esteemed Contributor
I think this may be a bug in the copy as python snippet.


I contacted Esri Support and, sure enough:

You are correct the Copy As Python Snippet for the Con tool is not generating the correct syntax. I have submitted a software bug report on your behalf for this issue. The reference number and subject line for this bug is as follows:
[#NIM078805 The Spatial Analyst > Conditional > Con Python snippet from Results window uses incorrect syntax.]
0 Kudos