Top Banner
http://nrm.salrm.uaf.edu/~dverbyla Python Scripting in ArcGIS10: 25 Potential Sources of Confusion Dave Verbyla Professor of GIS/Remote Sensing [email protected]
42

Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

Feb 16, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

Dave Verbyla Professor of GIS/Remote Sensing

[email protected]

Presenter
Presentation Notes
Title slide (2001 AK Surveying and Mapping Conference Feb 24, 2011, Anchorage, AK)
Page 2: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Why learn ArcGIS Python

• Python scripting language installs with

ArcGIS

• Python window in ArcGIS

• Create your own script tools

• Arcpy.mapping (new with ArcGIS10)

Presenter
Presentation Notes
At version10, python is integrated into ArcGIS with a python command window, new arcpy.mapping module for working with mxd objects, etc.
Page 3: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Here is an example where we want to rename all txt files in the folder to a simpler name by replacing the month integer with a 3-character month, and stripping away the string “ak.cr.temp.”.
Page 4: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
There is typically more than one way to solve a problem with arcgis Python scripting. Both these scripts solve the problem, the script in the blue box is more efficient, but the script in the red box may be more understandable? to a beginnner.
Page 5: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Source of Confusion• Python interpreter is case sensitive

Presenter
Presentation Notes
Remember all Python commands are lower case. A common convention is to name variables in mixed case such as myTheme, aList, intVariable
Page 6: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

• Python command not understood without appropriate module loaded

Presenter
Presentation Notes
Typically you load the modules you need at the start of your script.
Page 7: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Better to import module rather than from module import *
Page 8: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Python Keywords

Presenter
Presentation Notes
Python has reserved keywords that you can not use as variable names
Page 9: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

“\” character\ is a special (\t means tab, \n means new line, etc.)

(use / or \\ for paths instead of \)

Presenter
Presentation Notes
the \ causes problems when dealing with paths and the \n newline character when reading/writing text files.
Page 10: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The backslash \ character is a special character to the Python interpreter. There are 3 different ways you can specify paths: r’\....’,’/….’, or ‘\\....’
Page 11: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Newline = “\n”• Newline character at the end of every line in a text file

Presenter
Presentation Notes
The ‘\n’ is a carriage return or new line character that is invisible in the data file when you look at it in notepad.
Page 12: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Python Lists

Presenter
Presentation Notes
Remember that lists always start with element#0, and not element#11
Page 13: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Looping using range()

Presenter
Presentation Notes
The range function syntax is range(begin, up_to) So if you want to see all 3 items, use range(0,3) in the examples here.
Page 14: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

: then indents• Indentation interpreted as loop or decision structure

Presenter
Presentation Notes
PythonWin or other editors such as Wing helps because it automatically maintains correct indentation.
Page 15: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Recycling Variables• Some Python methods returns result None

Presenter
Presentation Notes
Instead of fList = fList.sort(), it is always safer to create a new object like srtFlist = fList.sort()
Page 16: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Arcpy Site Package

• Python scripting language

• Arcpy geoprocessing

Presenter
Presentation Notes
The arcpy site package allows you access to all the geoprocessing tools and environments either in a stand-alone script or in the arcgis Python window
Page 17: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Here the fields are represented by a string, a Python list, and a an arcpy value table.
Page 18: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The geoprocessing tools are case-sensitive, but tool parameters are not case sensitive.
Page 19: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
One source of confusion is tha the Python current working directory is no the same as arcpy’s workspace!
Page 20: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Also if your running a stand-alone geoprocessing script, it will not see the environments that are set by default in Arcmap.
Page 21: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The GetCount_management tool returns the number of records as an Result object. Use the Python str(nRecs) to convert the Result to a string. If you need a numeric value then use int(str(nRecs))
Page 22: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
When working with Cursors, always remember to delete your row and Cursor objects to unlock the files so other applications can access them.
Page 23: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The field calculator VB Script computes the field Root_Length as the square root of the Shape_Length of each polyline. The same operation fails when we use Python scripting instead!
Page 24: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The error occurred because a !field_name! was referenced in the pre-logic script code….always pass field names to the Python function in the pre-logic script code.
Page 25: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The table field calculator using Python really passes the calculations to the CalculateField_management tool.
Page 26: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Script Tools• Built-in Dialogs• Filtering to prevent

errors • Output to Arcmap

Data Frame• Portable (email the

.tbx file and script)• Toolbox, toolbar, or

context menus

Presenter
Presentation Notes
A script tool is a Python script that is run from an ArcGIS application such as ArcMap or ArcCatalog using the built-in dialog that most users are familiar with. With a script tool, you can filter so the user selects for example on Polygon feature classes, output is automatic to your Arcmap data frame. You can email your personal toolbox that contains the script tool(s) and your Python scripts.
Page 27: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
You can easily test-drive a script tool to figure out what the various script tool parameters represent.
Page 28: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

#Python script to message to userimport arcpyTheme = arcpy.GetParameterAsText(0) #user selects a feature class #output using arcpy AddMessage function:Message = 'User-selected input featue class: ' + str(Theme) arcpy.AddMessage(Message)arcpy.AddMessage('arcpy AddMessage function.....Good Bye')#output using Python print command:print 'Python print command...All Done'

Presenter
Presentation Notes
Notice that the Python print statement does not appear in the output, but the arcpy.AddMessage() does appear in the ouput.
Page 29: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Here is a simple script that runs the CopyFeatures tool and displays the resulting output to Arcmap.
Page 30: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
The same geoprocessing operation does NOT display the results to Arcmap when using a script tool.
Page 31: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
One work around is to have the user specify the output feature class as a script tool parameter.
Page 32: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
You will not see a list of fields in your script tool unless you specify the parameter to obtain the fields from…
Page 33: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Triple-quote Python functions when using the CalculateField_managment tool
Page 34: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Arcpy.mapping…new with ArcGIS10. This is more object-oriented compared to other arcpy methods. Here we first make a map doucment object, then get a list of grames in the arcmap object, get the fourth data frame, make this the active frame and then use the active frame to create the map document thumbnail….
Page 35: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Another example, turning the parcels layer off. Many arcpy.mapping methods return lists, so using method( )[0] returns the first item from the list.
Page 36: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
Here the Schools layer is no longer in the city_parcels dataset, so the arcmap layer has a broken data source. We use the arcpy.mapping replaceDataSource method to specify the new data source…no problem
Page 37: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Presenter
Presentation Notes
We try the same thing with our new data source Schools.shp in c:\temp. The required parameters are workspace path (r”C:\temp”, the keyword representing type of workspace “SHAPEFILE_WORKSPACE”, and the datasetname of the new souce (“Schools.shp”). This yields a value error. The correct syntax is to specify dataset_name as “Schools” since the method already knows it is a shapefile from the “SHAPEFILE_WORKSPACE” parameter.
Page 38: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

Beginner Websites(hyperlinks)

• Non-Programmer’s Tutorial for Python 2.6• Live Wires Python Course• Instant Hacking• Dive Into Python• Python Programming for Beginners• Python 101• Learn Python in 10 Minutes• Learn Python • 5 Minutes With Python (video)• Online Python resources (video)

Presenter
Presentation Notes
There are many websites aimed at beginner Python scripting.
Page 39: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

ESRI Resources

Using Python in ArcGIS10Free Web Course http://training.esri.com• Creating basic Python scripts with correct syntax• Arcmap Python window• Python scripting in field calculator• Creating basic script tools in ArcGIS10

Getting Started With Python in ArcGIS10Video http://resources.arcgis.com/gallery/video/geoprocessing/• Arcpy site package• Python window• Script tools• Arcpy mapping automation• Tool design and validation

Presenter
Presentation Notes
The free virtual campus course is a good place to start with arcpy.
Page 40: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

ESRI Resource Center http://resources.arcgis.com/gallery/file/geoprocessing/

Presenter
Presentation Notes
The UC 2010 Technical Workshop is a good source at the Resource Center.
Page 41: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

ESRI Resource Center

Presenter
Presentation Notes
You can download 20 excellent arpy.mapping script tools!
Page 42: Python Scripting in ArcGIS10: 25 Potential Sources of Confusion

http://nrm.salrm.uaf.edu/~dverbyla

ESRI Resource Centerhttp://resources.arcgis.com/gallery/video/geoprocessing

Presenter
Presentation Notes
A good video presentation on arcpy.mapping