python scripting in arcgis10: 25 potential sources of confusion

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

Upload: others

Post on 16-Feb-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

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)

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.

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.”.

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.

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

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.

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

Presenter
Presentation Notes
Better to import module rather than from module import *

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

Python Keywords

Presenter
Presentation Notes
Python has reserved keywords that you can not use as variable names

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.

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 ‘\\....’

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.

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

Python Lists

Presenter
Presentation Notes
Remember that lists always start with element#0, and not element#11

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.

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.

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()

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

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.

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

Presenter
Presentation Notes
The geoprocessing tools are case-sensitive, but tool parameters are not case sensitive.

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!

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.

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))

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.

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!

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.

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

Presenter
Presentation Notes
The table field calculator using Python really passes the calculations to the CalculateField_management tool.

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.

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.

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.

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.

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.

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.

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…

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

Presenter
Presentation Notes
Triple-quote Python functions when using the CalculateField_managment tool

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….

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.

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

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.

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.

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.

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.

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

ESRI Resource Center

Presenter
Presentation Notes
You can download 20 excellent arpy.mapping script tools!

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