0% found this document useful (0 votes)
158 views40 pages

Python - Introduction

Python is an open-source programming language that can be used for geoprocessing tasks in ArcGIS. It allows users to automate workflows, customize tools, and extend GIS capabilities. The document introduces Python syntax and discusses how Python can be used with ArcGIS Desktop and ArcGIS Pro through ArcPy and other application programming interfaces.

Uploaded by

ANA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
158 views40 pages

Python - Introduction

Python is an open-source programming language that can be used for geoprocessing tasks in ArcGIS. It allows users to automate workflows, customize tools, and extend GIS capabilities. The document introduces Python syntax and discusses how Python can be used with ArcGIS Desktop and ArcGIS Pro through ArcPy and other application programming interfaces.

Uploaded by

ANA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 40

Python – An Introduction

Clinton Dow, Jibin Liu


Agenda

ArcGIS
Python

• What is Python? • ArcPy and Geoprocessing


• Why Learn Python? • Writing Python scripts for
• Python Use Cases Geoprocessing
• Intro to Python Syntax • Python in Esri products
• Writing and executing
Python Scripts
What is Python?
What is Python?
• Python is an open-source programming language
- Released in 1991 by Guido Van Rossum
- Interpreted – no compilation
- Interactive – REPL (Read, Evaluate, Print Loop)
- Object-oriented
• Integrated into ArcGIS
- Geoprocessing Scripts
- Python Window
- Field Calculator Expressions
• Desktop and Web GIS
- arcpy
- ArcGIS Python API
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

• A “Batteries Included” Language


• Large Ecosystem of Open-Source
Packages
• Great community (Conferences,
User Groups, Online…)

[1] https://www.python.org
Why Learn Python?
Why Learn Python?

• Accessible to new-comers • A Versatile Language


- Top language for intro CS courses - “Glue” language that works with
Operating System, Server and the Web
• Large demand in multiple industries
• Extend the capabilities of ArcGIS
• Create your own geoprocessing tools
- Utilize third-party and/or open-source
- Create suite of custom tools
code in your scripts
- Suit client’s needs better than generic tools
- Built-in package management
- Scheduling tasks
• Automate repetitive tasks
- Saves time and money
- Frees up analysts for non-trivial work
Should I Learn Python 2 or Python 3?

• ArcGIS Desktop • ArcGIS Pro


- ArcGIS 10.5 - Python 2.7.12 - ArcGIS Pro 2.0 - Python 3.5.3
- Maintaining Existing Tools - New functionality of Python and ArcGIS
- Extending functionality of ArcMap, - Asynchrony
ArcCatalog - Reduced Memory Footprint
- End of Official Support in 2020 - In active development
Resources to Learn Python

• Websites
- Python.org
- Beginner’s Guide
- Language Reference
- Learnpython.org
- Pluralsight.com
• Esri Training
- Free and Paid Courses
• Books
- Python Scripting for ArcGIS
- Learning Python, 5th Edition
Where is Python Used?
• Examples include:

- Data Analysis
- Entertainment
• Examples include:
- Web Applications

- Entertainment
• Examples include:
- Web Applications
- Data Analysis
Python Syntax
• Math Calculations
Introduction to • Variables

Python Syntax: •
Built-in Data Structures and their operations
Conditions

Demo •
Loops
Import libraries
Python Scripting
Executing Python Scripts

• What is Python File (.py)?


- A text file with .py extension, which python interpreter can read the
instructions and execute them.
- Recommended Syntax defined in Python Enhancement Proposal 8 (PEP8)
• Where to write the .py file?
- Text editor (notepad), Python IDLE, other IDEs
• How to execute the .py file?
- Double-click the .py file
- Command line (python <path to .py file>)
- IDEs
- ArcGIS
- System Service
- Web Service
Python Building Blocks

• Module: a Python file where functions live (.py)


• 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
Executing Python
• Double-click Python File
Scripts: Demo • From Command Line
ArcPy and Geoprocessing
2017
Python in Esri

2015
• Python in Esri products

- Anaconda
2010
- ArcGIS Python API

2004
Python in Esri Conda embedded in Pro since 1.3

• “Conda is an open source package


• Python in Esri products management system and environment
- arcgisscripting management system for installing multiple
versions of software packages and their
- ArcPy
dependencies and switching easily between
them.” -- http://conda.pydata.org/docs/
- ArcGIS Python API

• Conda solves limitations in core Python


infrastructure
- Handling dependencies
- Locating, compiling Python libraries
- Managing multiple Python versions
Python in Esri Script and automate your Web GIS

• A pythonic library to interoperate with Esri Web


• Python in Esri products GIS Products
- arcgisscripting • Designed to integrate with the Jupyter
- ArcPy Notebook, an increasingly popular tool for
- Anaconda academics and data scientists.
Python in ArcGIS Desktop vs. ArcGIS Pro

Feature ArcGIS 10.x ArcGIS Pro


Python 2 √
Python 3 √
Python Window √ √
Python Script Tools √ √
Python Add-Ins √
Conda Package Manager √
Debugging Experience √ Coming soon
ArcGIS Python window

• Both Desktop and Pro have an embedded, interactive Python command line
• Access to Python and modules within ArcGIS applications
• Interact with maps and layers directly with Python code
ArcPy

• Access point to ArcGIS functionality through Python


- Desktop, Server, Engine, and Pro

1. Geoprocessing tools
2. Functions like ListFeatureClasses, Describe
3. Classes like Polygon, SpatialReference, FieldMap
4. Modules
a) Mapping: arcpy.mapping / arcpy.mp
b) Data access: arcpy.da
c) Map algebra: arcpy.sa
d) Network Analyst: arcpy.na
ArcPy - functions

• An ArcPy function for many


operations in ArcGIS UI

• Interact with ArcGIS Tool Dialogues

• Describe existing datasets

• Information about installation


ArcPy - Classes

• Python objects representing major


base classes in ArcGIS.

• Extend ArcGIS objects for use with


other systems.

• Customize behaviors of objects


within your scripts.
ArcPy - Geoprocessing environment settings

• Control the processing environment of the tools you run


- “Global” Environment Variables
- See tool help for honored environments
• Productivity and code cleanup
• Environments are properties on arcpy.env (over 50)
ArcPy - 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
ArcPy - Batch processing (ListFeatureClasses)
ArcPy - Getting data properties

• Describe functions reads data properties


- Like the properties window when right-click on the data

• Returns an object with properties like:


- Data type
- Shape type
- Spatial reference
• Open and execute python commands in ArcGIS
ArcGIS Python Python Window
• ArcPy : environment setting
Window and ArcPy: • ArcPy : batch processing
• ArcPy : getting data properties
Demo
Run geoprocessing tools

• import arcpy

• Follow tool syntax


- arcpy.toolname_toolboxalias(parameters)
or
arcpy.toolboxalias.toolname(parameters)

• How do I use a specific tool?


- Tool help page
- Copy as Python Snippet
- help(arcpy.Buffer_analysis)
Writing a Python • Build Python script for geoprocessing tools
script to chain
geoprocessing tools:
Demo
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()
- arcpy.AddMessage()
- arcpy.AddWarning()
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
Further Learning at Esri UC 2017:
Tues Wed Thurs Fri
Python: An Introduction 8:30am 1:30pm
Python: Beyond the Basics 10:15am 3:15pm
Python: Building Geoprocessing tools 1:30pm 9:00am
ArcGIS: Integrating Python and Conda 1:00pm
Python: Exploring the Ecosystem 12:30pm
Python: ArcPy Tips and Tricks 1:30pm
Working with Python in ArcGIS Pro 10:00am
Python - Tips and Tricks for Working with Cursors 4:00pm
Troubleshooting Python Issues in ArcGIS Desktop 1:30pm
Network Analysis using Python 3:30pm
Python: Raster Analysis 8:30am
Building Python Raster Functions 10:30am
Getting Started With Map Algebra Using the Raster Calculator and Python 9:30am
Python: Introduction to Map Automation 1:30pm
Python: Map Automation in ArcGIS Pro 3:15pm
Python - Tips and Tricks for Working with Arcpy Mapping 12:00pm
Please Take Our Survey on the Esri Events App!

Download the Esri Select the session Scroll down to Complete Answers
Events app and find you attended find the survey and Select “Submit”
your event
THANK YOU!

You might also like