Earlier this week I presented a session on using the ArcPY site package to manipulate map objects. I decided to take this approach since the standard geoprocessing through python is quite well known now. It turned out to have been a timely presentation since only yesterday ESRI announced a new online course on the subject http://training.esri.com/acb2000/showdetl.cfm?DID=6&Product_ID=1011
I also demonstrated a tool validator class which isn’t exactly new (it was introduced at 9.3) but which is little used functionality within the model/scripting framework. I realise that whilst the slides will be published there wasn’t much there without the demos. This article is partly to redress that by attaching the scripts I used so that anyone can download them:
https://resource.esriuk.com/wp-content/uploads/ArcPydemos.zip
Note that throughout the demos I chose to mix and match Arcpy and core (or OS module) methods. In many cases these are interchangeable when dealing with disk files.
The first demo centred on adding a feature class as a layer in a map. Much of the processing relies on creating a temporary disk file for the layer and this is then added to the map. I also played with the progressor bar in this, although it is fairly quick so you have to be watching to see it change.
The second demo iterated through the layers in a map and re-pointed these from an FGDB to an ArcSDE geodatabase. It was, in fact, a personal ArcSDE gdb on my machine but the same logic applies for an enterprise geodatabase. This showed the simplicity of looping with python lists and required nested lists to iterate through layers within data frames within the map. It also shows the perverse way in which group layers are handled. When I started the script I imagined that I’d iterate through a group layer if I found one but the demo2 script shows this is not the case. The layers are presented in the order they are in the TOC as if all the + toggles are hit. So the group layer appears in turn and the next layer is the first in the group. Once all the layers in the group layer are accessed the next layer is whatever is in the TOC after the group.
Due to time constraints I didn’t get chance to talk about the error handling. As a result of making the whole thing more “pythonic” it is no longer enough just to output the arcpy.GetMessages(2). Many of the errors are raised as python errors; assertion error, type error etc. and these have to be trapped by using the sys methods and the traceback module to report on these errors. The demos both show a rather crude example of this which has the essential steps to get the python error codes out of a failing script.
The final demo is a simple tool validator class to extract all the feature classes from a FGDB and use the list to populate a value list on a string field. While the example easily shows what could be done it is not especially useful as the field has to be of type String not FeatureClass for a value list to be applied to it. The called script therefore has to establish the workspace in the same way as the tool validator has. Take care writing code in a tool validator, it can be very hard to debug. Also ensure that it is in the correct place, initialise, update or message. InitialiseParameters runs as the tool opens. UpdateParameters runs whenever a parameter is changed by the user. The code must determine whether a parameter that is being “watched” has changed; there is no distinction in the invocation of the class method.
One of the biggest strengths of choosing python is that it opens up a whole range of prebuilt packages to perform tasks. Yesterday I was advising on using python modules to zip files and directories once geoprocessing has completed and investigation revealed that at least 3 (zipfile, gzip and tarfile) are packaged with the default python 2.6 installation.
I hope you find the demo scripts useful, and if you attended the session I hope you enjoyed it. Thanks, Rob