python - beyond the basics · esri uc2013 . technical workshop . technical workshop 2013 esri...

35
Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond the Basics David Wynne, Ken Smith

Upload: others

Post on 21-Jun-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Technical Workshop

2013 Esri International User Conference July 8–12, 2013 | San Diego, California

Python – Beyond the Basics David Wynne, Ken Smith

Page 2: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Agenda

Python – Beyond the Basics

Data structures &

functions

Script tools

Classes

Cursors

Geometry

Page 3: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Data structures & functions

Python 201:

Page 4: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Key Python data structures

• Lists - Flexible - Ordered

• Tuples - Immutable - Ordered

• Dictionary - Key/value pairs

Python – Beyond the Basics

Page 5: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

List comprehension

• Compact way of mapping a list into another

Python – Beyond the Basics

Page 6: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Defining Functions

• Organize and re-use functionality

Python – Beyond the Basics

Define your function

Return a result Call the function

Page 7: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Data structures

Demo

Page 8: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Script tools

Page 9: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Quadrat Analysis tool

Demo

Page 10: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Geoprocessing Framework and Python

• Tools can be called from Python • Python code can be turned into a tool

• A script tool… - Looks and behaves like any system tool - Provides default validation - Has no UI programming

Python – Beyond the Basics

Page 11: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Script tools

• Tools have parameters - Use GetParameterAsText (or GetParameter) to

get inputs

• Script tools support messages - arcpy.AddMessage - arcpy.AddWarning - arcpy.AddError

Python – Beyond the Basics

Page 12: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Tip: Script Template

Python – Beyond the Basics

http://esriurl.com/4558

Page 13: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Part B: Jump start your analysis with a template

Quadrat Analysis tool

Demo

Page 14: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

arcpy classes

Page 15: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

arcpy classes

• Classes are used to create objects

• Frequently used for… - Tool parameters - Working with geometry

Python – Beyond the Basics

Page 16: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

arcpy Classes

• Most tool parameters are easily defined with a string or number

• Some are not: - Spatial reference - Field map

• Classes can be used to define these parameters

Python – Beyond the Basics

Page 17: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Using classes for parameters

• Extent

• SpatialReference

Python – Beyond the Basics

Page 18: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Part C: • Extent class • Cursors • Chi-square function

Quadrat Analysis tool

Demo

Page 19: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Recap

1. Fast development I. A script template gets you started II. Easy access to tools and functions through

arcpy III. No UI development required IV. Script tools provide default validation

Python – Beyond the Basics

Page 20: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Recap…

2. Used a function to define / group useful functionality

3. Used open source libraries (numpy) 4. Easy to share and deploy

- No ArcObjects or dlls to register

Python – Beyond the Basics

Page 21: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Cursors

Page 22: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Cursors

• Use cursors to access records and features

• Two varieties - ‘Classic’ cursors - ‘Data access’ cursors (new at 10.1)

Python – Beyond the Basics

SearchCursor Read-only

UpdateCursor Update or delete rows

InsertCursor Insert rows

Page 23: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Cursor mechanics

• Data access cursors use lists and tuples - Values are accessed by index

• Classic cursors use row objects - Values are accessed by setValue/getValue

Python – Beyond the Basics

Page 24: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

With statements

• arcpy.da Cursors support with statements

Python – Beyond the Basics

Page 25: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Cursor performance

• Use only those fields you need

• Use tokens - Get only what you need - Full geometry is

expensive

Python – Beyond the Basics

Page 26: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

arcpy.da cursors

Demo

Page 27: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Geometry

Page 28: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Geometry and cursors

• Can create geometry in different ways - Geometry objects

- List of coordinates

- Using other formats

- JSON, WKT, WKB

Python – Beyond the Basics

Page 29: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Geometry and cursors

• Can create geometry in different ways - Geometry objects

- List of coordinates

- Using other formats

- JSON, WKT, WKB

Python – Beyond the Basics

Page 30: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Working with geometry

• Relational: - Is a point within a polygon?

Python – Beyond the Basics

Page 31: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Working with geometry

• Topological - What is the intersection of two

geometries?

Python – Beyond the Basics

Page 32: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Working with geometry

• Others - What is the halfway point of a line?

- What is the geodesic area of a polygon?

Python – Beyond the Basics

Page 33: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Resources

• Python resource center - pro.arcgis.com/analysis/python/

• ArcPy Café - arcpy.wordpress.com

• Python at UC 2013 - esriurl.com/uc13python

Python – Beyond the Basics

Page 34: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop .

Please fill out the session evaluation

First Offering ID (Tues.): 1210 Second Offering ID (Wed.): 1311

Online – www.esri.com/ucsessionsurveys

Paper – pick up and put in drop box

Thank you…

Python – Beyond the Basics

Page 35: Python - Beyond the Basics · Esri UC2013 . Technical Workshop . Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California Python – Beyond

Esri UC2013 . Technical Workshop . Python – Beyond the Basics