Masthead

Flavors of ArcGIS 10 Functions

1. ArcToolbox Help

You can go to any toolbox operation, open it, and click on the "Help" button to see information on the operation. If you scroll down, you'll see the definition of the Python function for the operation and the parameters you can send to the function. This is your main source of information for calling functions in ArcGIS.

If you do this now for the Aspect tool, you'll find a section called Syntax that defines how you can all the Aspect tool, the Parameters its function will take and what its Return Value is.

Examples

Note that there are typically two samples of Python code for calling ArcGIS tools. One is labeled Aspect example 1 (Python window) and the other Aspect example 2 (stand-alone script). The Python window version is for use within ArcMap and their support for Python is very poor. I recommend using the stand-alone versions of their samples and an IDE like Wing for development.

Also, notice that they have changed the way the library is included. When we called Aspect, we called it with acrpy.sa.Aspect. This is a fine approach but you can also make your code a little cleaner by including all the functions that are available within a library.

First, they include arcpy and then they include all of the tools in the sa and then they can access the Aspect function directly:

import arcpy  
from arcpy import env  
from arcpy.sa import *

outAspect = Aspect("elevation")

This is the approach that ArcMap uses in its help and is OK but will sometimes cause problems because you can have name collisions if you include another library that also has a function called Aspect. This is why I tend to use the other approach.

2. Wing Source Assistant

The Wing IDE contains a powerful feature that will help you figure out what functions are available in a library and what the possible parameters are. In a script that already has import arcpy at the top, type "arcpy.". You should see a popup appear when you press the period. This shows the functions that are available for you to call in the arcpy library. Type "sa." and you should see the functions within the Spatial Analyst library. Make sure the Source Assistant is selected in the lower right of the Wing IDE and type "Aspect". You should see documentation appear in the Wing IDE for the Aspect function.

Symbol: arcpy.sa.Aspect
Likely type: callable function Aspect 
def Aspect(in_raster)

Aspect(in_raster)

Derives aspect from a raster surface. The aspect identifies the downslope direction of the 
maximum rate of change in value from each cell to its neighbors.

Arguments: in_raster -- The input surface raster.

Results: out_raster -- Output raster

The Arguments are the same as parameters and define the inputs to the tool.

Using the Source Assistant can greatly speed up your coding and debugging.

3. ArcGIS Desktop Help

If you don't know the name of a toolbox tool, or you're looking for help on a Python function that is not in the toolbox, you can check the "ArcGIS Desktop Help".

  1. Go to the main ArcMap window and select Help -> ArcGIS Desktop Help.
  2. Enter "Buffer" in the search box and click "Ask"
  3. Click on "Buffer (Analysis)"
  4. Scroll down until you see the word "Syntax"

Here you will find a description of the Python function call to execute a buffer transform. This help is a great way to learn about the Python functions that are equivalents to the ArcGIS transform (toolbox functions).

Warning: The names of parameters in the "Help" are not always accurate! Watch for capitalization problems. You can use the Python Window help or the Source Assistant in the Wing IDE to see the exact parameter names.

4. The Python Code Snippets in the Results Window

In ArcGIS Pro, "Results" is now "History" in the Analysis tool bar. In the History panel, you can right click on a tool that was executed and "Copy Python Command". Then, paste the command into Wing.

For ArcMap: One of the features of ArcMap that used to be very useful was being able to copy code snippets from the Results window. Basically, after executing any tool in ArcMap, you can open the Results window, right click on the tool you just executed and Copy as Python Snippet.

As an example, if we execute the Aspect tool in ArcGIS and then copy the Python code we will see:

arcpy.gp.Aspect_sa("Tiny_elevation.img","C:/Users/jg2345/Documents/ArcGIS/Default.gdb/Aspect_img1")

Notice that this looks very different from how we called Aspect from ArcMap and what is documented in their own help. The problem is that ArcMap is using an old version of its Python library called the Global Python object or gp. The parameters may be the same but the calling sequence is not. Also, these function calls may not be available outside ArcMap.

I recommend using the ArcGIS help and the Wing IDE Source Assistant rather than the Python snippets (this is sad because this used to be a great resource).

Additional Resources

Python in ArcGIS Pro

The "Using Python in ArcGIS Desktop 10" tutorial from ESRI starts off great but then gets very detailed very quickly. It also did not show the entire video area for me on IE but did work correctly in FireFox. Also, the transcript gave a postscript error when I tried to open it.

There is some excellent online help from ESRI's website starting at What is Python? Take a look around this help section and the one below it called The ArcPy Package. The both have great information on how to use Python in ArcGIS.

 

© Copyright 2018 HSU - All rights reserved.