Author Topic: Python Programming Tips for ArcMap  (Read 7341 times)


  • GIS Support Team
  • Isis (Extreme Power Member)
  • *****
  • Posts: 1217
    • Email
Python Programming Tips for ArcMap
« on: November 15, 2013, 03:47:38 PM »
Python Tips for ArcMap 10.1 and higher

There are several methods to learn python for ArcMap. It will take tinkering and frustration to learn but I am a huge fan of using Python in ArcMap. Originally Python scripts could only do geoprocessing tasks but with the new python add-in you can now interact with the user. So the two main flavors or Python in ArcMap are:

(1) Python "geoprocessing" scripts (access to ~850 geoprocessing tools). These are written generally to automate common GIS tasks (e.g. do 5 things on one layer or 1 thing on 20 layers). This generally is limited to selecting a file or a directory of files (vector or raster) and doing "something" using parameters defined by the user. For example, take 10 input layers and one clip layer and clip all 10 input layers. These scripts are again not able to easily use the existing selected set from the data frame or allow the user to draw or point at features. And remember any single tool in ArcMap can be used in a batch mode to process many images.
free training:
Intro book (not great but good):

One of the best method to write these are to build a Model (and export as Python):
zonal example:

looping in models can be done using iterators

For an example of iteration, see attached toolbox (must be logged in). It is easy to loop in python but looping in a model can be confusing at first. This example Model takes a bunch of shapfiles and creates a new field and calculates the name of the file into the field. Once it has processed all the files (inside the iterated submodel), it "merges" them (called the collection) into one output shapefile. This allows the original filename to be maintained in the merged file.

(2) Python add-ins finally allow python scripts to interact with the user (generally as a tool from the toolbar). When run, you can get at the current selected set or ask the user for input (e.g.draw a line or click on something) and then do something with the results.  Prior to this you had to use VBA (depricated) or VB.NET and C++, C# to do this.

free training:

A good interactive example we support is here (asks the user to draw line):

Another which takes the selected set and does something (also available below as a python toolbox):

Tip: When downloading Python add-ins, simply rename to *.zip and extract to a folder. The Python code is available in the add-in file.

(3) I know I said only two but there are Python Toolboxes too:
This page has a confusing example. It actually uses a "field calculator script"inside the Python script. As an aside, if you didn't know, very simple Python scripts can be used in the Field Calculator and this shows how to use that programmatically. It is just not a good general example for a Python Toolbox. BTW, a good example Python Field Calculator script is


Here is simple Python toolbox we created (same function as above):

have fun,

more resources:
more training (not all free):
« Last Edit: November 17, 2013, 10:54:27 AM by thare »