1 Getting Started With Python

38
Getting Started With Python In ArcGIS Dale Honeycutt Dale Honeycutt David Wynne

Transcript of 1 Getting Started With Python

Page 1: 1 Getting Started With Python

Getting Started With Python In ArcGISg y

Dale HoneycuttDale HoneycuttDavid Wynne

Page 2: 1 Getting Started With Python

AgendaAgendaAgendaAgenda

•• 8:30 8:30 –– 9:30: Python 101 and 9:30: Python 101 and ArcPyArcPy 101101–– Essentials of the languageEssentials of the language–– Using Using ArcPyArcPy to execute geoprocessing toolsto execute geoprocessing toolsU gU g yy g p gg p g–– Python windowPython window

•• 9:309:30–– 10:00: Script and Script tool basics10:00: Script and Script tool basicsCC–– Creating a standalone scriptCreating a standalone script

–– Creating a script toolCreating a script tool•• 10:0010:00–– 10:15: Map automation10:15: Map automationpp•• 10:15 10:15 ––10:45: Break10:45: Break•• 10:4510:45–– 11:00: Raster analysis and Map Algebra11:00: Raster analysis and Map Algebra•• 11:0011:00–– 11:45: Tool Design and Validation11:45: Tool Design and Validation

Page 3: 1 Getting Started With Python

Python 101Python 101yy

Page 4: 1 Getting Started With Python

Why Python?Why Python?Why Python?Why Python?

•• ESRI has embraced Python for ESRI has embraced Python for ArcGISArcGIS 1010•• Python is the language that fulfills the needs of our user Python is the language that fulfills the needs of our user

communitycommunitycommunitycommunity–– Easy to learnEasy to learn–– Excellent for beginners and experts Excellent for beginners and experts

S it bl f l j t ll i tS it bl f l j t ll i t–– Suitable for large projects or small scripts Suitable for large projects or small scripts –– CrossCross--platform platform

Page 5: 1 Getting Started With Python

Python 101Python 101Python 101Python 101

•• Clear, easy to read syntaxClear, easy to read syntax•• Easy to use, makes it simple to get startedEasy to use, makes it simple to get started•• Variety of basic data typesVariety of basic data types•• Variety of basic data typesVariety of basic data types

–– Including lists and dictionariesIncluding lists and dictionaries•• Comes with a large standard libraryComes with a large standard library•• Supports raising and catching exceptionsSupports raising and catching exceptions•• Code can be grouped into modules and packagesCode can be grouped into modules and packages

S t bj tS t bj t i t d ii t d i•• Supports objectSupports object--oriented programmingoriented programming

http://www.python.org/about/

Page 6: 1 Getting Started With Python

Python at 10 0Python at 10 0Python at 10.0Python at 10.0

•• ESRI has fully embraced Python as its language for ESRI has fully embraced Python as its language for automationautomation

1.1. ArcPyArcPy sitesite--packagepackage–– Includes mapping and Map Algebra supportIncludes mapping and Map Algebra support–– Successor to the Successor to the arcgisscriptingarcgisscripting modulemodule

2.2. Python windowPython window–– Python access and interaction from withinPython access and interaction from within ArcGISArcGISPython access and interaction from within Python access and interaction from within ArcGISArcGIS

3.3. Python script tool frameworkPython script tool framework

Page 7: 1 Getting Started With Python

A brief history of Python inA brief history of Python in ArcGISArcGISA brief history of Python in A brief history of Python in ArcGISArcGIS

• dispatch–based Geoprocessor• Python 2.1Python 2.1

• arcgisscripting module• Cross-platformCross platform• Python 2.4

• arcgisscripting module, 9.3 version• “Pythonic”• Python 2.5

• ArcPy site-packagey p g• Mapping & Map Algebra support• Python window• Python 2 6Python 2.6

Page 8: 1 Getting Started With Python

ArcPyArcPy 101101yy

Page 9: 1 Getting Started With Python

What isWhat is ArcPyArcPy??What is What is ArcPyArcPy??

•• A cornerstone for automation in A cornerstone for automation in ArcGISArcGIS–– data analysisdata analysis–– data conversiondata conversion–– data managementdata management–– map automationmap automation

•• ArcPyArcPy is a native Python siteis a native Python site--packagepackage–– Access to 800+Access to 800+ geoprocessinggeoprocessing toolstoolsAccess to 800 Access to 800 geoprocessinggeoprocessing toolstools–– Provides embedded reference documentation for each Provides embedded reference documentation for each

function, class and modulefunction, class and moduleCode completion forCode completion for ArcGISArcGIS components in your favoritecomponents in your favorite–– Code completion for Code completion for ArcGISArcGIS components in your favorite components in your favorite Python editorPython editor

–– Familiar to Familiar to arcgisscriptingarcgisscripting usersusers

Page 10: 1 Getting Started With Python

ArcPyArcPy improvementsimprovementsArcPyArcPy improvementsimprovements

•• Improved coding experience, such as:Improved coding experience, such as:–– CursorsCursors–– ClassesClassesCC–– MultiMulti--value parameters can be expressed as Python listsvalue parameters can be expressed as Python lists–– Ability to convert Ability to convert rastersrasters to and from to and from NumPyNumPy arraysarrays

•• ArcPyArcPy is supported by modules, including: is supported by modules, including: –– A mapping module (A mapping module (arcpy.mappingarcpy.mapping))A mapping module (A mapping module (arcpy.mappingarcpy.mapping))–– A Spatial Analyst module (arcpy.sa) to support map algebraA Spatial Analyst module (arcpy.sa) to support map algebra–– A A GeostatisticalGeostatistical Analyst module (arcpy.ga)Analyst module (arcpy.ga)

Page 11: 1 Getting Started With Python

What is the Python window?What is the Python window?What is the Python window?What is the Python window?

•• An embedded Interactive Python window within An embedded Interactive Python window within ArcGISArcGIS–– Can access Can access ArcPyArcPy, including tools and environments, including tools and environments–– Can access any other Python functionality,Can access any other Python functionality,C y y y,C y y y,–– Better code completion and intelligenceBetter code completion and intelligence

•• The Python window is for:The Python window is for:yy–– Testing ideasTesting ideas–– Experimenting with and learning PythonExperimenting with and learning Python

Simple execution of toolsSimple execution of tools–– Simple execution of toolsSimple execution of tools–– Building quick and easy workflows in PythonBuilding quick and easy workflows in Python

Page 12: 1 Getting Started With Python

Demo: Demo: A tour of A tour of ArcPyArcPy

and and the Python windowthe Python windowthe Python windowthe Python window

Page 13: 1 Getting Started With Python

Getting more helpGetting more helpGetting more helpGetting more help

•• Desktop helpDesktop help

•• Resource CenterResource Center–– http://resourcesbeta.esri.com/http://resourcesbeta.esri.com/

content/geoprocessingcontent/geoprocessingcontent/geoprocessingcontent/geoprocessing

Page 14: 1 Getting Started With Python

Running toolsRunning toolsRunning toolsRunning tools

•• Tools are accessed as functions on Tools are accessed as functions on arcpyarcpy•• Environments as properties from arcpy.env classEnvironments as properties from arcpy.env class

# ~~~ PYTHON CODE ~~~ import arcpy

# Set the workspacearcpy.env.workspace = "c:/st_Johns/GISData.gdb"

# Execute Geoprocessing toolarcpy.Intersect_analysis(["roads","urban_area","urban_roads",

5, "join"), j )

Page 15: 1 Getting Started With Python

EnvironmentsEnvironmentsEnvironmentsEnvironments

•• Script writers set the environment and tools use itScript writers set the environment and tools use it–– General settingsGeneral settings

•• Current Workspace, Output Spatial Reference, ExtentCurrent Workspace, Output Spatial Reference, Extentp p pp p p–– Raster analysis settingsRaster analysis settings

•• Cell Size, MaskCell Size, Mask–– Many moreMany more–– Many moreMany more

arcpy.env.workspacearcpy.env.workspacearcpy.env.outputCoordinateSystemarcpy.env.outputCoordinateSystemarcpy.env.extentarcpy.env.extentarcpy.env.cellSizearcpy.env.cellSizepypyarcpy.env.maskarcpy.env.mask

Page 16: 1 Getting Started With Python

Tool messagesTool messagesTool messagesTool messages

E ti t l ill d 3 t fE ti t l ill d 3 t f•• Executing a tool will produce 3 types of messages. Executing a tool will produce 3 types of messages. –– Informative messages (severity = 0)Informative messages (severity = 0)–– Warning messages (severity = 1)Warning messages (severity = 1)–– Error messages (severity = 2)Error messages (severity = 2)

# start try blocktry:

arcpy.Buffer("c:/ws/roads.shp", "c:/outws/roads10.shp", 10)

# If an error occurs when running a geoprocessing tool# If an error occurs when running a geoprocessing tool,# print the tool messagesexcept arcpy.ExecuteError:

print arcpy.GetMessages(2)

# Any other errorexcept Exception as e:

print e.messageprint e.message

Page 17: 1 Getting Started With Python

FunctionsFunctionsFunctionsFunctions

•• Functions perform useful scripting tasksFunctions perform useful scripting tasks–– Access Access geoprocessinggeoprocessing tool messages (tool messages (GetMessagesGetMessages))–– List data to for batch processing (List data to for batch processing (ListFeatureClassesListFeatureClasses, , p g (p g ( CC ,,

ListFieldsListFields, nine other List functions), nine other List functions)–– Retrieve a dataset’s properties (Retrieve a dataset’s properties (DescribeDescribe))

import arcpy

# Set the workspace for ListFeatureClasses functionarcpy env workspace = "c:/test"arcpy.env.workspace = c:/test

# For each fc, create a scratch name and clipfor fc in arcpy.ListFeatureClasses():

outName = arcpy.CreateScratchName("clipped_" + fc,"", "featureclass",arcpy.env.workspace)

arcpy Clip analysis(fc "boundary" outName)arcpy.Clip_analysis(fc, boundary , outName)

Page 18: 1 Getting Started With Python

DescribeDescribeDescribeDescribe

•• Returns an object with dynamic propertiesReturns an object with dynamic properties•• Allows script to determine properties of dataAllows script to determine properties of data

–– Data type (Data type (shapefileshapefile coverage network dataset etc)coverage network dataset etc)Data type (Data type (shapefileshapefile, coverage, network dataset, etc), coverage, network dataset, etc)–– Shape type (point, polygon, line, etc)Shape type (point, polygon, line, etc)–– Spatial referenceSpatial reference

f ff f–– Extent of featuresExtent of features–– List of fieldsList of fields

•• Logic can be added to a script to branch (if statement) Logic can be added to a script to branch (if statement) g p ( )g p ( )based data propertiesbased data properties

Page 19: 1 Getting Started With Python

DescribeDescribeDescribeDescribe

# Describe a feature classd = arcpy.Describe("c:/base.gdb/rivers")

# Branch based on the # input’s ShapeType property# p p yp p p yif d.shapeType == "Polygon":arcpy.FeatureToLine_management(inFC, outFC)lelse:arcpy.CopyFeatures_management(inFC, outFC)

Page 20: 1 Getting Started With Python

ClassesClassesClassesClasses

•• Most tool parameters can be easily definedMost tool parameters can be easily defined–– Such as a path or buffer distanceSuch as a path or buffer distance

•• Some parameters cannot be easily defined with a stringSome parameters cannot be easily defined with a stringSome parameters cannot be easily defined with a stringSome parameters cannot be easily defined with a string–– Such as a spatial reference or field mappingSuch as a spatial reference or field mapping

•• Classes can be used to define parametersClasses can be used to define parameters

prjFile = "c:/North America Equidistant Conic.prj"

# Create a spatial reference using a projection file# Create a spatial reference using a projection filespatialRef = arcpy.SpatialReference(prjFile)

# Run CreateFeatureclass using the spatial referencearcpy.CreateFeatureclass management(inputWorkspace,arcpy.CreateFeatureclass_management(inputWorkspace,

outputName, "POLYLINE", "", "", "", spatialRef)

Page 21: 1 Getting Started With Python

CursorsCursorsCursorsCursors

•• Cursors allows Cursors allows –– Iteration over the set of rows in a tableIteration over the set of rows in a table–– Ability to insert new rows into a tableAbility to insert new rows into a tableyy–– Geometry accessGeometry access

•• Cursors have three formsCursors have three formsSS–– Search, Insert and UpdateSearch, Insert and Update

import arcpy

# Print road name and road typefor row in arcpy.SearchCursor("D:/data.gdb/roads"):

print “Road name:” row.roadnamei t “R d t ” tV l (“ dt ")print “Road type:” row.getValue(“roadtype")

del row

Page 22: 1 Getting Started With Python

Demo: Demo: Converting GPS in XML Converting GPS in XML

to useable Geographic datato useable Geographic data

Page 23: 1 Getting Started With Python

A note on Cursor functionsA note on Cursor functionsA note on Cursor functionsA note on Cursor functions

•• ArcPyArcPy cursors support iterationcursors support iteration

At 9.3rows = gp.SearchCursor(myTable)row = rows.next()while row:

print row.GetValue(“Rank”)row = rows.next()

At 10for row in arcpy.SearchCursor(myTable)

print row.GetValue(“Rank”)

Page 24: 1 Getting Started With Python

Insert CursorInsert CursorInsert CursorInsert Cursor

•• Cursors support creation of new geometriesCursors support creation of new geometries# Open an insert cursor for the feature classcur = arcpy.InsertCursor(fc)

# Create array and point objects ptList = [arcpy.Point(358331, 5273193),

arcpy.Point(358337, 5272830)]lineArray = arcpy.Array(ptList)

# Create a new row for the feature classfeat = cur.newRow()

# Set the geometry of the new feature to the # array of pointsfeat.Shape = lineArrayp y

# Insert the featurecur.insertRow(feat)

# Delete objectsdel cur, feat

Page 25: 1 Getting Started With Python

Creating Python script toolsCreating Python script toolsg yg y

Page 26: 1 Getting Started With Python

PlaceholderPlaceholder——building script tool stuffbuilding script tool stuffPlaceholderPlaceholder building script tool stuffbuilding script tool stuff

Page 27: 1 Getting Started With Python

arcpy.mappingarcpy.mappingy gy g

Page 28: 1 Getting Started With Python

arcpy mappingarcpy mapping modulemodulearcpy.mappingarcpy.mapping modulemodule

•• A new mapping module that is part of the A new mapping module that is part of the GeoprocessingGeoprocessingArcPyArcPy sitesite--packagepackage

•• A python scripting API that allows you to:A python scripting API that allows you to:A python scripting API that allows you to:A python scripting API that allows you to:–– Manage map documents, layer files, and the data withinManage map documents, layer files, and the data within

•• Find a layer with data source Find a layer with data source XX and replace with and replace with YY•• Update a layer’sUpdate a layer’s symbologysymbology across many MXDsacross many MXDs•• Update a layer s Update a layer s symbologysymbology across many MXDsacross many MXDs•• Generate reports that lists document informationGenerate reports that lists document information

–– Data sources, broken layers, spatial reference, info, etc.Data sources, broken layers, spatial reference, info, etc.

Automate the exporting and printing of map documentsAutomate the exporting and printing of map documents–– Automate the exporting and printing of map documentsAutomate the exporting and printing of map documents–– Automate map production/map seriesAutomate map production/map series

Page 29: 1 Getting Started With Python

•• Modify map document properties, save changes to a layer Modify map document properties, save changes to a layer file, save changes to the map documentfile, save changes to the map document

import arcpymxd = arcpy.mapping.MapDocument("input.mxd")df = arcpy.mapping.ListDataFrames(mxd)df scale = 24000df.scale = 24000df.rotation = 2.7for lyr in arcpy.mapping.ListLayers(mxd):

if lyr.name == "Landuse":lyr.visible = Truelyr.showLabels = Truelyr.saveACopy("output.lyr")

mxd save()mxd.save()del mxd

Page 30: 1 Getting Started With Python

Demo:Demo:Demo: Demo: Map automation toolsMap automation tools

Page 31: 1 Getting Started With Python

BREAK!BREAK!

See you at 10:45See you at 10:45See you at 10:45See you at 10:45

Page 32: 1 Getting Started With Python

Raster analysis and Map AlgebraRaster analysis and Map Algebray gy g

Page 33: 1 Getting Started With Python

Raster classRaster classRaster classRaster class

•• Returned output from Spatial Analyst toolsReturned output from Spatial Analyst tools–– Used to represent a raster dataset on diskUsed to represent a raster dataset on disk–– Can be used as inputs to tools and Spatial Analyst Map Can be used as inputs to tools and Spatial Analyst Map C p Sp y pC p Sp y p

Algebra expressionsAlgebra expressions•• Supports operators (or arithmetic operations in Map Supports operators (or arithmetic operations in Map

Algebra expressions)Algebra expressions)Algebra expressions)Algebra expressions)•• Has properties and methods for analysisHas properties and methods for analysis

–– raster.minraster.min–– raster.maxraster.max–– raster.saveraster.save()()

Page 34: 1 Getting Started With Python

Spatial Analyst moduleSpatial Analyst moduleSpatial Analyst moduleSpatial Analyst module

•• Spatial Analyst module that integrates Map Algebra into Spatial Analyst module that integrates Map Algebra into PythonPython

–– Includes all Spatial Analyst toolsIncludes all Spatial Analyst toolsp yp y–– Supports operators in Map Algebra expressionsSupports operators in Map Algebra expressions–– Helper classes that can be used to support complex parameterHelper classes that can be used to support complex parameter

Output on the leftOutput on the left sideside–– Output on the leftOutput on the left--sideside

from arcpy.sa import *demm = Raster("DEM") / 3.28slpdeg = Slope(demm, "DEGREE")

demfs = FocalStatistics("DEM" NbrRectangle(3 3) "MEAN")demfs = FocalStatistics( DEM , NbrRectangle(3,3), MEAN )

Page 35: 1 Getting Started With Python

Raster IntegrationRaster IntegrationRaster IntegrationRaster Integration

dd•• NumPyNumPy is a 3is a 3rdrd party Python library for scientific computingparty Python library for scientific computing–– A powerful array object A powerful array object –– Sophisticated analysis capabilitiesSophisticated analysis capabilitiesS p y pS p y p

•• ArcPyArcPy Raster objects can be converted to Raster objects can be converted to NumPyNumPy arrays arrays for analysisfor analysis

R t T N P AR t T N P A ()() N P A T R tN P A T R t ()()–– RasterToNumPyArrayRasterToNumPyArray(), (), NumPyArrayToRasterNumPyArrayToRaster()()inras = "ras100"

# t t t N# convert raster to Numnpy array.rasArray = arcpy.RasterToNumPyArray(inras)

# ARRAY SLICING: get the total sum of every third valueg y# from every third row of the rastersampArray = rasArray[::3,::3]sum = NUM.sum(sampArray)

i tprint sum

Page 36: 1 Getting Started With Python

Demo:Demo:Working with Map AlgebraWorking with Map Algebra

and and NumPyNumPy

Page 37: 1 Getting Started With Python

Tool Design and ValidationTool Design and Validationgg

Page 38: 1 Getting Started With Python

Dale’s script tool wizardryDale’s script tool wizardry –– validationvalidationDale s script tool wizardry Dale s script tool wizardry validationvalidation