python scripting 2011
TRANSCRIPT
![Page 1: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/1.jpg)
Python Scripting for ArcGIS
Paul ZandbergenDepartment of Geographyp g p yUniversity of New Mexico
![Page 2: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/2.jpg)
Outline of TopicsOutline of Topicsd• Introduction
– Examples, Python and ArcGIS, Python versions
• Fundamentals of geoprocessing in ArcGISFundamentals of geoprocessing in ArcGIS• Python language fundamentals
– Where to run Python code– Data types: numbers, strings, lists– Functions and modules– Controlling workflowg
• ArcPy: Geoprocessing using Python– Using tools, functions, classes
b d l d k h l– Describing data, listing data, working with lists
• Creating custom tools– Script tools, tool parametersp , p
• Resources
![Page 3: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/3.jpg)
Workshop Materials PostedWorkshop Materials Posted
posted until October 24
h // l db / k h
posted until October 24
http://www.paulzandbergen.com/workshops
![Page 4: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/4.jpg)
Forthcoming BookForthcoming Book
• Python Scripting for ArcGIS• Esri PressEsri Press• Sometime in 2012• Updated for ArcGIS 10.1
• Sample exercises posted (for 10.0)
![Page 5: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/5.jpg)
Introduction
![Page 6: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/6.jpg)
Prior Knowledge and ExperiencePrior Knowledge and Experience
• Using ArcGIS 9.3 or 10.0?– Workshop is for 10.0p
• Prior Python experience?– I’m not assuming anyg y
O h i i ?• Other programming experience?– I’m not assuming any
![Page 7: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/7.jpg)
Example 1Example 1
• Script to copy all shapefiles in a folder into a geodatabase
import arcpyfrom arcpy import envenv.overwriteOutput = True
k " / k h / 01"env.workspace = "c:/workshop/ex01"fclist = arcpy.ListFeatureClasses()for fc in fclist:
fcdesc = arcpy.Describe(fc)py ( )arcpy.CopyFeatures_management(fc, "c:/workshop/ex01/study.mdb/"
+ fcdesc.basename)
![Page 8: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/8.jpg)
Example 2Example 2
• Script tool to generate a k‐nearest neighbor table• Runs an existing ArcGIS tool multiple times, writes the result
import arcpyfrom arcpy import env
it t t Tenv.overwriteoutput = Trueinfc = arcpy.GetParameterAsText(0)output = arcpy.GetParameterAsText(1)k = arcpy GetParameter(2)k = arcpy.GetParameter(2)n = 1f = open(output, "w")while n <= k:while n < k:
result = arcpy.CalculateDistanceBand_stats(infc, n)f.write(str(n) + " " + str(result[1])+ "\n")n = n + 1n n + 1
f.close()
![Page 9: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/9.jpg)
Example 3Example 3
• Script tool to run Huff model• Sophisticated analysis not available in ArcGIS
![Page 10: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/10.jpg)
Example 3Example 3
![Page 11: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/11.jpg)
What is Python Scripting?What is Python Scripting?
Add f i li A GIS• Add functionality to ArcGIS– Integrated into ArcGIS interfaceB ild i i f i li– Builds upon existing functionality
– Automates repetitive tasksExpands analysis options– Expands analysis options
Sh f ti lit• Share new functionality– Script tools work just like regular toolsC b i t t d i t d l t l– Can be integrated into models, tools
– Easy to share with others (free)
![Page 12: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/12.jpg)
Why Python?Why Python?
• Free, open source• Object oriented• Basic scripting AND complex object‐oriented programming• “Batteries included”• Embraced by geospatial community, including ESRI• Many libraries
![Page 13: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/13.jpg)
Python CommunityPython Community
http://www.python.org
![Page 14: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/14.jpg)
Python and ArcGISPython and ArcGIS
P h i h f d i i l f A GIS• Python is the preferred scripting language for ArcGIS
1. You can run Python from within ArcGIS– Python Window works like an interactive interpreter
2. All tools in ArcToolbox can be accessed from Python– Import ArcPy to get full library of tools
3 P th i t b d i t t l3. Python scripts can be made into tools– Extend functionality of ArcGIS
4 Support for other scripting languages will go away4. Support for other scripting languages will go away– VBScript and JScript being replaced by Python
![Page 15: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/15.jpg)
Python Versions and ArcGISPython Versions and ArcGIS
• Versions:– Current version of Python is 3.2.2
Python that works with ArcGIS 10 0 is 2 6 x– Python that works with ArcGIS 10.0 is 2.6.x– Python that works with ArcGIS 10.1 is 2.7.x– Move to Python 3.x likely only with ArcGIS 11y y y
• ArcGIS only works with a specific version of Python:y p y– Use the one that comes installed with ArcGIS– Don’t install your own version of Python
![Page 16: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/16.jpg)
Installing PythonInstalling Python
• Remove any existing installations of Python• Install ArcGIS 10.0
– Python 2.6.5 will be installed by default
• Install a Python editorInstall a Python editor• Configure the editor to work with ArcGIS
• Note: You can run different versions of Python on one machine – however, a clean install of Python2.6.5 with ArcGIS 10.0 is recommended
![Page 17: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/17.jpg)
Demo: Check ArcGIS and Python installation
![Page 18: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/18.jpg)
Fundamentals of GeoprocessingFundamentals of Geoprocessingin ArcGIS
![Page 19: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/19.jpg)
Geoprocessing ToolsGeoprocessing Tools
![Page 20: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/20.jpg)
Tool OrganizationTool Organization
![Page 21: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/21.jpg)
Tool DialogsTool Dialogs
![Page 22: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/22.jpg)
Tool ParametersTool Parameters
• Parameters– Requiredq– Optional
• Errors• Warning
![Page 23: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/23.jpg)
Environment SettingsEnvironment Settings
![Page 24: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/24.jpg)
Geoprocessing OptionsGeoprocessing Options
![Page 25: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/25.jpg)
Demo: Geoprocessing Fundamentals
![Page 26: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/26.jpg)
Running Python Code
![Page 27: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/27.jpg)
Two ways to run Python CodeTwo ways to run Python Code
1. Using an Interactive Interpreter– Code is executed directly line‐by‐liney y
2 B i i t2. By running a script– Code saved in a .py file– Run from within a Python editor or directly from
operating systemp g y
![Page 28: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/28.jpg)
Where to type and run Python code?Where to type and run Python code?
1. Python window in ArcGIS– Built into any ArcGIS Desktop application– Good for testing code, very short scripts
2. Python editor– IDLE installed by defaultIDLE installed by default– Many others, PythonWin is a good one to start– Good for more complex code saving scripts– Good for more complex code, saving scripts
![Page 29: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/29.jpg)
Python Window in ArcGISPython Window in ArcGIS
![Page 30: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/30.jpg)
Python Window in ArcGISPython Window in ArcGIS
• Works with current map document• Interactive interpreter:
– Executes code directly line‐by‐line
• Good for testing short codeGood for testing short code• Code can be savedN h ki / d b i• No error checking / debugging
![Page 31: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/31.jpg)
Python Editor IDLEPython Editor ‐ IDLE
![Page 32: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/32.jpg)
Python Editor PythonWinPython Editor ‐ PythonWin
![Page 33: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/33.jpg)
Python EditorPython Editor
• Stand‐alone – outside of ArcGIS• Interactive interpreter:Interactive interpreter:
– Executes code directly line‐by‐line
( )• Save code as script files (.py)• Good for organizing more complex codeGood for organizing more complex code
![Page 34: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/34.jpg)
Demo: Running simple Python code
![Page 35: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/35.jpg)
Python Documentation
![Page 36: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/36.jpg)
Python DocumentationPython Documentation
http://www.python.org
![Page 37: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/37.jpg)
Python DocumentationPython DocumentationVersion specific!p
http://docs.python.org
![Page 38: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/38.jpg)
Python Beginners GuidePython Beginners Guide
http://wiki.python.org/moin/BeginnersGuide
![Page 39: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/39.jpg)
Python BooksPython BooksVersion specific!Version specific!
N f th b k i l di thi A GIS i !None of these books including anything on ArcGIS or geoprocessing!
![Page 40: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/40.jpg)
Python Language Fundamentals
![Page 41: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/41.jpg)
Python Data TypesPython Data Types
• Number (integer and float)• String• List• Tuple• Dictionary
• Strings, lists and tuples are sequences• Strings, numbers and tuples are immutable• List and dictionaries are mutable
![Page 42: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/42.jpg)
NumbersNumbers
• Integers– Whole number, i.e. no decimals– e.g. ‐34
• Floats– Decimal point– e.g. ‐34.8307g
![Page 43: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/43.jpg)
Numerical OperatorsNumerical Operators
Operator Description Integer Floating‐pointExample Result Example Result
* Multiplication 9 * 2 18 9 * 2.0 18.0/%+
DivisionModulusAddition
9 / 29 % 29 + 2
4111
9 / 2.09 % 2.09 + 2.0
4.51.011.0
‐ Subtraction 9 ‐ 2 7 9 – 2.0 7.0
![Page 44: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/44.jpg)
Demo: Numerical Operators
![Page 45: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/45.jpg)
StringsStrings
• A set of characters surrounded by quotes is called a string literal
• To create a string variable, assign a string literal to it
>>> mytext = "Crime hotspot maps are cool.">>> print mytext>>> print mytextCrime hotspot maps are cool.
![Page 46: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/46.jpg)
Quotes in PythonQuotes in Python
• In Python single and double quotes are the same• "NIJ" is the same as 'NIJ'
>>> print "I said: 'Let's go!'">>> print I said: Let s go!
• Quotes in Python are straight‐up• "text" or 'text', not “text” or ‘text’
• Be aware of copy/paste and auto‐formattingBe aware of copy/paste and auto‐formatting
![Page 47: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/47.jpg)
VariablesVariables
• Python scripts use variables to store information• To work with variables use an assignment statement
>>> x = 17>>> x 17>>> x * 234
![Page 48: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/48.jpg)
VariablesVariablesP h d i i• Python uses dynamic assignment
>>> x 17>>> x = 17>>> type(x)<type 'int'><type int >>>> x = "GIS">>> type(x)>>> type(x)<type 'str'>
• No need to declare variables• Value defines the type
![Page 49: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/49.jpg)
Variable NamesVariable Names
• Rules– Letters, digits and underscores– Cannot start with a digit– Don’t use keywords (print, import, etc.)
• RecommendationsRecommendations– Be descriptive (count instead of c)– Keep it short (count instead of count of records)Keep it short (count instead of count_of_records)– Follow convention: all lowercase, use underscores
![Page 50: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/50.jpg)
Statement and ExpressionsStatement and Expressions
• A Python expression is a value
>>> 2 * 173434
• A Python statement is an instruction to do something
>>> x = 2 * 17
![Page 51: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/51.jpg)
Working with StringsWorking with Strings
• Concatenate strings
>>> x = "G">>> y = "I">>> y I >>> z = "S">>> print x + y + zGIS
![Page 52: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/52.jpg)
Converting to StringConverting to String100>>> temp = 100
>>> print "The temperature is " + temp + " degrees"TypeError: cannot concatenate 'str' and 'int' objectsTypeError: cannot concatenate str and int objects
>>print "The temperature is " + str(temp) + " degrees"
• Converting the value of a variable from one type to another is known as casting
![Page 53: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/53.jpg)
ListsLists
• A Python list is an ordered set of items• The list of items is surrounded by square brackets [ ], and the
it t d b ( )items are separated by commas (,)• Items can consist of numbers, strings and other data types
mylist = [1, 2, 4, 8, 16, 32]j imywords ["jpg", "bmp", "tif"]
• Lists are very widely used in geoprocessing:– e.g. list of feature classes, list of records, list of fields, etc.
![Page 54: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/54.jpg)
Python FunctionsPython Functions
• A function carries out a certain action• Python has many built‐in functions
<function>(<arguments>)
>> pow(2,3)8
• Using a function is referred to as calling a function• Additional functions can be accessed using modules
![Page 55: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/55.jpg)
Python MethodsPython Methods
• A method is a function that is closely coupled to some object
<object>.<method>(<arguments>)
>>> topic = "Crime Mapping">>> topic.count("i")2
• Many of Python’s data types have methods
![Page 56: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/56.jpg)
String IndexingString IndexingP th t i h i d iti i t• Python strings have an index positioning system
>>> mystring = "Crime Mapping">>> mystring Crime Mapping>>> mystring[0]'C'>>> mystring[-1]'g'
• Strings can be sliced into smaller strings using slicing
>>> mystring[0:5]'Crime’
![Page 57: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/57.jpg)
Working with ListWorking with Listh l h d• Python lists have an index positioning system
>>> crimes = ["arson", "burglary", "robbery"]>>> crimes [ arson , burglary , robbery ]>>> cities[1]'burglary'
• There are many list methods
>>> crimes.append("homicide")>>> crimes.remove("arson")>>> crimes.remove( arson )>>> crimes['burglary', 'robbery', 'homicide']
![Page 58: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/58.jpg)
Working with PathnamesWorking with Pathnames
P h i i l h i i i• Pathnames are critical when writing scripts:– Example workspace: c:\data\resultsE l h fil \d \ l \ h– Example shapefile: c:\data\results\streams.shp
• In Python a backslash (\) is an escape character• Pathnames in Python should therefore look like one of the following
"c:/data"\\"c:\\data"
r"c:\data" (raw string)
![Page 59: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/59.jpg)
Python ModulesPython ModulesM d l lik i h b i d i P h• Modules are like extensions that can be imported into Python to extend its capabilities
>>> import time
• A typical module contains a number of specialized functions which can be called once the module has been imported
<module>.<function>
>>> time.localtime()
![Page 60: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/60.jpg)
Conditional StatementsConditional Statements
• Branching can be used to control workflow
import randomx = random.randint(0,6)print xif x == 6:
print = "You win!“
• Syntax: keyword if, followed by a condition, followed by (:)
![Page 61: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/61.jpg)
Indentation in PythonIndentation in Python
• Indented code is referred to as a block• Use tabs or spaces – be consistentUse tabs or spaces be consistent• Recommended: 4 spaces
• Tip: be careful with copy/paste from otherTip: be careful with copy/paste from other applications
![Page 62: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/62.jpg)
More Conditional StatementsMore Conditional Statements
f d l• Use of elif and else is optional
import randomimport randomx = random.randint(0,6)print xpif x == 6:
print "You win!" elif x == 5:
print "Try again!" else:else:
print "You lose!"
![Page 63: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/63.jpg)
Loop Structures: WhileLoop Structures: WhileL ll i f• Loop structures allow you to repeat a certain part of your code
• A while loop repeats until a particular condition is reachedA while loop repeats until a particular condition is reached
i = 00while i <= 10:
print ipi += 1
• The while statement uses a sentry variable in the exit condition
![Page 64: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/64.jpg)
Loop Structures: ForLoop Structures: For
l bl k f d f h l f• A for loop repeats a block of code for each element of a sequence
mylist = ["A", "B", "C", "D"]for letter in mylist:for letter in mylist:
print letter
• In the example, letter is the name of a variable and for each iteration of the loop this varaible is assigned a different value
![Page 65: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/65.jpg)
ArcPy: Geoprocessing using Python
![Page 66: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/66.jpg)
What is ArcPy?What is ArcPy?
A P i d d i h A GIS 10 0• ArcPy was introduced with ArcGIS 10.0• ArcPy is a collection of modules, classes and f i hi h i ll h ifunctions which give access to all the geoprocessingtools in ArcGIS from within PythonM i i ill i h• Most geoprocessing scripts will start with:
import arcpy
• Note: ArcPy replaces the older arcgisscriptingmodule
![Page 67: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/67.jpg)
Setting Current WorkspaceSetting Current Workspace
• After importing ArcPy, most scripts start with setting a workspace to retrieve and store files
import arcpyk " / k h "arcpy.env.workspace = "c:/workshop"
h d b i l d i• In the code above env is a class and workspace is a property of this class
arcpy.<class>.<property>
![Page 68: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/68.jpg)
Using ToolsUsing Tools
• ArcPy gives you access to all tools in ArcToolbox• All tools are provided as functions
arcpy.<toolname_toolboxalias>(<parameters>)
• Example:
import arcpyarcpy env workspace "c:/data"arcpy.env.workspace = "c:/data"arcpy.Clip_analysis("streams.shp", "study.shp", "result.shp")
![Page 69: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/69.jpg)
Tool ParametersTool Parameters
• A good understanding of tool parameters is essential• Parameters have properties:
– Name– Type (feature class, integer, etc.)– Direction (input or output)Direction (input or output)– Required or optional
• Example: Clip toolp p
![Page 70: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/70.jpg)
Tool SyntaxTool Syntax
Tool dialog:
Python syntax: Clip_analysis(in_features, clip_features,out_feature_class,{cluster tolerance}){cluster_tolerance})
Example: Clip_analysis("streams.shp","study.shp"," lt h ")"result.shp")
![Page 71: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/71.jpg)
Optional ParametersOptional Parameters
• Required tool parameters are listed first• Optional tool parameters can be left out
– But what if some need to be set?
Buffer_analysis (in_features, out_feature_classbuffer_distance_or_field, {line_side}, {line_end_type}, {dissolve option}, {dissolve field}){ _ p }, { _ })
arcpy.Buffer_analysis("roads", "buffer", "100 METERS", "", "" "LIST" "Code"), LIST , Code )
arcpy.Buffer_analysis("roads", "buffer", "100 METERS", di l ti LIST di l fi ld C d )dissolve_option=LIST, dissolve_field=Code)
![Page 72: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/72.jpg)
Hard coded ParametersHard‐coded Parameters
• Consider the example
import arcpyimport arcpyarcpy.env.workspace = "c:/data"arcpy.Clip_analysis("streams.shp", "study.shp", "result.shp")
• How can we make this code more usable?
![Page 73: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/73.jpg)
Using Variables for ParametersUsing Variables for Parameters
import arcpyarcpy.env.workspace = "c:/data"infc = "streams.shp"clipfc = "study.shp"outfc = "result.shp"arcpy.Clip_analysis(infc, clipfc, outfc)
![Page 74: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/74.jpg)
Variables Provided by a UserVariables Provided by a User
import arcpyinfc = arcpy.GetParameterAsText(0)clipfc = arcpy.GetParameterAsText(1)outfc = arcpy.GetParameterAsText(2)arcpy.Clip_analysis(infc, clipfc, outfc)
![Page 75: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/75.jpg)
Result ObjectsResult Objects
• ArcPy returns the output of a tool as a Result object
import arcpyarcpy.env.workspace = "c:/data"myresult = arcpy.Clip_analysis("streams.shp“,"study.shp","result.shp")print myresult
• This will print the path to the output dataset
c:/data/result.shp
![Page 76: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/76.jpg)
Multiple Operations using Result ObjectsMultiple Operations using Result Objects
• Result objects can be used as the input into another function
import arcpyarcpy.env.workspace = "c:/data/study.gdb"buffer = arcpy.Buffer_analysis("str","str_buf","100 METERS")count = arcpy.GetCount management(buffer)py _ gprint count
• This allows complex geoprocessing operations
![Page 77: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/77.jpg)
ArcPy ClassesArcPy Classes
• Some tool parameters are complicated/detailed– e.g. coordinate system
• ArcPy classes are used to work with these parameters– Classes are used to create objectsj– Classes have properties and methods
• General syntax
arcpy.<classname>(<parameters>)
![Page 78: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/78.jpg)
ArcPy Classes: ExampleArcPy Classes: Example
• The following is an example of the contents of a .prj file
• To avoid having to work with this actual string, we can use a SpatialReference class
![Page 79: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/79.jpg)
ArcPy Classes: ExampleArcPy Classes: ExampleTh f ll i l i l f bj• The following example creates a spatial reference object based on an existing .prj file ‐ properties of this object can then be used
import arcpyprjfile = "c:/data/streams prj"prjfile = c:/data/streams.prjspatialref = arcpy.SpatialReference(prjfile)myref = spatialRef.nameprint myRefprint myRef
• This will print
NAD_1983_StatePlane_Florida_East_FIPS_0901_Feet
![Page 80: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/80.jpg)
ArcPy Classes: ExampleArcPy Classes: Example
• The following example creates a spatial reference object and use this to define the coordinate system of a new feature class
import arcpyimport arcpyout_path = "c:/data"out_name = "lines.shp"prjfile = "c:/data/streams.prj"spatialref = arcpy.SpatialReference(prjfile)arcpy.CreateFeatureclass management(out path, out name,arcpy.CreateFeatureclass_management(out_path, out_name,
"POLYLINE", "", "", "", spatialref)
![Page 81: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/81.jpg)
ArcPy FunctionsArcPy Functions
• All geoprocessing tools are ArcPy functions• Additional ArcPy functions:
– listing data– Retrieving and setting propertiesg g p p– Many more…
• General syntaxGeneral syntax
arcpy <functionname>(<arguments>)arcpy.<functionname>(<arguments>)
![Page 82: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/82.jpg)
ArcPy FunctionsArcPy FunctionsC• Cursors
• Describing data• Environment and settings• Fields• General• General data functions• General data functions• Getting and setting parameters• Licensing and installation• Listing data• Messaging and error handling• Progress dialogProgress dialog• Tools and toolboxes
![Page 83: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/83.jpg)
Describing and Listing Data
![Page 84: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/84.jpg)
Describing DataDescribing DataTh D ib f ti i d t d t i ti f d t t• The Describe function is used to determine properties of dataset
• General syntax
import arcpy<variable> = arcpy.Describe(<input dataset>)
• Example:
import arcpydesc = arcpy.Describe("c:/data/streams.shp")print desc shapeTypeprint desc.shapeType
![Page 85: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/85.jpg)
Describing Data: ExampleDescribing Data: Examplei timport arcpyarcpy.env.workspace = "c:/data"infc = "streams.shp"clipfc = "study.shp"outfc = "streams_clip.shp"desc = arcpy Describe(clipfc)desc = arcpy.Describe(clipfc)type = desc.shapeTypeif type == "Polygon":
arcpy.Clip_analysis(infc, clipfc, outfc)else:
print "The clip features are not polygons "print The clip features are not polygons.
![Page 86: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/86.jpg)
Listing DataListing DataLi ti d t i• Listing data is very common
• Several different list functions in ArcPy– ListFields– ListIndexes– ListDataset– ListFeatureClasses– ListFiles– ListRasters– ListTables– ListWorkspaces– ListVersions
• Similar logic:g– Create a list– Iterate over the list using a for loop
![Page 87: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/87.jpg)
Listing Feature ClassesListing Feature ClassesTh Li tF t Cl f ti t li t f• The ListFeatureClasses function returns a list of feature classes in the current workspace
• General syntax:y
ListFeatureClasses ({wild_card}, {feature_type},{feature dataset}){feature_dataset})
• Example:
import arcpyfrom arcpy import envenv.workspace = "c:/data"fclist = arcpy.ListFeatureClasses()
![Page 88: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/88.jpg)
Listing Feature ClassesListing Feature ClassesN filt i• No filtering:
fclist = arcpy ListFeatureClasses()fclist = arcpy.ListFeatureClasses()
• Filtering based on wild cardFiltering based on wild card
fclist = arcpy.ListFeatureClasses("w*")
• Filtering based on feature type
fclist = arcpy.ListFeatureClasses("", "point")
![Page 89: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/89.jpg)
Listing FieldsListing Fields
h f l h f ld f l• The ListFields function lists the fields in a feature class or table in a specified dataset. G l t• General syntax:
ListFields (dataset {wild card} {field type})ListFields (dataset, {wild_card}, {field_type})
• Examplep
import arcpyarcpy.env.workspace = "c:/data"fieldlist = arcpy.ListFields("roads.shp")
![Page 90: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/90.jpg)
Using Lists in for loopsUsing Lists in for loops
• The following script creates a list of fields of type String and determines for each text field what the length of the field is
import arcpyarcpy.env.workspace = "c:/data"fieldlist = arcpy.ListFields("roads.shp", "",
"String")for field in fieldlist:
print field.name + " " + str(field.length)
![Page 91: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/91.jpg)
Using Lists in for loopsUsing Lists in for loops
h f ll i i li f fil d• The following script creates a list of TIFF files and iterates through each file in the list to build
idpyramids
import arcpyfrom arcpy import envenv.workspace = "c:/data"tifflist = arcpy.ListRasters("","TIF")for tiff in tifflist:
arcpy.BuildParamids_management(tiff)
![Page 92: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/92.jpg)
Creating Custom Tools
![Page 93: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/93.jpg)
Ways to Execute a ScriptWays to Execute a Script
1 A d l i1. As a stand‐alone script– The script is executed from the operating system or from within a Python editor such as PythonWinwithin a Python editor such as PythonWin
– When using ArcPy, ArcGIS needs to be installed and licensed
– No ArcGIS Desktop application needs to be open
2. As a script tool within ArcGIS– A tool dialog is created to execute the scriptA tool dialog is created to execute the script– Script tool looks like any other tool in ArcToolbox– Tool execution is controlled from ArcGIS Desktopp
![Page 94: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/94.jpg)
Python Scripts as ToolsPython Scripts as Tools
![Page 95: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/95.jpg)
Why Create Script Tools?Why Create Script Tools?
• Tool dialog makes it easier to use• Tool dialog validates user inputs• Becomes part of all geoprocessing• Environment settings are passed on• Environment settings are passed on• Writes messages to the Results window• Easier to share• Does not require user to know Python
![Page 96: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/96.jpg)
Steps to Create Script ToolsSteps to Create Script Tools
1. Create a Python script (.py)2. Create a custom Toolbox (.tbx)2. Create a custom Toolbox (.tbx)3. Add a tool to the Toolbox using Add Script4. Modify the script with inputs and outputs
![Page 97: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/97.jpg)
Example Script: Hardcoded VariablesExample Script: Hardcoded Variables
impo t a cpimport arcpyfrom arcpy import envenv.overwriteoutput = True
/ /infc = "c:/data/points.shp"output = "c:/data/result.txt"k = 10n = 1f = open(output, "w")while n <= k:
result = arcpy.CalculateDistanceBand_stats(infc, n)f.write(str(n) + " " + str(result[1])+ "\n")n = n + 1n n + 1
f.close()
![Page 98: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/98.jpg)
Tool Parameters and DialogTool Parameters and Dialog
![Page 99: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/99.jpg)
Example Script: User Provided ParametersExample Script: User Provided Parameters
impo t a cpimport arcpyfrom arcpy import envenv.overwriteoutput = Trueinfc = arcpy.GetParameterAsText(0)output = arcpy.GetParameterAsText(1)k = arcpy.GetParameter(2)n = 1f = open(output, "w")while n <= k:
result = arcpy.CalculateDistanceBand_stats(infc, n)f.write(str(n) + " " + str(result[1])+ "\n")n = n + 1n n + 1
f.close()
![Page 100: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/100.jpg)
More ArcPy Functionality
![Page 101: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/101.jpg)
More ArcPy FunctionalityMore ArcPy Functionality
• Cursors to work with rows and geometry– Retrieve, edit, create
• arcpy.samodule to work with rasters• arcpy.mappingmodule for map automationa cpy. app gmodule for map automation• Creating custom functions and classes
![Page 102: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/102.jpg)
Resources for Python Scripting in ArcGIS
![Page 103: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/103.jpg)
ArcGIS Desktop HelpArcGIS Desktop Help
![Page 104: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/104.jpg)
Virtual Campus CoursesVirtual Campus Courses
http://training.esri.com
![Page 105: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/105.jpg)
ArcScriptsArcScripts
http://arcscripts.esri.com
![Page 106: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/106.jpg)
ArcGIS Resource CenterArcGIS Resource Center
http://resources.arcgis.com
![Page 107: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/107.jpg)
ArcGIS Resource CenterArcGIS Resource Center
http://resources.arcgis.com/content/geoprocessing/10.0/about
![Page 108: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/108.jpg)
Beyond ArcGIS
![Page 109: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/109.jpg)
Using PySAL for Spatial AnalysisUsing PySAL for Spatial Analysis
http://geodacenter.asu.edu/pysal
![Page 110: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/110.jpg)
PySALPySAL
• Python library of spatial analysis methods• ESDA, spatial statistics, geostatisticsESDA, spatial statistics, geostatistics• Growing and expandable
![Page 111: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/111.jpg)
Using R for Spatial AnalysisUsing R for Spatial Analysis
• Open source language for data analysis• Libraries have been developed for spatial methods• Large and active user community• Growing and expandable• Growing and expandable
![Page 112: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/112.jpg)
ArcGIS and RArcGIS and R
![Page 113: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/113.jpg)
Script ToolScript Tool
![Page 114: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/114.jpg)
Python script that calls RPython script that calls R
![Page 115: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/115.jpg)
Evaluating R StatementsEvaluating R Statements
![Page 116: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/116.jpg)
Concluding RemarksConcluding Remarks
• Python is a relatively easy to learn language
• ArcGIS is becoming more “Pythonesque”
• Creating time‐savings scripts for repetitive tasks does t t k l t f dnot take a lot of code
• Easy to share script tools
![Page 118: Python Scripting 2011](https://reader033.vdocuments.net/reader033/viewer/2022050908/55cf9ced550346d033ab9130/html5/thumbnails/118.jpg)
Workshop Materials PostedWorkshop Materials Posted
posted until October 24
h // l db / k h
posted until October 24
http://www.paulzandbergen.com/workshops