arcpy Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide. http://desktop.arcgis.com/en/analytics/python/
This lecture Introduction to arcpy Debugging Using arcpy
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]
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]
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). http://desktop.arcgis.com/en/arcmap/latest/analyze/python/working-with-numpy-in-arcgis.htm https://docs.scipy.org/doc/numpy/user/basics.rec.html arr = arcpy.da.FeatureClassToNumPyArray(fc, fields, skip_nulls=True) my_array = arcpy.RasterToNumPyArray('C:/data/inRaster') http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/tabletonumpyarray.htm
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: https://support.esri.com/en/technical-article/000013224 https://arcpy.wordpress.com/2016/10/21/conda-and-arcgis-pro/ http://pro.arcgis.com/en/pro-app/arcpy/get-started/installing-python-for-arcgis-pro.htm https://community.esri.com/docs/DOC-8359
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.
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: http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html See also: https://docs.python.org/3.0/whatsnew/3.0.html
ArcGIS Pro Uses Python 3. arcpy.mapping --> arcpy.mpmodule Uses conda for library installs. Can have scheduled jobs in Arc. Introduction: https://pro.arcgis.com/en/pro-app/arcpy/get-started/installing-python- for-arcgis-pro.htm
Help Starting point for ArcMap programming is: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/ You can change the documentation depending on which version you have. NB: don't confuse this with the ArcGIS Pro documentation, at: https://pro.arcgis.com/en/pro-app/arcpy/
API Complete lists of classes, functions: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy/what-is-arcpy-.htm Complete list of geoprocessing tools: http://desktop.arcgis.com/en/arcmap/latest/tools/main/a-quick-tour-of- geoprocessing-tool-references.htm Help docs for tools now come with Python examples, e.g.: http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/buffer.htm
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.
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.
Scheduling a task http://desktop.arcgis.com/en/arcmap/latest/analyze/python/scheduling-a- python-script-to-run-at-prescribed-times.htm As it happens, this is useful for all Python.
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.
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) http://desktop.arcgis.com/en/arcmap/latest/tools/supplement/pathname s-explained-absolute-relative-unc-and-url.htm
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. http://desktop.arcgis.com/en/arcmap/latest/analyze/python-addins/essential-python-add-in-concepts.htm See also https://anothergisblog.blogspot.co.uk/2014/06/finding-location-of-your-python-file.html for some nuances.
Start Any issues importing libraries you think are installed, see: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/importing- arcpy.htm Note that Arc has many classes etc. with the same names, so don't use: from someLibrary import *
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).
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")
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: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/extending- geoprocessing-through-python-modules.htm the same setup process can be used with standard custom toolboxes. Internationalisation: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/international- language-support.htm