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/

Slides:



Advertisements
Similar presentations
Tutorial 8: Developing an Excel Application
Advertisements

Customisation The GUI in most GIS applications is sufficient for most needs. However, situations arise where you want either to: –Modify the interface,
Introduction to ArcPython Dan Mahr ‘11 10/28/2014 GEOL1320.
Creating a Program In today’s lesson we will look at: what programming is different types of programs how we create a program installing an IDE to get.
Automating Tasks With Macros
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
GIS Customization I. Binaural recording + reconstructing performances Holophonics - Virtual Barber Shop John Q. Walker: Re-creating great performances.
Introduction to ESRI Add-Ins
ModelBuilder at ArcGIS 9.2 Lyna Wiggins Rutgers University May 2008.
Introduction to ArcGIS Add-Ins Exercises GIS/LIS Conference, 2014 Rochester, MN.
Arc: Programming Options Dr Andy Evans. Programming ArcGIS ArcGIS: Most popular commercial GIS. Out of the box functionality good, but occasionally: You.
Introduction to InVEST ArcGIS Tool Nasser Olwero GMP, Bangkok April
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Introduction to Spatial Analysis and Spatial Modeling
Network Analysis with Python
Arc: AddIns Dr Andy Evans. Java Direct access to ArcObjects Framework inside and outside Arc. Ability to add components to the GUI. Ability to communicate.
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Python: An Introduction
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Martin Dodge Practical 2, 24th March 2004, pm Social Science Research Methodologies.
Programming for Geographical Information Analysis: Advanced Skills Lecture 1: Introduction Programming Arc Dr Andy Evans.
ArcGIS Pro: A Quick Tour of Python David Wynne.
Lecture Set 2 Part A: Creating an Application with Visual Studio – Solutions, Projects, Files.
LTER Information Management Training Materials LTER Information Managers Committee Documenting Spatial Data Theresa Valentine Andrews LTER.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Esri UC 2014 | Technical Workshop | Python Map Automation – Beyond the Basics of arcpy.mapping Jeff Barrette Jeff Moulds.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Lecture Set 2 Part A: Creating an Application with Visual Studio – Solutions, Projects, Files 8/10/ :35 PM.
Esri UC 2014 | Technical Workshop | Python Map Automation – Introduction to arcpy.mapping Michael Grossman Jeff Barrette.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Development Environment
Introduction to InVEST ArcGIS Tool
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Creating LOVs and Editors
PYTHON: AN INTRODUCTION
Testing and Debugging.
Arrays and files BIS1523 – Lecture 15.
Programming and Automation
Week 1 Gates Introduction to Information Technology cosc 010 Week 1 Gates
User Defined Functions
Instructor: Prasun Dewan (FB 150,
Lecturer: Mukhtar Mohamed Ali “Hakaale”
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
How to enter the world of Python Programming for ArcGIS
Programming for Geographical Information Analysis: Advanced Skills
Microsoft® Office Word 2007 Training
Lesson 4 Using Basic Formulas
Introduction to Python
Topics Introduction to File Input and Output
Working with Text and Cascading Style Sheets
Addins Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
Addins Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
This lecture Introduction to arcpy Debugging Using arcpy.
Programming for Geographical Information Analysis: Advanced Skills
This lecture Introduction to arcpy Debugging Using arcpy.
Python Map Automation – Beyond the Basics of arcpy.mapping
A QUICK START TO OPL IBM ILOG OPL V6.3 > Starting Kit >
Python Map Automation – Beyond the Basics of arcpy.mapping
Network Analysis using Python
PYTHON: BUILDING GEOPROCESSING TOOLS
Network Analyst – Automating Workflows with Geoprocessing
Programming Arc.
Topics Introduction to File Input and Output
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
Presentation transcript:

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