Upload
buinguyet
View
262
Download
1
Embed Size (px)
Citation preview
Esri UC2013 . Technical Workshop . EEsrEsrEsrsrEsrsrEsrEsrEsrEsrri UUUUi U Ui Ui UUi UUUUUC20C202C20C20C20C2C2C2C20C20C200201313 1313131113 13 . T. TT. T. TTTTT. T. TTTTecheechechechechechechecheechceechechechechhchechechec cnicnicccnicnicniccnininininicccnn cccalalalalalalalalalalalalalaaaaalalalaaaa WorWorWorWorWorWorWorWorWorWorWoW rrrrW rrrrorkshkkshkshkshkshkskshkshkshkkshkshkshkkshkshkshkkshkshkshssssss opop oppppop opopop op opopo .......
Technical Workshop
2013 Esri International User ConferenceJuly 8–12, 2013 | San Diego, California
Python – Getting StartedDrew Flater, Ghislain Prince
Esri UC2013 . Technical Workshop .
Does this describe you?
New to Python scripting
Comfortable using ArcGIS but want to become more efficient
Moving to Python from other scripting language like VB, AML
Python - Getting Started
Esri UC2013 . Technical Workshop .
Agenda
What is Python and why use it?Python 101What is ArcPy?Scripting geoprocessing toolsTroubleshootingArcPy functionsBatch processing
Python - Getting Started
Esri UC2013 . Technical Workshop .
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
Scripting language of ArcGIS
Free, cross-platform, easy to learn, widely useful, great community
Python - Getting Started
Esri UC2013 . Technical Workshop .
Why use Python and ArcGIS?
Automate repetitive tasksDevelop custom toolsAdd geoprocessing to web applicationsCustomize Desktop apps Extend the capabilities of ArcGIS
Python - Getting Started
Esri UC2013 . Technical Workshop .
Python 101
Where do I write Python code?Python file is text with .py extensionIDE like PyScripter, Wing IDE ($), PythonWinPython window in ArcGIS
How do I run? …Double-click, IDE, ArcGISWhat are variables?
A name that stores a value; assigned using =
input = "C:/Data/Roads.shp"distance = 50both = [input, distance]
# Variables act as substitutes for raw valuesarcpy.Buffer_analysis(input, "Roads_buffer.shp", distance)
Python - Getting Started
Esri UC2013 . Technical Workshop .
Python 101
Python has logic for testing conditionsif, else statementColon at end of each conditionIndentation determines what is executed== tests equality; other operators like >, <, !=
var = "a"if var == "a":
# Execute indented linesprint("variable is a")
else:print("variable is not a")
Python - Getting Started
Esri UC2013 . Technical Workshop .
Python 101
Techniques for iterating or loopingWhile loops, for loopsColon at end of statementIndentation determines what is executed
x = 1while x < 5:
print(x)x = x + 1
x = [1, 2, 3, 4]for num in x:
print(num)
Python - Getting Started
Esri UC2013 . Technical Workshop .
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
math.sqrt(100) … 10
Python Standard Library / Built-insos, sys, math, datetime, urllib2
Python - Getting Started
Esri UC2013 . Technical Workshop .
ArcPy
Site package that adds ArcGIS functionality to PythonAccess to 800+ geoprocessing toolsFunctions, classes and modules
Helper functions like ListFeatureClasses, DescribeClasses that can be used to create complex objects like SpatialReference, FieldMapModules that provide specialized functionality like Mapping, SpatialAnalyst, NetworkAnalyst, DataAccess
Enhancement of arcgisscripting module (pre-10.0)Your old scripts
gs will work
Python - Getting Started
Esri UC2013 . Technical Workshop .
ArcGIS Python window
Embedded, interactive Python window Access to Python and modules within ArcGIS applicationsGreat for experimenting with Python code and learning tool syntax
Python - Getting Started
Esri UC2013 . Technical Workshop . Esri UC2013 . Technical Workshop .p30Cs U ec ca o s o .hrkWni lh. T1ri 2E ii
Run Geoprocessing tools
Demo
Esri UC2013 . Technical Workshop .
Run geoprocessing tools
import arcpy
Follow tool syntaxarcpy.toolname_toolboxalias()
Enter input and output parameters
How do I use a specific tool?Tool help pageCopy as Python Snippethelp(arcpy.Buffer_analysis)
Python - Getting Started
Esri UC2013 . Technical Workshop .
Geoprocessing environment settings
Use geoprocessing environments as global parameters
See tool help for honored environmentsProductivity and code cleanuparcpy.env
arcpy.env.workspace = "C:/Data"
arcpy.env.extent = "0 0 100 100"
arcpy.env.outputCoordinateSystem = 4326 #WKID
Python - Getting Started
Esri UC2013 . Technical Workshop . EEEsri Ui UUC20C20C20C202013 13 13 11313 3 . Techhchchchnicnicnnn alalal alal Workshop .
Troubleshooting
Why do errors occur?Incorrect tool use, typos, syntax, ...or bugs
My script doesn't work!? Help!View geoprocessing messagesUse Python error handlingDebug the script in an IDE
Python - Getting Started
Esri UC2013 . Technical Workshop .
Geoprocessing messages
Three types of messagesInfo, warning, error
Displayed in the Python window
Errors displayed in IDE
To see other messagesarcpy.GetMessages
Python - Getting Started
Esri UC2013 . Technical Workshop .
Python error handling
Try…Except…Try to do something, and if an error occurs, do something else
# Start Try blocktry:
arcpy.Buffer_analysis("Roads.shp", … )# If an error occursexcept:
# Print that Buffer failed and why
print("Buffer failed")print(arcpy.GetMessages())
Python - Getting Started
Esri UC2013 . Technical Workshop . Esri UC2013 . Technical Workshop .p30Cs U ec ca o s o .hrkWni lh. T1ri 2E ii
Use try except logic and debug a script
Troubleshooting
Demo
Esri UC2013 . Technical Workshop . Esri UC2013 . Technical Workshop .p30Cs U ec ca o s o .hrkWni lh. T1ri 2E ii
ArcPy Functions
Esri UC2013 . Technical Workshop .
ArcPy functions
Perform useful tasksList data: ListFeatureClassesGet data properties: Describe
Enables automationof manual tasks
Python - Getting Started
Esri UC2013 . Technical Workshop .
Batch processing
Automating a geoprocessing operation to run multiple times
Clip every feature class in a geodatabase to a boundaryCalculate statistics for every raster in a folder
List functions used in Python to perform batch processing
Python - Getting Started
Esri UC2013 . Technical Workshop .
arcpy.ListFeatureClasses
# Set the workspacearcpy.env.workspace = "C:/Data/FileGDB.gdb/FDs"
# Get a list of all feature classesfcList = arcpy.ListFeatureClasses()
# Print the names of the feature classesfor fc in fcList:print(fc)
Python - Getting Started
Esri UC2013 . Technical Workshop .
Getting data properties
Describe function reads data propertiesReturns an object with properties
Data type Shape typeSpatial referenceFields
# Describe a feature classdesc = arcpy.Describe("C:/Data/Roads.shp")
print(desc.shapeType)>>> "Polyline"
Python - Getting Started
Esri UC2013 . Technical Workshop . Esri UC2013 . Technical Workshop .p30Cs U ec ca o s o .hrkWni lh. T1ri 2E ii
Batch processing
Demo
Esri UC2013 . Technical Workshop .
Resources
Python - Getting Started
resources.ArcGIS.comAnalysis, Python
arcpy.wordpress.comGIS Stack Exchange, Stack Overflow
Python ReferencesPython Scripting for ArcGISby Esri PressLearning Python by LutzThe Python Standard Library by Example by Hellmannpython.org
training.esri.comesriurl.com/uc13python
More Python sessions
Thanks for your feedback! www.esri.com/ucsessionsurveys
1204 (Tuesday)1304 (Wednesday)