python – beyond the basics
DESCRIPTION
2013 Esri International User Conference July 8–12, 2013 | San Diego, California. Technical Workshop. Python – Beyond the Basics. David Wynne, Ken Smith. Agenda. Data structures & functions. Classes. Geometry. 1. 2. 3. 4. 5. Script tools. Cursors. Python 201:. - PowerPoint PPT PresentationTRANSCRIPT
Esri UC2013 . Technical Workshop .
Technical Workshop
2013 Esri International User ConferenceJuly 8–12, 2013 | San Diego, California
Python – Beyond the BasicsDavid Wynne, Ken Smith
Esri UC2013 . Technical Workshop .
Agenda
Python – Beyond the Basics
Data structures &
functions
Script tools
Classes
Cursors
Geometry
Esri UC2013 . Technical Workshop .
Data structures & functions
Python 201:
Esri UC2013 . Technical Workshop .
Key Python data structures
Python – Beyond the Basics
Esri UC2013 . Technical Workshop .
List comprehension
• Compact way of mapping a list into another
Python – Beyond the Basics
Esri UC2013 . Technical Workshop .
Defining Functions
• Organize and re-use functionality
Python – Beyond the Basics
Define your function
Return a result
Call the function
Esri UC2013 . Technical Workshop .
Data structures
Demo
Esri UC2013 . Technical Workshop .
Script tools
Esri UC2013 . Technical Workshop .
Quadrat Analysis tool
Demo
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
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
Esri UC2013 . Technical Workshop .
Tip: Script Template
Python – Beyond the Basics
http://esriurl.com/4558http://esriurl.com/4558
Esri UC2013 . Technical Workshop .
Part B: Jump start your analysis with a template
Quadrat Analysis tool
Demo
Esri UC2013 . Technical Workshop .
arcpy classes
Esri UC2013 . Technical Workshop .
arcpy classes
• Classes are used to create objects
• Frequently used for…- Tool parameters- Working with geometry
Python – Beyond the Basics
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
Esri UC2013 . Technical Workshop .
Using classes for parameters
• Extent
• SpatialReference
Python – Beyond the Basics
Esri UC2013 . Technical Workshop .
Part C: •Extent class•Cursors•Chi-square function
Quadrat Analysis tool
Demo
Esri UC2013 . Technical Workshop .
Recap
1. Fast developmentI. 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
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
Esri UC2013 . Technical Workshop .
Cursors
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
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
Esri UC2013 . Technical Workshop .
With statements
• arcpy.da Cursors support with statements
Python – Beyond the Basics
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
Esri UC2013 . Technical Workshop .
arcpy.da cursors
Demo
Esri UC2013 . Technical Workshop .
Geometry
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
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
Esri UC2013 . Technical Workshop .
Working with geometry
• Relational: - Is a point within a polygon?
Python – Beyond the Basics
Esri UC2013 . Technical Workshop .
Working with geometry
• Topological- What is the intersection of two
geometries?
Python – Beyond the Basics
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
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
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
Esri UC2013 . Technical Workshop . Python – Beyond the Basics