Post on 25-May-2018
Getting Started with
Python and ArcGIS Pro Presented to: Esri Canada User Conference
Presented by: Nathan Enge
April 13, 2016 bit.ly/Python45
Agenda
Python ArcGIS
What is Python?
• High-level scripting language
Python 101
• Working with variables
- Numbers
- Strings
- Lists
• Conditional statements
• Looping statements
- Count/collection-controlled
- Condition-controlled
• and more…
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
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
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
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
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()
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
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
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")
Demonstration
• Review Python basics
• Write some code
- Python window
- PyScripter IDE
• Build a geoprocessing script tool
• Automate map production
bit.ly/Python45
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
Resources
• python.org
• ArcGIS Pro documentation
- ArcPy reference
- Tool reference
- Python migration for
ArcGIS Pro
• ArcPy Café
• GitHub - Esri
• GIS Stack Exchange
Resources
• Books
- Python Scripting for ArcGIS
- GIS Tutorial for Python Scripting
- Learning Python
- Core Python Programming
• Training Courses
- Getting Started with Python
- Introduction to Geoprocessing
Scripts Using Python
- Mastering Arcpy