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

thare

  • GIS Support Team
  • Isis (Extreme Power Member)
  • *****
  • Posts: 1217
    • http://webgis.wr.usgs.gov
    • 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: http://training.esri.com/gateway/index.cfm?fa=catalog.webCourseDetail&courseID=1868
Intro book (not great but good): http://amzn.to/1dAiMn7

One of the best method to write these are to build a Model (and export as Python): http://resources.arcgis.com/en/help/main/10.1/index.html#//002w00000001000000
video: http://www.youtube.com/watch?v=lQFBuhtZD6M
zonal example: http://www.youtube.com/watch?v=89I_r3n23YY

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.
overview: http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000025000000

free training: http://training.esri.com/gateway/index.cfm?fa=catalog.webCourseDetail&courseID=2485
video: http://www.youtube.com/watch?v=B6zbzPOnYYQ

A good interactive example we support is here (asks the user to draw line):
http://www.arcgis.com/home/item.html?id=30625d7ae1094b089c90a1a0525fc823

Another which takes the selected set and does something (also available below as a python toolbox):
http://www.arcgis.com/home/item.html?id=590c3c9dc67c4214a3725cb2be67a97a

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:
http://resources.arcgis.com/en/help/main/10.1/index.html#//001500000022000000
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 http://support.esri.com/en/knowledgebase/techarticles/detail/38517

video: http://www.youtube.com/watch?v=B6zbzPOnYYQ&list=PLmCMYvFOcqc5c36xY-VIzNtmAOXzWEX3W

Here is simple Python toolbox we created (same function as above):
http://www.arcgis.com/home/item.html?id=9fb418623828407095aff32e551c4a31


have fun,
Trent

more resources:
http://blogs.esri.com/esri/supportcenter/2012/01/12/10-easy-ways-to-tame-python-scripting-in-arcgis/
more training (not all free): http://training.esri.com/gateway/index.cfm?fa=search.results&searchterm=python
http://www.arcgis.com/home/item.html?id=666adb14c58c41129a4c1034b8277be8
« Last Edit: November 17, 2013, 10:54:27 AM by thare »