GIS Logo GSP 118 (318): GIS Programming

Calling ArcGIS from Python

In Review

1. Introduction to Calling ArcGIS Functions

If we are going to have scripts of any level of complexity we are going to have to get out of the Python Window within ArcGIS and into a more complete development environment. The following script will create an "aspect" raster from an existing raster (GRID). The aspect tool replaces each pixel of a DEM with a value indicating the direction that pixel is facing. We're showing aspect because it is one of the simplest processes. You can use the same example code if you download a DEM from the "Data" web page at left or use the HSU data hub. To have this run somewhat fast, you'll want to use a relatively small DEM.

import arcpy

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Path to the data (you can also set a workspace instead), change this to point to your data
InputPath="C:/ProjectsPython/"
# run the aspect tool
OutRaster=arcpy.sa.Aspect(InputPath+"OregonDEM.img") # save to a TIFF file (try and GRID by removing the extention but it may not work)
OutRaster.save(InputPath+"Aspect.img") # Print out something so we know we've finished print("Done")

The script above will work but it is missing some critical error checking. In the code below we've added "try" and "catch" blocks. These are part of the error, or "exception", handling in Python and it similar to the error handling in Java and JavaScript. The inportant point is that if any of the code within the "try" block generates an error, then the code in the "catch" block will be called. This makes error handling very easy as you don't have to worry about checking errors in every function, you do need to check errors in your "top-level" script. Try the following:

try:
    import arcpy

    # Check out the ArcGIS Spatial Analyst extension license
    arcpy.CheckOutExtension("Spatial")

    # Path to the data (you can also set a workspace instead), change this to point to your data
    InputPath="C:/ProjectsPython/"

    # run the aspect tool
    OutRaster=arcpy.sa.Aspect(InputPath+"OregonDEM.img")

    # save to a TIFF file (try and GRID by removing the extention but it may not work)
    OutRaster.save(InputPath+"Aspect.img")

    print("Finished without an error")
    
except:
    print("An error occurred")
    print arcpy.GetMessages()    

You can make the error handling as complex or as simple as your target users (who will be using the script) need. If only you are using the script then the code above is probably fine. However, if you are going to make this a tool that is distributed to others, you'll probably want to add additional "try-except" blocks to check where the error occurred. The code above could fail on the "import" if ArcGIS is not installed, on the "CheckOutExtension" call if the proper license is not avaiable, on the "Aspect" call if the file is not available, and finally on the "save" if the file cannot be written. We'll touch on error checking like this again when we look at user interfaces.

Now be a good time to try additional functions to make sure you're comforatble with them. Try creating a "slope" raster and even a "hillshade". Then you'll be able to write complete script for processing a single rasters. Now lets see how to process a lot, or "batch", of rasters.

2. Calling Any ArcGIS Function

In the previous section, we learned how to obtain the definition of functions within ArcGIS. The problem is that we have to do some conversion of these functions to access them from an external IDE. First, the function definitions in the code snippets may have different function names outside ArcGIS.

In the previous section we use "Buffer_analysis()" inside of ArcGIS but the function that is available outside of ArcGIS is called with "analysis.Buffer()". You can find out exactly what functions are available by using the "Source Assisant" in the Wing IDE. First make sure you import "arcpy". Then type "arcpy." so see the packages that are avialable within ArcGIS. "analysis" is the package that cooresponds to the toolbox called "Analaysis" in ArcGIS. Enter "arcpy.analysis." to see the functions that are available in the analysis package. Then type "acrpy.analysis.Buffer" to see the exact function definition in the Source Assistant.

When we are running tools within ArcGIS you can access the layers that are currently loaded into ArcGIS. This is why your code snippets may only contain a file name. There is a workspace defined within ArcGIS and you may only see partial filenames for files that are within the workspace. We can also set the workspace outside of ArcGIS but many times we will want to access files in different folders and even on different disks. The most general approach is to change all of the paths that are specified in code snippets to full pages (i.e. they start with a drive letter).

In summary, to use a function that is in the ArcGIS help in an external IDE:

  • Change the name: an examle would be "Buffer_analysis" -> analysis.Buffer
  • Change the layer names to file paths, make sure the slashes are "\\" or "/"