introtopython session1 slides
DESCRIPTION
PytonTRANSCRIPT
Session 1: November 8, 2013
Introduction to Python for ArcGIS 10.1A hands‐on workshop offered by the Geospatial Centroid and CSU Libraries
Sarah E. Reed, PhD
Associate Conservation ScientistWildlife Conservation Society
Faculty AffiliateDepartment of Fish, Wildlife, and Conservation Biology
Introductions:
1) Name and affiliation
2) Experience using Python or other programming languages
3) How do you plan to use Python in your own GIS research or work?
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 3
Goals of workshop:
1) Introduce the Python scripting language and its application in ArcGIS 10.1;
2) Become familiar with several methods for writing, editing, and running geoprocessing scripts using Python; and
3) Apply Python scripts to automate a GIS workflow, determine the properties of a spatial dataset, and search and update attribute information in ArcGIS.
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 4
Workshop outline:
Session 1 Session 2
1) Introduction
2) What is Python?
3) Python syntax
4) Running scripts
5) Writing scripts
6) Preview
7) Review
8) Automating workflows
9) Describe object
10) Cursor object
11) Troubleshooting
12) Other topics
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 5
Demonstration 1:
SPreAD‐GIS(or why I learned Python)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 6
a spherical spreading loss
b atmospheric absorption
c foliage and ground cover loss
d upwind and downwind loss
e terrain effects
f ambient sound conditions
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 12
cumulative noise from multiple sources
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 13
acoustic effects for species with variable auditory sensitivity
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 14
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 15
What is Python?
Applications of Python in ArcGIS:
1) Automate workflows
2) Batch process data
3) Manipulate data tables, geometry, and map docs
4) Use functions accessible only by scripts
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 16
Advantages of Python:
1) Open source
2) Cross‐platform
3) Object‐oriented
• A data structure that combines data with a set of methods for accessing and managing those data
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 17
Python editors:
Integrated Development Environment (IDE):A software application for programming and software development
Source code editor:A text editor for software code, with features specially designed to simplify and speed up writing and editing of code
Suggested Python editors:
1) IDLE
2) PythonWin (sourceforge.net)
3) PyScripter (code.google.com/p/pyscripter)
4) Others (wiki.python.org/moin/PythonEditors)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 18
User resources:
1) Books
2) Websites
www.python.org
forums.arcgis.com
3) ArcPy site package (online)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 20
User resources:
4) Web courses
Python for Everyone (free)
Basics of Python (for ArcGIS 10) ($32)
Using Python in ArcGIS Desktop 10 (free)
Python Scripting for Geoprocessing Workflows ($32)
Creating Python Toolboxes Using ArcGIS 10.1 (free)
Python Scripting for Map Automation in ArcGIS 10 ($32)
5) Instructor‐led courses
Introduction to Geoprocessing Scripts Using Python ($1515)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 21
Exercise 1:
Download workshop data and verify software installation
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 22
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 23
Python structure and syntax
Python structure:
1) Programs are composed of modules
2) Modules contain statements
3) Statements contain expressions
4) Expressions create and process objects
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 24
Object:
A piece of memory, with values and associated operations; also known as variables
Types of objects:
• Numbers
• Strings
• Lists
• Files
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 25
Expression:
Processes an object: x * 2
Statement:
Performs a task, via an expression: y = x * 2
Types of statements:• Assignment: x=5
• Call: open(‘DataFile’)
• import
• if/elif/else
• for, while
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 26
Module:
A library of tools; permanent file of code, composed of statements
Types of modules:
• Standard library modules: os, sys, string … (module index)
• Specialized modules or site‐packages: arcpy (site package)
a.py
b.py
c.py
Standard library modules
Your script Modules
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 27
Python syntax:
• Case sensitivity (DataFile ≠ datafile)
• Indentation
• File paths (/, \\ or r’string’)
• Quotation marks (“, ‘)
• Commenting (#)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 28
Python structure:
1) Programs are composed of modules
2) Modules contain statements
3) Statements contain expressions
4) Expressions create and process objects
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 30
Demonstration 2:
Writing a basic script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 31
Exercise 2:
Writing a basic script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 32
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 33
Running a Python script in ArcGIS
The ArcPy site‐package:
1) Provides Python access to all geoprocessing tools and extensions in ArcGIS
a. All geoprocessing tools in ArcMap are provided as functions in ArcPy
b. ArcPy also includes several functions not available as tools in ArcMap
2) ArcPy has several sub‐modules with related sets of functions (e.g., spatial analyst, mapping)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 34
Accessing the ArcPy site‐package
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 35
A Python script for ArcGIS should:
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 36
A Python script for ArcGIS should:
1) Include a header
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 37
A Python script for ArcGIS should:
1) Include a header
2) Import modules
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 38
A Python script for ArcGIS should:
1) Include a header
2) Import modules
3) Specify environment settings
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 39
A Python script for ArcGIS should:
1) Include a header
2) Import modules
3) Specify environment settings
4) Define variables
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 40
A Python script for ArcGIS should:
1) Include a header
2) Import modules
3) Specify environment settings
4) Define variables
5) Run geoprocessing tools
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 41
3 ways to run a Python script for ArcGIS:
1) In a Python editor (e.g., PythonWin)
2) In the Python window in ArcMap
3) As a script tool in ArcToolbox
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 42
Demonstration 3:
Three ways to run a script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 43
Coding a workspace filepath:
1) Environment settings (arcpy.env.workspace)
2) Hard code (C:/workspace/exercise/filename.shp)
3) Create a workspace variable (“workspace” + os.sep + “filename.shp”)
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 44
Exercise 3:
Three ways to run a script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 45
Writing a Python script for ArcGIS
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 46
3 ways to write a Python script for ArcGIS:
1) Edit an existing script
2) Export a script from ModelBuilder
3) Build a script in the Python window
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 47
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 48
General tips:1) Python is case sensitive
2) Python is sensitive to indentation
3) Filepaths use single forward slash (/), double back slash (\\), or raw string supression (r”filepath”)
4) May need to hard code filepaths (workspace + os.sep + “filename”)
5) Save scripts with the .py file extension
6) Comment out section: Alt+3 (PythonWin and IDLE)
7) Uncomment section: Alt+4 (PythonWin and IDLE)
8) Avoid schema lock: remove datasets from ArcMap
Demonstration 4:
Three ways to write a script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 49
Exercise 4:
Three ways to write a script
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 50
Introduction What is Python? Python syntax Running scripts Writing scripts Preview 52
Workshop outline:
Session 1 Session 2
1) Introduction
2) What is Python?
3) Python syntax
4) Running scripts
5) Writing scripts
6) Preview
7) Review
8) Automating workflows
9) Describe object
10) Cursor object
11) Troubleshooting
12) Other topics