Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started...

37
Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011

Transcript of Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started...

Page 1: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Esri International User Conference | San Diego, CA

Technical Workshops |

Python – Getting StartedDrew Flater, Ghislain Prince

July 12 - July 14, 2011

Page 2: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Does this describe you?

• New to Python scripting

• Comfortable using ArcGIS but want to become more efficient

• Moving to Python from other scripting language

• Interested in what’s new in ArcGIS 10

Page 3: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Agenda

• Python scripting essentials- What is Python? Why use Python scripting?

- Python 101

- What is ArcPy?

- Scripting geoprocessing tools

- Error handling

• ArcPy functions

• Batch processing

• Python script tools

Page 4: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python Scripting Essentials

Page 5: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

What is Python?

• “Python is an easy to learn, powerful language… (with) high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing…make it an ideal language for scripting…in many areas and on most platforms.” –python.org

• Scripting language of ArcGIS

• Free, cross-platform, easy to learn, great community

Page 6: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Why use Python scripting?

• There are many ways to run single tools

• Use scripting to develop, execute, and share geoprocessing workflows

• Improves productivity

Page 7: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python 101

• Where do I write Python code?- IDE like PythonWin or PyScripter

- Python window in ArcGIS

• How do I run a Python script?- Double-click or cmd prompt

• What are variables?- A name that stores a value; assigned using =input = "C:/Data/Roads.shp"

distance = 50both = [input, distance]

# Variables act as substitutes for raw valuesarcpy.Buffer_analysis(input, "Roads_buffer.shp", distance)

Page 8: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python 101

• Python has logic for testing conditions- if, else statement

- Colon at end of each condition

- Indentation determines what is executed

- == tests equality; other operators like >, <, !=

var = "a"if var == "a": # Execute indented lines print("variable is a")else: print("variable is not a")

Page 9: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python 101

• Techniques for iterating or looping - While loops, for loops- Colon at end of statement

- Indentation determines what is executed

x = 1while x < 5: print x x = x + 1

x = [1, 2, 3, 4]for num in x: print num

Page 10: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python 101

• Case sensitivity- Variables, functions, etc. are case sensitive

- name ‘X’ is not defined, function ‘X’ does not exist

• Building blocks- Function: a defined piece of functionality that performs a

specific task; requires arguments ()

- Module: a Python file where functions live; imported

- Package: a collection of modules

- math.sqrt(100) … 10.0

- “There’s a module for that”

Page 11: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

ArcPy

• The access point to geoprocessing tools

• A package of functions, classes and modules, all related to scripting in ArcGIS- Helper functions that enable workflows (ListFeatureClasses, Describe,

SearchCursor, etc)

- Classes that can be used to create complex objects (SpatialReference, FieldMap objects)

- Modules that provide extended functionality (Mapping, SpatialAnalyst modules)

• Enhancement of arcgisscripting module (pre-10.0)- Your old scripts will work

Page 12: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

ArcGIS Python window

• Embedded, interactive Python window within ArcGIS- Access to ArcPy, any Python functionality

• Great for experimenting with Python and learning tool syntax- Help pane

Page 13: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Demo

Scripting geoprocessing tools

Page 14: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Executing a tool in Python

• ArcPy must be imported

• Follow syntax: arcpy.toolname_toolboxalias()

• Enter input and output parameters

# Import ArcPyimport arcpy

# Set workspace environmentarcpy.env.workspace = "C:/Data"

# Execute Geoprocessing toolarcpy.Buffer_analysis("Roads.shp", "Roads_buffer.shp", "50 Meters")

Page 15: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Getting tool syntax

• Tool help page

• Copy as Python Snippet

• Export Model to Python script

• Drag tool into Python window

• help(“arcpy.Buffer_analysis”)

Page 16: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Setting environments in Python

• Accessed from arcpy.env- Workspace, coordinate system, extent, etc.

• Global parameter- See tool help for honored environments

• Provides finer control of tool execution

• Makes scripting easier

arcpy.env.workspace = "C:/Data"

arcpy.env.extent = "0 0 100 100"

Page 17: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Error handling

• Why do errors occur?- Incorrect tool use

- Typos

- Syntax errors

• What can I do if my script doesn't work?- View the geoprocessing messages

- Use Python error handling

- Debug the script in an IDE

Page 18: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Geoprocessing messages

• Three types of messages:- Informative messages - Warning messages- Error messages

• Displayed in the Python window

• arcpy.GetMessages()- (): All messages- (0): Only informative messages- (1): Only warning messages- (2): Only error messages

Page 19: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

# Execute Geoprocessing toolarcpy.Buffer_analysis("Roads.shp", "Roads_buffer.shp", "50 Meters")

# Print the execution messagesprint(arcpy.GetMessages())

>>> Executing: Buffer Roads.shp Roads_buffer.shp '50 Meters' Start Time: Tue July 12 08:52:40 2011Executing (Buffer) successfully.End Time: Tue July 12 03:52:45 2011(Elapsed Time: 5.00…

arcpy.GetMessages

Page 20: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python error handling

• Try…Except…- "Try to do something, and if an error occurs, do

something else!"

# Start Try blocktry: arcpy.Buffer_analysis("Roads.shp", "Roads_buffer.shp",

“50 Meters”)

# If an error occursexcept: # Print that Buffer failed and why print("Buffer failed")

print(arcpy.GetMessages(2))

Page 21: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Demo

Error handling

Page 22: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

ArcPy Functions

Page 23: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.
Page 24: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

ArcPy functions

• Helper functions

• Perform useful scripting tasks- Print messages (GetMessages)

- List data to perform batch processing (ListFeatureClasses, 12 total List functions)

- Getting data properties (Describe)

- Etc.

• Supports automation of manual tasks

Page 25: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Batch processing

• Run a geoprocessing operation multiple times with some automation- Clip every feature class in a geodatabase to a boundary

- Calculate statistics for every raster in a folder

• List functions used in Python to perform batch processing

• One of the highest values in scripting

Page 26: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

arcpy.ListFeatureClasses

# Set the workspacearcpy.env.workspace = "C:/Data/FileGDB.gdb/FDs"

# Get a list of all feature classesfcList = arcpy.ListFeatureClasses()

# Print the list of feature classes one at a timefor fc in fcList:print(fc)

Page 27: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

ArcPy functions -- Describe

• Use the Describe function to read data properties- Returns an object with properties

# Describe a feature classdesc = arcpy.Describe("C:/Data/Roads.shp")

print(desc.shapeType)>>> "Polyline"

• Allows script to determine properties of data- Data type (shapefile, coverage, network dataset, etc.)

- Shape type (point, polygon, line, etc.)

- Spatial reference

- Etc.

Page 28: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Demo

Batch Processing

Page 29: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python script tools• Connects Python to ArcGIS

• Best way to create and share custom workflows- More accessible than stand-alone Python script

- Extends ArcGIS

• Integrated with geoprocessing framework- Inherits geoprocessing properties and

environments from application

- Can be used in ModelBuilder

- Works with map layers

Page 30: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Receiving arguments

• Arguments are user-defined inputs to a script- Values passed to script from user, instead of hard-coded

• Use GetParameter or GetParameterAsText to read arguments

# Create variables from input argumentsinputFC = arcpy.GetParameterAsText(0)outputFC = arcpy.GetParameterAsText(1)

# First and third parameters come from argumentsarcpy.Clip_analysis(inputFC, "C:/Data/boundary.shp", outputFC)

Page 31: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Connecting parameters

Page 32: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Demo

Script tools

Page 33: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Python scripting resources• ArcGIS Resource Center- resources.arcgis.com

- Online documentation

- Geoprocessing: script gallery, blog, tutorials, presentations

• python.org

• Python References- Learning Python by Lutz, et al.

- The Python Standard Library by Example by Hellmann

- diveintopython.org

Page 34: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Esri Training for Pythonesri.com/training

Page 35: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Other Python sessions

• Python – Intermediate

- Following this session

• Python – Raster Analysis

- Wed 3:15pm Room 5 A/B

• Python - Scripting for Map Automation

- Wed 3:15pm Room 9

• Building Tools with Python

- Thu 10:15am Room 9

• Spatial Analysis Island demo theater

Page 36: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.

Thanks for your feedback!www.esri.com/sessionevals

Page 37: Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.