Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted,...
Transcript of Python: Getting Started - Esri · 2017-05-24 · What is Python? •Python is an interpreted,...
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
Topics covered
• What’s is python?
• Why use python?
• Basics of python
• ArcPy
• Geoprocessing tools
• Other python options
What is Python?
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
Why use Python?
Why use Python with ArcGIS
• Automate repetitive tasks
• Develop custom tools
• Add geoprocessing to web applications
• Customize Desktop apps
• Extend the capabilities of ArcGIS
Basics of Python
Where do I write Python scripts?
• Python file is text with .py extension
• Python window in ArcGIS
• Best to use IDE
- Integrated Development Environment
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
Python in an IDEGIS from the comfort of your development environment
• Recommended IDEs- PyCharm
- Python Tools for Visual
Studio
- Spyder
- Eclipse with PyDev
- Wingware
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
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
Basics
Python Demo
ArcPy
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
ArcGIS Desktop and ArcGIS ProPython versions
• ArcGIS Desktop uses
Python 2.7
• ArcGIS Pro uses Python
3.x
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
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
Running geoprocessing tools
with Python
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)
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”
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
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
Python error handling
• Try…Except
- Try to do something, and if an error occurs, do something else
- Like…
- Print(arcpy.GetMessages())
ArcPy functions
• Perform useful tasks
- List data:
- ListFeatureClasses
- Get data properties:
- Describe
• Enables automation of manual tasks
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)
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)
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
Script against a portal
ArcGIS API for Python
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
Four different groups
working with the
ArcGIS Python API
- Org Administrators
- Content Publishers
- GIS Analysts and Data Scientists
- Power Users/Developers
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
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
Questions?