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 ArcGIS 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 ArcGIS 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 ArcGIS 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 History 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 ArcGIS: One of the features of ArcGIS that used to be very useful was being able to copy code snippets from the Results window. Basically, after executing any tool in ArcGIS, 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:

out_raster = arcpy.sa.Aspect(
in_raster="Tiny_elevation.img",
method="PLANAR",
z_unit="METER",
project_geodesic_azimuths="GEODESIC_AZIMUTHS",
analysis_target_device="GPU_THEN_CPU"
)
out_raster.save(r"C:\Users\jg2345\Desktop\GSP 318\Lab 09\TinyDEM\aspect4.img")

Note that if we execute the Aspect tool that is in the 3D Analyst library, the result looks identical but the Python code that we get from the History is different and requires an output raster path rather than returning the raster object. Watch for these and other issues!

arcpy.ddd.Aspect(
in_raster="Tiny_elevation.img",
out_raster=r"C:\Users\jg2345\Desktop\GSP 318\Lab 09\TinyDEM\aspect3",
method="PLANAR",
z_unit="METER",
project_geodesic_azimuths="GEODESIC_AZIMUTHS",
analysis_target_device="GPU_THEN_CPU"
)

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

© Copyright 2018 HSU - All rights reserved.