Monday, August 15, 2016

Checklist for updating from ArcGISScripting to ArcPy

Checklist for updating from ArcGISScripting to ArcPy

All 9.3 python scripts starts with the import arcgisscripting command and are full of gp statements.  And they are still useful scripts, I have spent some time upgrading them to the new ArcPy site-package for ArcMap version 10.x

I took basic checklist from The GIS Studio  and I felt it is very helpful for me and I included some more points which I feel important.

1. Replace the import statement and remove the geoprocessing creation statement. In other words, remove this:
import arcgisscripting
gp = arcgisscripting() (or) gp=arcgisscripting.create(9.3)
gp.toolbox = "management" (or) gp.AddToolBox("C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcToolbox\Toolboxes\Data Management Tools.tbx")
              

and replace it with

import arcpy
No need to specify any toolboxes in v10!

2. Replace all gp. with arcpy.  I do this with a find/replace command in my code editor. For example
gp.exists()
now becomes
arcpy.Exists()

3. Check your capitalization.  Previous versions of arcgisscripting allowed for sloppy capitalization.  Now it has to be exact and this can be a real pain to find all the mismatched cases. gp.addmessage worked before but now it has to be arcpy.AddMessage.  And note the capital “Exists” in the example above.  Now that I’ve been working with arcpy a bit I find my eye is trained, however in the beginning this was a common bug to find in my scripts and repair.
Some other examples
gp.searchcursor()  now arcpy.SearchCursor()
gp.describe() now arcpy.Describe()

4. The result tool is different.  Results were pretty straightforward up until ArcMap 9.3.  At that point they became a little more tricky.
Now, many geoprocessing tools return a result object of the derived output dataset. A print statement will display the string representation of the output.
>>result = arcpy.GetCount_management("myLayer")
>>result
<Result '879'>
>>print result
879
The result object’s getOutput method returns values as a unicode string. To convert to a different Python type, use  built-in Python functions: str(), int(), long(), float().
>>resultValue = result.getOutput(0)
>>resultValue
u'879'
>>count = int(resultValue)
>>print count
879
In your code you can smash it all together to look like this and save a few lines.  Although this makes your code less readable by other novice users that might need or want to modify the script.
>>ItemCount=int(arcpy.GetCount_management("myLayer").getOutput(0))
>>ItemCount
879

6. Replace any del gp statements.  I had been taught to always delete the gp module any time you ended the script either at the very end or in an try/except statement.  I was told that it cleaned up any memory usage.  I don’t know how true it is, but what can it hurt, right?   So my scripts are littered with
del gp
statements.  Now, if you’ve never created the geoprocessor in the first place (see step 1), these are little bombs waiting to go off when your script runs into them.  You can’t delete what doesn’t exist.  Be sure to find them all and replace them with a
del arcpy

7. The overwrite tool is different.  You’ll need to find and update all uses of it that you might have. Previously you had something like this:
gp.overwrite = 1
where 1 is true and 0 is false.  Now you can do something like:
arcpy.env.overwriteOutput = True
or even
from arcpy import env
env.overwriteOutput = True

8.  Import the env module from arcpy.  This isn’t a requirement, however I tend to use the environment settings fairly often in my scripts so implicitly importing the env module saves a bit of typing. In the example above you can use the shorter env.overwriteOutput = true statement.  Additionally, another common one that I use is the workspace setting. By importing the env module you can then use a short
env.workspace = 'c:/my/path'
statement.  If you go this route be sure to remember to put
from arcpy import env
up near the top of your scripts with the other import statements in order for these shorter statements to work.

9. Tool name suffixed with toolbox name. Below SelectLayerByAttribute tool exists in Data Management tools. In arcpy it’s suffixed with toolbox (_management) name. Its alias name of Data Management tools.
gp.SelectLayerByAttribute()
now become
arcpy.SelectLayerByAttribute_management()


That’s it! At least for now.  If I find I run into some more common things to remember I’ll update this posting. And please let me know if you have run into any tips while updating your scripts in the comments below.

No comments:

Post a Comment