Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted,...

36
Python: Getting Started Ben Ramseth [email protected] @esriMapNinja

Transcript of Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted,...

Page 1: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python: Getting StartedBen Ramseth

[email protected]

@esriMapNinja

Page 2: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

E M E R A L D

S A P P H I R E

T H A N K Y O U T O

OUR SPONSORS

Page 3: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Topics covered

• What’s is python?

• Why use python?

• Basics of python

• ArcPy

• Geoprocessing tools

• Other python options

Page 4: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

What is Python?

Page 5: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

What is Python?

• Python is an interpreted, object-oriented, high-level programming .

• Python's simple, easy to learn syntax emphasizes readability and therefore reduces

the cost of program maintenance.

• Python supports modules and packages, which encourages program modularity

and code reuse.

• Scripting language of ArcGIS

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

Page 6: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Why use Python?

Page 7: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Why use Python with ArcGIS

• Automate repetitive tasks

• Develop custom tools

• Add geoprocessing to web applications

• Customize Desktop apps

• Extend the capabilities of ArcGIS

Page 8: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Basics of Python

Page 9: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Where do I write Python scripts?

• Python file is text with .py extension

• Python window in ArcGIS

• Best to use IDE

- Integrated Development Environment

Page 10: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python in an IDEGIS from the comfort of your development environment

• Integrated Development Environments

- Debuggers

- Test Frameworks

- Coverage

- PyLint/pycodestyle

- Version Control Integration

- Refactoring Tools

- Virtual Environment Support

- AutoComplete

Page 11: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python in an IDEGIS from the comfort of your development environment

• Recommended IDEs- PyCharm

- Python Tools for Visual

Studio

- Spyder

- Eclipse with PyDev

- Wingware

Page 12: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python basics

• Variable

- A name that stores a value; assigned using =

• Logic for testing conditions

- If, else statement

- Colon at the end of each condition

- Indentation determines what is executed

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

• Techniques for iterating or looping

- While loops, for loops

- Colon at end of statement

- Indentation determines what is executed

Page 13: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python building blocks

• Function:

- A defined piece of functionality that performs a specific task; requires arguments

• Module:

- a Python file where functions live; import

• Package:

- A collection of related modules

• Code example

- Math.sqrt(100) … 10

• Python Standard Library / Built-ins

- Os, sys, math, datetime, urllib2

Page 14: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Basics

Python Demo

Page 15: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcPy

Page 16: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcPy

• ArcGIS Python site package

• Introduced in ArcGIS 10.0- June 29, 2010

• Successor to arcgisscripting module

• Create tools leveraging ArcGIS- In-Application Geoprocessing Tools and Scripting

- Standalone Python Programs

• 1,000+ ArcPy-based Tools Included in ArcGIS

Page 17: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcGIS Desktop and ArcGIS ProPython versions

• ArcGIS Desktop uses

Python 2.7

• ArcGIS Pro uses Python

3.x

Page 18: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcPy in ArcGIS Pro

•ArcGIS 10.x to ArcGIS Pro- ArcObjects SDK will not move to Pro

- Great time to be a Python GIS Developer!

- ArcObjects high cost of maintenance and very tightly

coupled to COM

- Rewrite code in Python 3 in Pro

- Platform agnostic vs. .NET code

- Web Services

- Vast library of Packages

- Improved features in Pro

- … it’s Python and not .NET

Page 19: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Automating ArcGIS with PythonToolboxes, Python Toolboxes, Interpreters, Command Line, Services…

• Multiple ways to create tools- ArcGIS Toolbox (.tbx)

- Python Toolbox (.pyt)

- Stand-alone Scripts (.py)

- Interactive Python Window

• Decouple logic code from Toolbox- Testing!!

• Distutils to create packaged codeSharing

Install into tools as needed

Page 20: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Running geoprocessing tools

with Python

Page 21: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Run gp tools

• Import arcpy

• Follow tool syntax

- Arcpy.toolname_toolboxalias()

- Enter input and output parameters

• How do I use a specific tool?

- Tool help page

- Copy as Python Snippet

- Help(arcpy.Buffer_analysis)

Page 22: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Geoprocessing environment settings

• Use geoprocessing environments as global parameters

- See tool help for honored environments

• Productivity and code cleanup

• Arcpy.env

- Example: arcpy.env.workspace = “c:/code”

Page 23: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Troubleshooting

• Why do errors occur?

- Incorrect tool use, typos, syntax, data already exists

• My script doesn’t work…now what!

- View geoprocessing messages

- Use Python error handling

- Debug the script in an IDE

Page 24: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Geoprocessing messages

• Three types of messages

- Info, warning, Error

• Displayed in the Python window

• Errors displayed in the IDE

• To see other messages

- arcpy.GetMessages

Page 25: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python error handling

• Try…Except

- Try to do something, and if an error occurs, do something else

- Like…

- Print(arcpy.GetMessages())

Page 26: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcPy functions

• Perform useful tasks

- List data:

- ListFeatureClasses

- Get data properties:

- Describe

• Enables automation of manual tasks

Page 27: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Batch processing

• Automating a geoprocessing operation to run multiple times

- Clip every feature class in a geodatabase to a boundary

- Calculate statistics for every raster in a folder

• May List functions in ArcPy to perform batch processing

• Example:

- arcpy.env.workspace = “c:/data”

- fcList = arcpy.ListFeatureClasses()

- for fc in fcList:

- print(fc)

Page 28: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Getting data properties

• Describe function reads data properties

• Returns an object with properties

- Data type

- Shape type

- Spatial reference

- Fields

• Example:

- desc = arcpy.Describe(“c:/data/roads.shp”)

- print(desc.shapeType)

Page 29: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Python script tools

• Python script as custom geoprocessing tool

• Great way to create and share workflows, extend ArcGIS

- More accessible the stand-alone

• Integrated with geoprocessing framework

- Use it just like any other tool

- Geoprocessing properties and environments

- Works with map layers

Page 30: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Script against a portal

ArcGIS API for Python

Page 31: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

ArcGIS Python API vs ArcPy

ArcGIS Python API• Script against a portal

- ArcGIS Online or ArcGIS Enterprise

- Python 3.5

• Analysis, Portal Admin, Content

Creation, Big Data Analysis

ArcPy• ArcGIS Desktop

- ArcMap – Python 2.7

- ArcGIS Pro – 3.5

• Mostly geoprocessing

• Some map automation

Page 32: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Four different groups

working with the

ArcGIS Python API

- Org Administrators

- Content Publishers

- GIS Analysts and Data Scientists

- Power Users/Developers

Page 33: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Overview of the ArcGIS Python

API

• A Pythonic GIS API• Corresponds to best practices

• Uses standard Python constructs

• Data structures with clean, readable idioms

• Easy for a Python programmer to use ArcGIS

• Easy for an ArcGIS user to script and automate their GIS

Page 34: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

The Future of Python in ArcGIS

• Conda integration into ArcGIS- Advanced Python Package Management

- Curated channels of scientific Python packages

- Includes C and Fortran compiler

- Create and host your own conda packages

- Easily share your code with the GIS community

- Built-In Virtual Environment Support

- Manage multiple environments with access to ArcPy

- Easily share environments

- Trivializes dependency issues

Page 35: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes

Questions?

Page 36: Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted, object-oriented, high-level programming . •Python's simple, easy to learn syntax emphasizes