You are on page 1of 22

Introduction to Python

LESSON 11

Why Python?
Simple and easy to learn
Free, Open source
Cross Platform
Interpreted Language
Object oriented
Embraced by geospatial community, including
ESRI

Introducing Python using the Python


window in ArcGIS
Lets take the Buffer tool which you recently ran from the ArcToolbox GUI
and run it in the ArcGIS Python window.
This time, well make buffers of 15 miles around the cities.
1.Open ArcMap to a new empty map.
2.Add the us_cities.shp dataset from the Lesson10 data.
3.On the Standard toolbar, click the Python window button . Once the window appears,
drag it over to the side or bottom of the screen to dock it.
4.Type the following in the Python window
>>> import arcpy
>>> arcpy.Buffer_analysis("us_cities", "us_cities_buffered", "15 miles", "", "", "ALL")
5.Zoom in and examine the buffers that were created.

Differences between writing code in the


Python window and the others
In the Python window, you can reference layers in the map document by their
names only, instead of their file paths. Thus, we were able to type "us_cities"
instead of something like "C:\\data\us_cities.shp". We were also able to make
up the name of a new layer "us_cities_buffered" and get it added to the map by
default after the code ran.
If you're going to use your code outside the Python window, make sure you use
the full paths

When you write more complex scripts, it will be helpful to use an integrated
development environment (IDE), meaning a program specifically designed to
help you write and test Python code, ex. PythonWin IDE.

Python IDE
Python comes with a simple default editor called IDLE;
However, in this course youll use the PythonWin integrated
development environment (IDE) to help you write code.
PythonWin is free, has basic debugging capabilities, and is
included with ArcGIS

Download:
https://sourceforge.net/projects/pywin32/files/pywin32/Build%
20219/pywin32-219.win32-py2.7.exe/download

Variables in Python
Variable names are case sensitive. myVariable is a different variable than
MyVariable.
Variable names cannot contain spaces.

Variable names cannot begin with a number.


A recommended practice for Python variables is to name the variable
beginning with a lower-case letter, then begin each subsequent word with a
capital letter. For example: myVariable, mySecondVariable, roadsTable,
bufferField1, etc.
Variables cannot be any of the special Python reserved words such as "import"
or "print."

Example
>>> print x + 3
5
>>> myTeam = "Nittany Lions"
>>> print myTeam
Nittany Lions
>>> string1 = "We are "
>>> string2 = "Penn State!"
>>> print string1 + string2
We are Penn State!
>>> x = 5
>>> x = x - 2
>>> print x
3

Python Syntax
Python is case-sensitive both in variable names and reserved words
You end a Python statement by pressing Enter and literally beginning a new line.
If you have a long statement that you want to display on multiple lines for
readability, you need to use a line continuation character, which in Python is a
backslash (\). You can then continue typing on the line below and Python will
interpret the line sequence as one statement.

Indentation is required in Python to logically group together certain lines, or


blocks, of code. You should indent your code four spaces inside loops, if/then
statements, and try/except statements.
You can add a comment to your code by beginning the line with a pound (#) sign.

Demo
Task: Add field(s) to a shapefile

As always with ArcGIS, there are multiple ways to get it done

Method 1: Attribute table

Method 2: ArcToolbox tool

Method 3: ModelBuilder

Method 4: Python
Open the Python window in ArcMap by
clicking the Python button in the
Geoprocessing menu

Python basics

Command line interface (CLI) runs


Python and arcpy commands
In some software, the graphical user
interface is merely an abstraction
layer over the CLI

Python console in ArcMap is suitable


for running small snippets of code
and testing syntax; not suited towards
running entire programs

Python basics (cont.)

print is the command

Hello world! is a string object that is output by the print


command

Python basics (cont.)

Python treats + as sum, which works as expected with integer (int) and
floating point (float) objects.
You cant sum an int and a string (str), but you can concatenate two strings.

ArcPy commands

Every tool accessible from the ArcToolbox and Search panes can be run from
ArcPy.
In the example above, the Add Field tool from the Data Management toolbox is run.

ArcPy commands (cont.)


How to find a commands syntax?

From
results
window

Console code completion


Web documentation:
http://resources.arcgis.com/en/help/m
ain/10.2/
Export geoprocessing results to Python
snippet
Export ModelBuilder to Python snippet

From
Model
Builder

Python syntax for a Tool


1. Run the tool interactively (e.g. buffer) with your input data, output
data and any other relevant parameters (e.g. distance to buffer)
2. Go to the Geoprocessing -> Results window and right click the
completed tool run.
3. Pick "Copy Python Snippet"

4. Paste the code into PythonWin or the Python code window to see
how you would code the same operation you just ran in Desktop in
Python.

Example: Printing the spatial reference of


a feature class

Creating a Custom Toolbox


Empty custom toolboxes can be
made in the Catalog pane.
Each toolbox can contain multiple
tools
Each tool references a Python
script file with the .py extension.

Creating a Custom Toolbox (cont.)

You might also like