Introduction to Python in ArcGIS
In Review
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.
- Load any Shapefile into ArcGIS (you can use the Oregon Dams Geodatabase if desired at: Oregon Dams Geodatabase)
- In the ArcToolbox, go to Analysis Tools -> Proximity -> Buffer
- Go ahead and buffer the shapefile you loaded
2. Getting the Python Script
- Open the "Results" window by going to the Geoprocessing menu and selecting "Results".
- Click on the "plus" sign to see the buffer operation you just completed.
- Right click on the buffer operation and select "Copy as Python Code Snippet"
- 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".
- Go to the main ArcMap window and select Help -> ArcGIS Desktop Help.
- Enter "Buffer" in the search box and click "Ask"
- Click on "Buffer (Analysis)"
- 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).
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. We'll be coming back to ArcGIS a little later, for right now we're going to move into the Wing IDE and learn to program in Python very quickly.
5. Python Window Help
Another way to get help is through the Python window.
- In the main ArcGIS window click on the Python Window icon
- This will open the Python "IDLE"
- 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.
- Scroll down in the window until you see "Buffer_analysis" and click on it.
- 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.
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.