Technical Tips
Python - Scripting in ArcGIS 9
by Milos Pelikan, Senior Analyst and Scott Manley, Senior Analyst/Programmer |
Introduction
Python & ArcGIS
Editing and Running Python code
Some basic Python syntax
Books & Websites
Python is a scripting language available free for download
for a range of platforms including Windows, Unix, Linux and
the Mac. Python has gained considerable popularity due to
its simple syntax but powerful library and command set.
The first version of Python was developed in 1990 in Amsterdam,
however, the language is now owned and developed by the Python
Software Foundation. The Python name comes not from the reptile
but from the English comedy team ‘Monty Python’.
Python enjoys a growing popularity for development of scripts
as an alternative to languages such as Perl and TCL.
With the release of ArcGIS 9, ESRI has added support for
the Python language. Python scripts can be written and added
as Tools to ArcEditor or ArcView to perform the type of processing
tasks previously written in AML or Avenue. Tools can be configured
to easily pass one or more arguments to the Python script.
Editing and Running Python code
Editor/IDE
You have a number of options for authoring and editing your
Python scripts:
• The first and simplest is to use your favorite text
editor, there are Python ‘plug-ins’ for many of
the most popular editors such as Textedit and UltraEdit.
• Secondly, the version of Python installed with ArcGIS
comes with a simple IDE called WinPython. This will provide
simple text highlighting and popup syntax to assist you when
authoring scripts.
• The final option is to obtain one of the dedicated
IDEs now being developed for Python. Many of these are commercial
products ranging in price from $100 to $500, most of these
will have 30-60 day evaluation version available for download.
Running your code
As Python is an ‘interpreted’ language rather
than ‘compiled’, a single Python script can be
run on any platform (with some minor exceptions).
To run a Python script we use the Python ‘interpreter’
or ‘run-time engine’, interpreters are available
for most platforms. To run your script, simply type the following
at the command prompt:
“python scriptname.py”
(The python program will need to be in your path or in your
current working directory).
As with all languages, the best way to learn it is to roll
up your sleeves and give it a go. Here are some of the basic
code elements to get started.
Condition statements
Python provides if, elif
and else commands for conditional execution
of script elements.
Example:
i = 10
if i == 1:
print "One"
elif i == 2:
print "Two"
elif i == 3:
print "Three"
elif i == 4:
print "Four"
elif i == 5:
print "Five"
else:
print "Greater than Five" |
Result:
Greater than Five
Notes:
• Python uses ‘==’ rather than ‘=’
for equality checking
• If statements end in ‘:’
• Execution blocks must be indented otherwise an error
will result
• The ‘elif’ statement is used instead of
‘case’ or ‘switch’ statements
Looping statements
For looping, Python provides the familiar for construct.
for will loop through a list or array of items placing each
value it finds into a holding variable. This variable can
then be used to output or interact with each value in turn.
Example 1:
To loop through a list of items use the following syntax:
a
= ['Spatial', 'Vision', 'solutions']
for x in a:
print x
|
a = ['Spatial', 'Vision', 'solutions']
for x in a:
print x
Result:
Spatial
Vision
solutions
Notes:
• The for statement will automatically extract each
item from the array and place it in the variable provided
Example 2:
To repeat a section of code a number of times use the range()
function:
a
= range(3)
for x in a:
print x
|
Result:
0
1
2
Notes:
• range() can also be used with two arguments range(i,
j) with i as the start and j as the end value.
Website www.python.org
This is the home of the python language, from here you can
download python and python documentation. They also have tutorials
and how-to guides.
Website
http://www.activestate.com/Products/ActivePython/
An excellent free python distribution for Windows (comes
with the PythonWin IDE.
Learning Python (O’Reilly)
An excellent introduction to the language and programming.
Dive into Python: www.diveintopython.org
A free but very good introduction for Python (also available
at bookshops)
Programming Python (O’Reilly)
Everything you need to know (not for beginners)
Thinking in Python: http://www.mindview.net/Books/TIPython
Another free and excellent Python book (again not for beginners)
back to top
Copyright © Spatial Vision, Thursday, 31-March-2005
|