Download presentation
Presentation is loading. Please wait.
Published byAnthony Horton Modified over 5 years ago
1
arcpy Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
2
This lecture Introduction to arcpy Debugging Using arcpy
3
Application programming
One use of Python is as an extension language; that is, one to script other software. Others include: Visual Basic for Applications (VBA); Lua. Generally extension languages work in two ways: as scripts or as GUI elements that have event listening functions overridden. Usually access is provided to an application object, representing the running code, and then this is drilled into using the dot operator: application.getDocument().getActiveView().getLayers()[0]
4
arcpy Access to the ESRI applications is through the arcpy library, which needs importing. Inline with Python's "It just works" philosophy, arcpy does some of the drilling down for you: Layer a = application.getDocument().getActiveView().getLayers().getLayer(0) is: with arcpy.mapping as m: a = m.listLayers(MapDocument("CURRENT"))[0]
5
arcpy Contains several modules depending on the extensions installed: GUI element support (pythonaddins) Geoprocessing etc. (arcpy) Data access module (arcpy.da) Mapping module (arcpy.mapping) (arcpy.mpmodule in ArcGIS Pro) ArcGIS Spatial Analyst extension module (arcpy.sa) ArcGIS Network Analyst extension module (arcpy.na) Also comes with a suitable numpy (though it only uses a specific numpy format called structured arrays, where columns have names and a memory structure). arr = arcpy.da.FeatureClassToNumPyArray(fc, fields, skip_nulls=True) my_array = arcpy.RasterToNumPyArray('C:/data/inRaster')
6
Python 2.7 ArcDesktop/ArcMap uses Python 2.7 ArcGIS Pro uses 3.5 (including conda libraries). If you use Anaconda in the labs, there is a 2.7 (27) version of Spyder, but it won't work as it doesn't know about the arcpy libraries. We'll see how to use 2.7 in the practicals. Find the Python versions used here:
7
Python 2.7 Changes were mainly to make Python 3 more restrictive, so reasonably simple to go from 3 to 2 — carry on writing 3-style for the majority. Standard libraries may have slight changes. External libraries may not exist. Major issues: except NameError as err: # 3 except NameError, err: # 2 "/" gives integer division in 2 (i.e. results in an integer if both numbers are integers). Use "//" if you want this in Python 3, which also works in 2. String formatting only with % and print("{}, {}".format(x, y)) * iterable unpacking operator doesn't work in 2.
8
Python 2.7 Try not to pick up any bad habits you might see in Python 2: print 'Hello World' (use parentheses) xrange() (use range, which now does this job, unless efficiency key) raise Error, "message" (use raise Error("message") instead) generator.next() (use next(generator) instead) <> (use != instead; it is the same from 2.6 onwards) Watch also: That input()in 2 reads numbers (and other types) directly, not strings (dangerous for security). That kwarg argument positioning is more flexible in 2. Loose use of variables: the scoping rules were much more flexible in 2, especially with loops. There's a maximum size for ints in 2 (found with sys.maxint); for very large ints, use long in 2. A very nice summary can be found at: See also:
9
ArcGIS Pro Uses Python 3. arcpy.mapping --> arcpy.mpmodule Uses conda for library installs. Can have scheduled jobs in Arc. Introduction: for-arcgis-pro.htm
10
Help Starting point for ArcMap programming is: You can change the documentation depending on which version you have. NB: don't confuse this with the ArcGIS Pro documentation, at:
11
API Complete lists of classes, functions: Complete list of geoprocessing tools: geoprocessing-tool-references.htm Help docs for tools now come with Python examples, e.g.:
12
Running Python Python Window: single commands Script tools: multiple commands with option for a parameter GUI Python Toolbox: ArcToolbox built from Python with special access Python addins: GUI elements with special access External scripts: standard Python using arcpy As we'll see in the practicals, it is sometimes useful to have these working together.
13
Writing Python Generally write in notepad++. You can use Spyder, but you won't be able to run it, and the debugging messages appear in Arc. Set editor for scripts (right-click -> edit) in Geoprocessing menu -> Geoprocessing Options. Documenting scripts: right-click tool/box -> Item Description -> Edit button.
14
Scheduling a task python-script-to-run-at-prescribed-times.htm As it happens, this is useful for all Python.
15
Start import arcpy arcpy.env.workspace = "c:/data/myDirectory" arcpy.env.workspace = "c:/data/myGeoDatabase.gdb" Then don't need to give paths for data in that directory/database. By default this is %USER%/ArcGIS/Default.gdb And stays this even for saved maps.
16
Paths Decide on relative or absolute paths when adding scripts. If you put relative, Arc will adjust some paths when Arc opens: The script path Default paths Files referenced in tool metadata and help Layer files (.lyr) used for the symbology property Compiled help files (.chm) Style sheets However, you cannot use relative paths yourself inside scripts that use ".." (going up directories is fine, just not down) s-explained-absolute-relative-unc-and-url.htm
17
Finding the current directory
For external files, if you want to find the current directory (so you can do stuff relative to that, or set the workspace) the __file__ hidden variable is set for all Python files as the current script running. So: filename = os.path.abspath(__file__) directory_name = os.path.dirname(__file__) new_file_path = os.path.join(os.path.dirname(__file__), 'new.txt') new_file_path = os.path.join(os.path.dirname(__file__), r'data\new.txt') Note use of "r" to use raw string rather than interpreting "\" as escape. See also for some nuances.
18
Start Any issues importing libraries you think are installed, see: arcpy.htm Note that Arc has many classes etc. with the same names, so don't use: from someLibrary import *
19
Stuff in Arc ESRI distinguish between: Built in functions
Classes and objects Object functions Geoprocessing tools Though the latter are just functions within modules and most stuff is accessed through arcpy, e.g. a = arcpy.Exists("c:/data/roads.shp") Note that ESRI generally don't follow community practice in having functions start with lowercase letters and using snake_case (using PascalCase instead).
20
Getting at stuff Either drill down to it through ArcObjects.
(later in course) Or use a tool that accesses it directly. arcpy.AddField_management("c:/data/myGeodatabase.gdb/roads ", "ROAD_NUMBER", "TEXT")
21
Distribution Scripts can be distributed with toolboxes in the same directory using relative paths. Addins are distributed as zip files with associated resources, as we'll see. Python toolboxes have a complex distribution setup, but are probably the best way to tie tools and toolboxes: geoprocessing-through-python-modules.htm the same setup process can be used with standard custom toolboxes. Internationalisation: language-support.htm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.