Masthead

Introduction to Python in ArcGIS

1. Execute a Toolbox function

The toolbox tools can all be converted into Python scripts. This is a great way to quickly learn Python in ArcGIS.

  1. Load any Shapefile into ArcGIS (you can use the Humboldt County outline if desired)
  2. In the ArcToolbox, go to Analysis Tools -> Proximity -> Buffer
  3. Go ahead and buffer the shapefile you loaded

2. Getting the Python Script

  1. Open the "Results" window by going to the Geoprocessing menu and selecting "Results".
  2. Click on the "plus" sign to see the buffer operation you just completed.
  3. Right click on the buffer operation and select "Copy as Python Code Snippet"

  1. Open the Python window , click after the ">>>", and paste the code snippet into the Python window

You can see that this is not the prettiest way to look at the code. In just a moment, we'll paste the same code into an IDE and it will be much more readable. Go ahead and press <enter>. This will run the buffer operation again but will produce an error because it is using the same name you just use. Now push the <up arrow> key and you should see the "Buffer_analysis" appear with just it's parameters in parenthesis. Go ahead and change the second "parameter", the file path for the output and then press <enter> again. The buffer operation should complete and place a new, buffered layer, into ArcGIS. You just modified and ran your first Python script in ArcGIS!

Warning: Not all of the Python functions available within Arc are available from an external IDE! In the example above "Buffer_analysis()" was used, but this function is only available through "analysis.Buffer()" when outside of the ArcGIS.

Tip: The "up" arrow allows you to access previous entries in the Python Window. You can also copy and paste text into the window to make it easier to execute lines of Python code.

3. 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.

4. 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.

5. Python Window Help

Another way to get help is through the Python window.

  1. In the main ArcGIS window click on the Python Window icon
  2. This will open the Python "IDLE"
Note: This is not a full "Integrated Development Environment" like the Wingware IDE. It is very limited but can give you help on ArcGIS functions and we'll be coming back to it because it is also faster to run Python in this window than outside ArcGIS.

  1. In the IDLE window, type "arcpy" and then a period ("."). You should see a popup window with all the functions available from the arcpy package.
  2. Scroll down in the window until you see "Buffer_analysis" and click on it.
  3. Now type an opening parenthesis next to the "Buffer_analysis". You should see some help text appear on the right of the IDLE window with the definition of the possible parameters you can send into the function.

This is not as helpful as might first appear because there are a huge number of functions, it's easier to find a function through the Toolbox.

The combination of IDLE in ArcGIS and the help you've just seen could be used to write Python programs in ArcGIS. However, this development environment is very limited and makes it hard to create programs and especially to learn to program.

6. Calling ArcGIS from Python in the Wing IDE

Take the code snippet from our "Buffer" tool above a paste it into a new file in the Wing IDE. There are two changes you'll need to make to get this code to run:

  1. Add "import arcpy" at the top of the file
  2. Change the first parameter to be a full file path to the input file.

Run the script and see if it works. You may need to change the output file path as ArcGIS will not overwrite an existing file. Now try changing some of the parameters and see what effect this has.

Additional Resources

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.