Python Scripting For ArcGIS - Darryl Mcleod

Transcription

Python Scripting for ArcGISPaul ZandbergenDepartmentpof Geographyg p yUniversity of New Mexico

Outline of Topics Introductiond– Examples, Python and ArcGIS, Python versions Fundamentals of geoprocessing in ArcGIS Python language fundamentals––––Where to run Python codeData types: numbers, strings, listsFunctions and modulesControllingg workflow ArcPy: Geoprocessing using Python– Using tools, functions, classes– Describingb ddata, llisting data,dworkingk withh llists Creating custom tools– Scriptp tools,, tool pparameters Resources

Workshop Materials Postedposted until October 24h //http://www.paulzandbergen.com/workshopsl db/kh

Forthcoming Book Python Scripting for ArcGISEsri PressSometime in 2012Updated for ArcGIS 10.1 Sample exercises posted (for 10.0)

Introduction

Prior Knowledge and Experience Using ArcGIS 9.3 or 10.0?– Workshopp is for 10.0 Prior Python experience?– I’m not assumingg anyy OtherO h programmingi experience?i?– I’m not assuming any

Example 1 Script to copy all shapefiles in a folder into a geodatabaseimport arcpyfrom arcpy import envenv.overwriteOutput Trueenv.workspacek "c:/workshop/ex01"" /k h / 01"fclist arcpy.ListFeatureClasses()for fc in fclist:py( )fcdesc arcpy.Describe(fc)arcpy.CopyFeatures management(fc, "c:/workshop/ex01/study.mdb/" fcdesc.basename)

Example 2 Script tool to generate a k‐nearest neighbor table Runs an existing ArcGIS tool multiple times, writes the resultimport arcpyfrom arcpy import envenv.overwriteoutputitt t TrueTinfc arcpy.GetParameterAsText(0)output arcpy.GetParameterAsText(1)k arcpy.GetParameter(2)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 1f.close()

Example 3 Script tool to run Huff model Sophisticated analysis not available in ArcGIS

Example 3

What is Python Scripting? Add functionalityfili to ArcGISA GIS––––Integrated into ArcGIS interfaceB ild upon existingBuildsi i functionalityfiliAutomates repetitive tasksExpands analysis options ShareShnew ffunctionalitytilit– Script tools work just like regular tools– CanC beb integratedi tt d iintot models,d l toolst l– Easy to share with others (free)

Why Python? Free, open sourceObject orientedBasic scripting AND complex object‐oriented programming“Batteries included”Embraced by geospatial community, including ESRIMany libraries

Python Communityhttp://www.python.org

Python and ArcGIS PythonP h iis theh preferredfd scriptingi i llanguage ffor ArcGISA GIS1. You can run Python from within ArcGIS–Python Window works like an interactive interpreter2. All tools in ArcToolbox can be accessed from Python–Import ArcPy to get full library of tools3 Python3.P th scriptsi t can beb maded iintot ttoolsl–Extend functionality of ArcGIS4 Support for other scripting languages will go away4.–VBScript and JScript being replaced by Python

Python Versions and ArcGIS Versions:––––Current version of Python is 3.2.2Python that works with ArcGIS 10.010 0 is 22.6.x6xPython that works with ArcGIS 10.1 is 2.7.xMove to Pythony3.x likelyy onlyy with ArcGIS 11 ArcGIS onlyy works with a specificpversion of Python:y– Use the one that comes installed with ArcGIS– Don’t install your own version of Python

Installing Python Remove any existing installations of Python Install ArcGIS 10.0– Python 2.6.5 will be installed by default Install a Python editor Configure the editor to work with ArcGIS Note: You can run different versions of Python on onemachine – however, a clean install of Python2.6.5with ArcGIS 10.0 is recommended

Demo: Check ArcGIS and Python installation

Fundamentals of Geoprocessingin ArcGIS

Geoprocessing Tools

Tool Organization

Tool Dialogs

Tool Parameters Parameters– Requiredq– Optional Errors Warning

Environment Settings

Geoprocessing Options

Demo: Geoprocessing Fundamentals

Running Python Code

Two ways to run Python Code1. Using an Interactive Interpreter– Code is executed directlyy line‐by‐liney2 By2.B runningi a scripti t– Code saved in a .py file– Run from within a Python editor or directly frompg systemyoperating

Where to type and run Python code?1. Python window in ArcGIS––Built into any ArcGIS Desktop applicationGood for testing code, very short scripts2. Python editor–––IDLE installed by defaultMany others, PythonWin is a good one to startGood for more complex codecode, saving scripts

Python Window in ArcGIS

Python Window in ArcGIS Works with current map document Interactive interpreter:– Executes code directly line‐by‐line Good for testing short code Code can be saved NoN error checkingh ki / ddebuggingb i

Python Editor ‐ IDLE

Python Editor ‐ PythonWin

Python Editor Stand‐alone – outside of ArcGIS Interactive interpreter:– Executes code directly line‐by‐line Save code as script files (.py)( ) Good for organizing more complex code

Demo: Running simple Python code

Python Documentation

Python Documentationhttp://www.python.org

Python DocumentationVersion specific!phttp://docs.python.org

Python Beginners Guidehttp://wiki.python.org/moin/BeginnersGuide

Python BooksVersion specific!NNoneoff ththese booksb k iincludingl di anythingthi on ArcGISA GIS or geoprocessing!i !

Python Language Fundamentals

Python Data Types Number (integer and float)StringListTupleDictionary Strings, lists and tuples are sequences Strings, numbers and tuples are immutable List and dictionaries are mutable

Numbers Integers– Whole number, i.e. no decimals– e.g. ‐34 Floats– Decimal point– e.g.g ‐34.8307

Numerical OperatorsOperatorDescription*/% nIntegerExampleResult9*2189/249%219 2119‐27Floating‐pointExampleResult9 * 2.018.09 / 2.04.59 % 2.01.09 2.011.09 – 2.07.0

Demo: Numerical Operators

Strings A set of characters surrounded by quotes is called astring literal To create a string variable, assign a string literal to it mytext "Crime hotspot maps are cool." print mytextCrime hotspot maps are cool.

Quotes in Python In Python single and double quotes are the same "NIJ" is the same as 'NIJ' print "II said: 'Let'sLet s go!'"go! Quotes in Python are straight‐up "text" or 'text', not “text” or ‘text’ Be aware of copy/paste and auto‐formatting

Variables Python scripts use variables to store information To work with variables use an assignment statement x 17 x * 234

Variables PythonP h uses dynamicdi assignmenti x 17 type(x) type 'int' int x "GIS" type(x) type 'str' No need to declare variables Value defines the type

Variable Names Rules– Letters, digits and underscores– Cannot start with a digit– Don’t use keywords (print, import, etc.) Recommendations– Be descriptive (count instead of c)– Keep it short (count instead of count of records)count of records)– Follow convention: all lowercase, use underscores

Statement and Expressions A Python expression is a value 2 * 1734 A Python statement is an instruction to do something x 2 * 17

Working with Strings Concatenate strings GISx "G"y "I"Iz "S"print x y z

Converting to String temp 100 print "The temperature is " temp " degrees"TypeError: cannot concatenate 'str'str and 'int'int objects print "The temperature is " str(temp) " degrees" Converting the value of a variable from one type to another isknown as casting

Lists A Python list is an ordered set of items The list of items is surrounded by square brackets [ ], and theititemsare separatedt dbby commas ((,)) Items can consist of numbers, strings and other data typesmylist [1, 2, 4, 8, 16, 32]mywords ["jpg",j"bmp", "tif"]i Lists are very widely used in geoprocessing:– e.g. list of feature classes, list of records, list of fields, etc.

Python 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

Python 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

String Indexing PythonP th stringstihavehan indexi d positioningiti i systemt mystring "CrimeCrime Mapping"Mapping mystring[0]'C' mystring[-1]'g' Strings can be sliced into smaller strings using slicing mystring[0:5]'Crime’

Working with List Pythonh llists hhave an indexd positioning system crimes [["arson",arson , "burglary",burglary , "robbery"]robbery ] cities[1]'burglary' There are many list methods crimes.append("homicide") crimes.remove(crimes.remove("arson")arson ) crimes['burglary', 'robbery', 'homicide']

Working with Pathnames PathnamesP hare criticali i l whenh writingi i scripts:i– Example workspace: c:\data\results– ExampleEl shapefile:h fil c:\data\results\streams.shp\d \l \h In Python a backslash (\) is an escape character Pathnames in Python should therefore look like oneof the following"c:/data""c:\\data"\\r"c:\data" (raw string)

Python Modules MModulesd l are likelik extensionsithath can beb importedid intoiPythonP hto extend its capabilities import time A typical module contains a number of specialized functionswhich can be called once the module has been imported module . function time.localtime()

Conditional Statements Branching can be used to control workflowimport randomx random.randint(0,6)print xif x 6:print "You win!“ Syntax: keyword if, followed by a condition, followed by (:)

Indentation in Python Indented code is referred to as a block Use tabs or spaces – be consistent Recommended: 4 spaces Tip: be careful with copy/paste from otherapplications

More Conditional Statements Use off elif andd else is optionallimport randomx random.randint(0,6)print xpif x 6:print "You win!"elif x 5:print "Try again!"else:print "You lose!"

Loop Structures: While LLoop structures allowll you to repeat a certaini part off yourcode A while loop repeats until a particular condition is reachedi 0while i 10:print ipi 1 The while statement uses a sentry variable in the exitcondition

Loop Structures: For A for looplrepeats a blblockk off coded ffor eachh elementloff asequencemylist ["A", "B", "C", "D"]for letter in mylist:print letter In the example, letter is the name of a variable and for eachiteration of the loop this varaible is assigned a different value

ArcPy: Geoprocessing using Python

What is ArcPy? AArcPyP was introducedid d withi h ArcGISA GIS 10.010 0 ArcPy is a collection of modules, classes andffunctionsiwhichhi h givei access to allll theh geoprocessingitools in ArcGIS from within Python MostM geoprocessingi scriptsi willill start with:ihimport arcpy Note: ArcPy replaces the older arcgisscriptingmodule

Setting Current Workspace After importing ArcPy, most scripts start with setting aworkspace to retrieve and store filesimport arcpyarcpy.env.workspacek "c:/workshop"" /k h " In theh coded abovebenv isi a classl andd workspace isi aproperty of this classarcpy. class . property

Using Tools ArcPy gives you access to all tools in ArcToolbox All tools are provided as functionsarcpy. toolname toolboxalias ( parameters ) Example:import arcpyarcpy env workspace "c:/data"arcpy.env.workspacearcpy.Clip analysis("streams.shp", "study.shp", "result.shp")

Tool Parameters A good understanding of tool parameters is essential Parameters have properties:––––NameType (feature class, integer, etc.)Direction (input or output)Required or optional Example:p Clipp tool

Tool SyntaxTool dialog:Python syntax:Clip analysis(in features, clip features,out feature class,{cluster tolerance}){cluster tolerance})Example:Clip t h ")

Optional 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},{p}, {{dissolve field})})arcpy.Buffer analysis("roads", "buffer", "100 METERS", "",LIST , "Code")Code )"", "LIST"arcpy.Buffer analysis("roads", "buffer", "100 METERS",didissolve option LIST,ltiLIST dissolve field Code)dilfi ld C d )

Hard coded ParametersHard‐coded Consider the exampleimport arcpyarcpy.env.workspace "c:/data"arcpy.Clip analysis("streams.shp", "study.shp", "result.shp") How can we make this code more usable?

Using Variables for Parametersimport arcpyarcpy.env.workspace "c:/data"infc "streams.shp"clipfc "study.shp"outfc "result.shp"arcpy.Clip analysis(infc, clipfc, outfc)

Variables Provided by a Userimport arcpyinfc arcpy.GetParameterAsText(0)clipfc arcpy.GetParameterAsText(1)outfc arcpy.GetParameterAsText(2)arcpy.Clip analysis(infc, clipfc, outfc)

Result Objects ArcPy returns the output of a tool as a Result objectimport arcpyarcpy.env.workspace "c:/data"myresult arcpy.Clip print myresult This will print the path to the output datasetc:/data/result.shp

Multiple Operations using Result Objects Result objects can be used as the input into anotherfunctionimport arcpyarcpy.env.workspace "c:/data/study.gdb"buffer arcpy.Buffer analysis("str","str buf","100 METERS")pymanagement(buffer)gcount arcpy.GetCountprint count This allows complex geoprocessing operations

ArcPy 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 syntaxarcpy. classname ( parameters )

ArcPy 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 aSpatialReference class

ArcPy Classes: Example ThThe followingf ll i examplel creates a spatiali l referencefobjectbjbased on an existing .prj file ‐ properties of this object canthen be usedimport arcpyprjfile "c:/data/streamsc:/data/streams.prjprj"spatialref arcpy.SpatialReference(prjfile)myref spatialRef.nameprint myRef This will printNAD 1983 StatePlane Florida East FIPS 0901 Feet

ArcPy Classes: Example The following example creates a spatial referenceobject and use this to define the coordinate systemof a new feature classimport arcpyout path "c:/data"out name "lines.shp"prjfile "c:/data/streams.prj"spatialref class management(out path, out name,arcpy.CreateFeatureclass management(out path,out name,"POLYLINE", "", "", "", spatialref)

ArcPy Functions All geoprocessing tools are ArcPy functions Additional ArcPy functions:– listing data– Retrievingg and settingg propertiesp p– Many more General syntaxarcpy functionname ( arguments )arcpy. functionname ( arguments )

ArcPy Functions CursorsCDescribing dataEnvironment and settingsFieldsGeneralGeneral data functionsGetting and setting parametersLicensing and installationListing dataMessaging and error handlingProgress dialogTools and toolboxes

Describing and Listing Data

Describing Data TheTh DescribeDib ffunctionti isi usedd tot determined ti propertiesti off ddatasett t General syntaximport arcpy variable arcpy.Describe( input dataset ) Example:import arcpydesc arcpy.Describe("c:/data/streams.shp")print desc.shapeTypedesc shapeType

Describing Data: Exampleimportit arcpyarcpy.env.workspace "c:/data"infc "streams.shp"clipfc "study.shp"outfc "streams clip.shp"desc arcpy.Describe(clipfc)arcpy Describe(clipfc)type desc.shapeTypeif type "Polygon":arcpy.Clip analysis(infc, clipfc, outfc)else:print "TheThe clip features are not polygonspolygons."

Listing Data LiListingti ddatat iis very common Several different list functions in rsListTablesListWorkspacesListVersions Similar logic:g– Create a list– Iterate over the list using a for loop

Listing Feature Classes ThThe ListFeatureClassesLi tF tClf ti returnsfunctionta listli t offfeature classes in the current workspace General syntax:yListFeatureClasses ({wild card}, {feature type},{feature dataset}){feature dataset}) Example:import arcpyfrom arcpy import envenv.workspace "c:/data"fclist arcpy.ListFeatureClasses()

Listing Feature Classes NoN filtering:filt ifclist arcpy.ListFeatureClasses()arcpy ListFeatureClasses() Filtering based on wild cardfclist arcpy.ListFeatureClasses("w*") Filtering based on feature typefclist arcpy.ListFeatureClasses("", "point")

Listing Fields Theh ListFields functionflistsltheh fieldsf ld in a featurefclassl ortable in a specified dataset. GeneralGl syntax:tListFields (dataset,(dataset {wild{wild card},card} {field{field type})type}) Examplepimport arcpyarcpy.env.workspace "c:/data"fieldlist arcpy.ListFields("roads.shp")

Using Lists in for loops The following script creates a list of fields of typeString and determines for each text field what thelength of the field isimport arcpyarcpy.env.workspace "c:/data"fieldlist arcpy.ListFields("roads.shp", "","String")for field in fieldlist:print field.name " " str(field.length)

Using Lists in for loops Theh ffollowingll i scripti creates a listli off TIFF filesfil andditerates through each file in the list to buildpyramidsidimport arcpyfrom arcpy import envenv.workspace "c:/data"tifflist arcpy.ListRasters("","TIF")for tiff in tifflist:arcpy.BuildParamids management(tiff)

Creating Custom Tools

Ways to Execute a Script1 As1.A a stand‐aloned lscripti– The script is executed from the operating system or fromwithin a Python editor such as PythonWin– When using ArcPy, ArcGIS needs to be installed andlicensed– No ArcGIS Desktop application needs to be open2. As a script tool within ArcGIS–––A tool dialog is created to execute the scriptScript tool looks like any other tool in ArcToolboxTool execution is controlled from ArcGIS Desktopp

Python Scripts as Tools

Why Create Script Tools? Tool dialog makes it easier to useTool dialog validates user inputsBecomes part of all geoprocessingEnvironment settings are passed onWrites messages to the Results windowEasier to shareDoes not require user to know Python

Steps to Create Script Tools1.2.3.4.Create a Python script (.py)Create a custom Toolbox (.tbx)Add a tool to the Toolbox using Add ScriptModify the script with inputs and outputs

Example Script: Hardcoded Variablesimportimpot arcpya cpfrom arcpy import envenv.overwriteoutput Trueinfc "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 1f.close()

Tool Parameters and Dialog

Example Script: User Provided Parametersimportimpot arcpya cpfrom 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 1f.close()

More ArcPy Functionality

More ArcPy Functionality Cursors to work with rows and geometry– Retrieve, edit, create arcpy.sa module to work with rasters aarcpy.mappingcpy. app g module for map automation Creating custom functions and classes

Resources for Python Scripting in ArcGIS

ArcGIS Desktop Help

Virtual Campus Courseshttp://training.esri.com

ArcScriptshttp://arcscripts.esri.com

ArcGIS Resource Centerhttp://resources.arcgis.com

ArcGIS Resource ssing/10.0/about

Beyond ArcGIS

Using PySAL for Spatial Analysishttp://geodacenter.asu.edu/pysal

PySAL Python library of spatial analysis methods ESDA, spatial statistics, geostatistics Growing and expandable

Using R for Spatial Analysis Open source language for data analysisLibraries have been developed for spatial methodsLarge and active user communityGrowing and expandable

ArcGIS and R

Script Tool

Python script that calls R

Evaluating R Statements

Concluding Remarks Python is a relatively easy to learn language ArcGIS is becoming more “Pythonesque” Creating time‐savings scripts for repetitive tasks doesnott ttakek a llott off coded Easy to share script tools

Paul ZandbergenDepartment of Geographyzandberg@unm.edudb @dwww.paulzandbergen.compg

Workshop Materials Postedposted until October 24h //http://www.paulzandbergen.com/workshopsl db/kh

Python and ArcGIS PhPython is the prefdferred scriiipting language for AGISArcGIS 1. You can run Python from within ArcGIS – Python Window works like an interactive interpreter 2. All tools in ArcToolbox can be accessed from Pytho