Masthead

Calling ArcGIS from Python

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.

Here are some sample DEMs to try with the code below:

import arcpy

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

# This allows us to run the script repeatedly without deleting the intermdiate files
arcpy.env.overwriteOutput=True 

# Path to the data (you can also set a workspace instead), change this to point to your data
InputPath="C:/Temp/"
# run the aspect tool
OutRaster=arcpy.sa.Aspect(InputPath+"Temp.img") # save to an IMAGINE (img) file
OutRaster.save(InputPath+"Aspect.img") # Print out something so we know we've finished print("Done")

Notice in the script above that we call the Aspect tool by using arcpy.sa.Aspect(...). This is because the Aspect tool is inside the Spatial Analyist toolbox.

Now be a good time to try additional functions to make sure you're comfortable 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.

© Copyright 2018 HSU - All rights reserved.