Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS...

17
Getting Started with Python and ArcGIS Pro Presented to: Esri Canada User Conference Presented by: Nathan Enge April 13, 2016 bit.ly/Python45

Transcript of Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS...

Page 1: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Getting Started with

Python and ArcGIS Pro Presented to: Esri Canada User Conference

Presented by: Nathan Enge

April 13, 2016 bit.ly/Python45

Page 2: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Agenda

Python ArcGIS

Page 3: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

What is Python?

• High-level scripting language

Page 4: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Python 101

• Working with variables

- Numbers

- Strings

- Lists

• Conditional statements

• Looping statements

- Count/collection-controlled

- Condition-controlled

• and more…

Page 5: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Where to write code?

• Inside of ArcGIS Pro

- Python Window

• Outside of ArcGIS Pro

- Integrated Development Environment

IDE examples: IDLE, PyScripter

.py

Embedded command line

Experiment with Python code interactively

Page 6: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Python and ArcGIS

• Make your own geoprocessing tools

• Automate repetitive tasks

• Add geoprocessing to web applications

• Customize Desktop apps

• Extend the capabilities of ArcGIS

Data Management Data Conversion Map Automation Spatial Analysis

Page 7: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Python and ArcGIS Pro

• To run stand-alone scripts:

- Install ArcGIS Pro Python from my.esri.com

- Before executing your script…

- Sign me in automatically is checked

- ArcGIS Pro is open

- Authorize to work offline

• ArcGIS Pro uses Python 3

Python 3.4

Built-in

functions

Modules

ArcPy NumPy

Matplotlib xlrd / xlwt

Site packages

Page 8: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

ArcPy site package

• Access point to ArcGIS functionality through Python

Functions

Automating map

production

Listing data

Accessing

field values

Performing

spatial analysis

Classes

Points

Polylines

Polygons

Spatial Reference

Cursors

Workflow

Scripting of

geoprocessing

tools

Modules

Data Access

Mapping

Network Analyst

Spatial Analyst

Workflow Manager

import arcpy

Page 9: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Executing a tool

import arcpy arcpy.env.workspace = "C:/Data" input = "Streets.shp" output = "Streets_buffer.shp" dist = "500 METERS" arcpy.Buffer_analysis(input, output, dist)

Getting syntax help • Tool help page (e.g. Buffer)

• Copy as Python Snippet

• Export model to Python script

• Drag tool into Python window

• help("arcpy.Buffer_analysis")

Pseudocode • ArcPy must be imported (standalone)

• Set environment settings

• Define input and output parameters

• Run the tool

arcpy.toolname_toolboxalias()

Page 10: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Creating a script tool

import arcpy arcpy.env.workspace = "C:/Data" input = arcpy.GetParameterAsText(0) output = arcpy.GetParameterAsText(1) dist = arcpy.GetParameterAsText(2) arcpy.Buffer_analysis(input, output, dist)

Pseudocode • ArcPy must be imported

• Set environment settings

• Create variables from input arguments

• Run the tool

• User defines the inputs

Page 11: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Batch processing

• List functions

• Cursor functions

# Get a list of all the functions fcList = arcpy.listFeatureClasses() for fc in fcList: print(fc)

# Get a value for each row in a table field = "StreetName" cursor = arcpy.SearchCursor(fc) for row in cursor: print(row.getValue(field))

Returns list of data • Feature classes • Fields • Rasters • and many more…

Works with rows in a table • SearchCursor

• InsertCursor

• UpdateCursor

Page 12: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Automate mapping

• Use the arcpy.mp module

- Report information contained in projects

- Update, repair, or replace layer data sources

- Work with layout elements

- Use map export commands

# Export a PDF from a layout import arcpy aprx = arcpy.mp.ArcGISProject(r"C:\Project\Banff.aprx") lyt = aprx.listLayouts("Main Attractions*")[0] lyt.exportToPDF(r"C:\Project\Output\Attractions.pdf")

Page 13: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Demonstration

• Review Python basics

• Write some code

- Python window

- PyScripter IDE

• Build a geoprocessing script tool

• Automate map production

bit.ly/Python45

Page 14: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training

Migrating Python from ArcGIS for Desktop

• Some of your code will still work, but…

- Changes to functionality in arcpy

- e.g. arcpy.mp replaces arcpy.mapping

- Run the Analyze Tools for Pro tool

- Upgrade to Python 3

- e.g. print functions replaces print statements

- Use the 2to3 utility or the python3porting.com resource

- Unsupported data formats and tools

- e.g. personal geodatabases are not supported

- Access a list of tools not supported in ArcGIS Pro

Page 17: Getting Started with Python and ArcGIS Pro - Esri Canada · Getting Started with Python and ArcGIS Pro ... -Python Scripting for ArcGIS -GIS Tutorial for Python Scripting ... •Training