python: an introduction - recent...

30
PYTHON: AN INTRODUCTION David Wynne, Clinton Dow

Upload: tranphuc

Post on 12-Mar-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

PYTHON: AN INTRODUCTIONDavid Wynne, Clinton Dow

Page 2: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing
Page 3: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

AgendaP

ytho

n • What is Python?• Python 101• Trouble-shooting

Arc

GIS • ArcPy

• Geoprocessing tools in Python

• Batch processing• Python and ArcGIS Pro

Page 4: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

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, widely

useful, great community

Page 5: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Why use Python and ArcGIS?

• Automate repetitive tasks• Make your own geoprocessing tools• Add geoprocessing to web applications• Customize Desktop apps • Extend the capabilities of ArcGIS

Page 6: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

ArcGIS Pro and Python

• Python continues to be an important language

• Migration from 10.x- Most geoprocessing tools- Mapping changes: arcpy.mapping vs. arcpy.mp- Pro uses Python 3, 10.x uses Python 2- Analyze Tools For Pro

• Future note: ArcGIS Pro 1.3 uses a conda distribution

http://esriurl.com/PythonProMigration

Page 7: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - IDEs

• Python code is written in .py files- Or an interactive Python prompt (Python command line, Python window in ArcGIS)

• Where do I write Python code?- IDEs such as Wing IDE, PyCharm, Spyder, PyScripter, Python IDLE, …

• How do I run?- Double-click .py, from command line, IDEs, ArcGIS

Page 8: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - Comments

Page 9: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - Strings and string manipulation

• Combining strings

• Pathname manipulations

Page 10: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - Conditional logic

• Python has logic for testing conditions- if, elif, else statements- Colon at end of each condition- Indentation determines what is executed- == tests equality; other operators like >, <, !=

Page 11: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - iteration

• Python has for and whilestatements for looping

- Colon at end of statement- Indentation determines what

is executed

Page 12: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python 101 - libraries and import

• Python is “batteries included”• Python has a broad collection of 3rd party libraries• Can organize your own functionality and import it

- Organize and re-use

• Additional libraries are loaded using import

Page 13: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

DemoPython basics

Page 14: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

ArcGIS Python window

• Both Desktop and Pro have an embedded, interactive Python command line• Access to Python and modules within ArcGIS applications• Experiment with Python code interactively

Page 15: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python building blocks

• Module: a Python file where functions live; import• Package: a collection of related modules

• Function: a defined piece of functionality that performs a specific task; requires arguments

• Class: a blueprint to create an object

• math.sqrt(100)

Page 16: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

ArcPy

• Access point to ArcGIS functionality through Python- Desktop, Server, Engine, and Pro

1. Geoprocessing tools2. Functions like ListFeatureClasses, Describe3. Classes like Polygon, SpatialReference, FieldMap4. Modules

1. Mapping: arcpy.mapping / arcpy.mp2. Data access: arcpy.da3. Map algebra: arcpy.sa4. Network Analyst: arcpy.na

Page 17: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Run geoprocessing tools

• import arcpy

• Follow tool syntax- arcpy.toolname_toolboxalias(arguments)

orarcpy.toolboxalias.toolname(arguments)

- Include input and output parameters

• How do I use a specific tool?- Tool help page- Copy as Python Snippet- help(arcpy.Buffer_analysis)

Page 18: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Demo

Geoprocessing and Python

Page 19: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Geoprocessing environment settings

• Control the processing environment of the tools you run- See tool help for honored environments

• Productivity and code cleanup• Environments are properties on arcpy.env (over 50)

Page 20: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Troubleshooting

• Why do errors occur?- Incorrect tool use, typos, syntax,

logic errors

• My script doesn’t work?- Examine the messages- Use Python exception handling- Debug the script in an IDE

Page 21: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Geoprocessing tool messages

• Three types of messages- Informative, warning, error

• Displayed in ArcMap / Pro- Results- Messages window- Python window

• To access messages in Python- arcpy.GetMessages()

Page 22: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python exception handling

• try / except statements- Try to do something, and

if an exception occurs, do something else

• Many different exception types

• An arcpy.ExecuteErrorexception occurs when a geoprocessing tool fails

Page 23: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

ArcPy functions

• An ArcPy function for many jobs

• Support geoprocessing workflows

• Enable automation of manual tasks

Page 24: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Batch processing

• Automating a process to run multiple times- Clip every feature class in a geodatabase to a common boundary- Calculate statistics for every raster in a folder

• List functions used in Python to perform batch processing- Also arcpy.da.Walk

Page 25: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Batch processing (ListFeatureClasses)

Page 26: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Getting data properties

• Describe functions reads data properties

• Returns an object with properties like:- Data type- Shape type- Spatial reference

Page 27: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

DemoWriting a Python script

Page 28: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Conda is part of Pro at 1.3

• “Conda is an open source package management system and environment management system for installing multiple versions of software packages and their dependencies and switching easily between them.” -- http://conda.pydata.org/docs/

• Conda solves limitations in core Python infrastructure- Handling dependencies- Locating, compiling Python libraries- Managing multiple Python versions

Page 29: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Please take our SurveyYour feedback allows us to help maintain high standards and to help presenters

Find the session you want to review

Find your event in the Esri Events App

Scroll down to the bottom of the session

Answer survey questions and submit

Page 30: Python: An Introduction - Recent Proceedingsproceedings.esri.com/library/userconf/proc16/tech-workshops/tw_150...Use Python exception handling-Debug the script in an IDE. Geoprocessing

Python at User Conferencehttp://esriurl.com/PythonUC16

Tues Wed Thurs

Python: An Introduction 8:30 8:30

Python: Beyond the Basics 10:15 10:15

Python: Building Geoprocessing tools 1:30 1:30

Python: Raster Analysis 8:30 1:30

Python: ArcPy Tips and Tricks 1:30

Harnessing the Power of Python in ArcGIS using the Conda Distribution 3:30

Python Map Automation: Introduction to arcpy.mapping / arcpy.mp 1:30 1:30

Advanced Map Automation with Python in ArcGIS for Desktop 3:15 3:15