programming in arcgis ii - lunds...

22
David Tenenbaum – NGEN13 – Lund University Programming in ArcGIS II In ArcGIS, tools not only appear in the middle of processes in ModelBuilder, but they can also be represented by equivalent Python code In fact, whether you are using a tool straight from ArcToolbox, or are adding a tool to a model in ModelBuilder, what you are really doing in both of those cases is making use of a graphical user interface that creates an appropriate line of Python code for ArcGIS to run Python code is the fundamental way that ArcGIS’ geoprocessing functions work in recent versions

Upload: others

Post on 03-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

  • David Tenenbaum – NGEN13 – Lund University

    Programming in ArcGIS II

    • In ArcGIS, tools not only appear in the middle of

    processes in ModelBuilder, but they can also be

    represented by equivalent Python code

    • In fact, whether you are using a tool straight from

    ArcToolbox, or are adding a tool to a model in

    ModelBuilder, what you are really doing in both of

    those cases is making use of a graphical user

    interface that creates an appropriate line of Python

    code for ArcGIS to run

    • Python code is the fundamental way that ArcGIS’

    geoprocessing functions work in recent versions

  • David Tenenbaum – NGEN13 – Lund University

    Programming in ArcGIS II

    • The equivalency of GUI tool to a process in

    ModelBuilder to a line of Python code, and the fact that

    we can drag and drop items between these parts of

    ArcGIS, makes it pretty easy to learn how to use them:

    – We can work out details of how to use a tool in its GUI

    – We can then drag and drop that into ModelBuilder and

    combine it with other tools

    – We can drag and drop to the Python command line (or

    request the Python code for a complete model) and then we

    can see how it looks as code

    • This way, we can learn Python code incrementally, by

    using the GUI to help us

  • David Tenenbaum – NGEN13 – Lund University

    Object-oriented Programming

    • Object-oriented programming (OOP) is a programming

    approach that uses "objects" to design applications and

    computer programs

    – This is contrast to modular or procedural programming that

    you might be familiar with, where lines of code are numbered

    and run in sequence

    • Even though it originated in the 1960s, OOP was not

    commonly used in mainstream software application

    development until the 1990s

    • Today, many popular programming languages (such as

    Java, JavaScript, C#, VB, .Net, C++, Python, PHP, Ruby

    and Objective-C) support OOP

  • David Tenenbaum – NGEN13 – Lund University

    Object-oriented Programming

    Important OOP terminology:

    • Object - Anything that can be ‘seen’ or ‘touched’ in the

    software programming environment . Objects have a

    value, a type, and methods

    • Value – The value is what the object contains:

    – e.g. >>> name = “Paul”

    • Type – The type defines the kind of object:

    – e.g. >>> type(name) returns

    • Methods - An object’s methods are operations that the

    object can perform or that can be performed upon the

    object

  • David Tenenbaum – NGEN13 – Lund University

    Object-oriented Programming

    Important OOP terminology:

    • Class - A pattern or blueprint for creating an object. It

    contains all the properties and methods that describe the

    object

    • Instance - The object you create from a class is called an

    instance of a class

    • Distinguishing an object/instance vs. a class, examples:

    – Cookie vs. a cookie-cutter

    – Car vs. the blueprint for manufacturing the car

  • David Tenenbaum – NGEN13 – Lund University

    ArcObjects

    • So where do ArcObjects come into the picture?

  • David Tenenbaum – NGEN13 – Lund University

    • ArcObjects are platform

    independent software

    components, written in C++,

    that provide services to support

    GIS applications

    • Because this architecture

    supports a number of unique

    ArcGIS products with

    specialized requirements, all

    ArcObjects are designed and

    built to support a multi-use

    scenario

    ArcObjects

  • David Tenenbaum – NGEN13 – Lund University

    ArcObjects

    • You can think of ArcObjects as the stuff that the insides

    of ArcGIS is made of … all the structure and

    functionality that allows ArcGIS to do what it does

    • ArcObjects are a set of computer objects specifically

    designed for programming ArcGIS applications, and

    they include things that have manifestations you can see

    onscreen, as well as more abstract data objects

    • Anytime you have used ArcGIS, whether or not you knew

    it, you have been using ArcObjects … but what we will

    learn is how to work with them in more powerful ways

    than just through the user interface

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagrams

    • Part of the process of learning how to program any

    object-oriented language is to learn about the classes

    available in that language and their relationships. This

    includes:

    – Learning what properties and methods a class has

    – Learning the relationships between classes so you understand

    how one gets from one class to another

    • There is no substitute for experience (i.e. actually

    writing Python code and using the classes, setting and

    getting their properties, using their methods) … but object

    model diagrams (OMDs) are a useful guide to the set of

    classes in a particular language / package

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.0

  • David Tenenbaum – NGEN13 – Lund University

    • Let’s zoom in a little and have a look:

    • The light blue box on the left is the arcpy

    object itself

    • Properties are listed at the top

    • Below those, methods are listed

    • The symbols to the left of the properties

    and methods tell you a little about them,

    using a symbology called the Unified

    Modeling Language

    • Here’s the key for this from the OMD:

    OMD for ArcPy in ArcGIS 10.0 - arcpy

  • David Tenenbaum – NGEN13 – Lund University

    Unified Modeling Language Diagrams - Classes

    • Part of what a Unified Modeling Language (UML)

    diagram shows you is the characteristics of a class:

    Parcel

    Value: Currency

    Zoning: String

    Calculate Tax (): Currency

    Class Name

    Properties

    Methods

    • While these diagrams are useful to us just to see the

    characteristics of a class, their real power comes in

    showing us the relationships between the classes in the

    object model

  • David Tenenbaum – NGEN13 – Lund University

    BirdAbstract

    ChickenCoClass

    NestClass

    *

    WingsClass

    2

    FarmCoClass

    EggClass

    • Is a type of

    • Is composed of

    • Creates a(n)

    • Multiplicity

    • Association

    Unified Modeling Language Diagrams –

    Relationships Between Classes

    • Unfortunately, we

    do not have a full,

    proper UML

    diagram of ArcPy

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.1

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.2.1

    Here are arcpy

    classes and

    functions

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.2.1

    – arcpy Classes and Functions

    • Still too small to read!

    • I will provide the PDFs

    Here is the FieldInfo class

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.2.1

    – FieldInfo Class

    • Unfortunately, this is not a

    proper UML class diagram

    • No ‘barbells’ to show if one

    can put a property …

    • It still lists methods and

    properties

    • It still identifies types for

    properties, and parameters

    and types of objects

    returned for methods

    It is still extremely useful for learning the object model!

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.2.1

    arcpy

    classes &

    functions

    Geometry

    & Spatial

    Classes

    Data Access Module

    Time ModuleGraph

    Generation

    Network Analyst

    Module

    Geospatial

    Analyst Module

    Mapping

    Module

    Spatial

    Analyst

    Module

  • David Tenenbaum – NGEN13 – Lund University

    Object Model Diagram for ArcPy in ArcGIS 10.2.1

    – Mapping Module

    • Also too small to read!

    • You will use the PDF

  • David Tenenbaum – NGEN13 – Lund University

    ArcPy – Mapping Module

    • The Mapping Module allows you to manipulate maps

    using code, in terms of both their content and appearance

    – This includes both the kinds of tasks you would perform in the

    Data View or the Layout View of ArcMap

    • This includes dealing with map documents:

    – Saving, printing, and exporting map documents

    • It includes managing data frames:

    – Adding and removing layers, updating the frame

    • It also includes manipulating the appearance of layers:

    – Changing or controlling all aspects of their appearance

    • Finally, it can control all aspects of layout elements:

    – Position and values of all elements

  • David Tenenbaum – NGEN13 – Lund University

    ArcPy – Mapping Module

    Map Element

    Map Document Data Frames Layers Layout Elements

    Operations

    Save Add, Remove

    Layers

    Change Names Modify Text,

    Legend, Map

    Surround,

    Picture, and

    Graphical

    Elements

    Page Position

    and Values

    Print

    ExportChange Frame

    Size, Rotation,

    Scale, Units,

    Spatial

    Reference, etc.

    Get/Set Extent

    Zoom to Selected

    Change Visibility

    Change

    Transparency

    Labels On/Off

    Apply Definition

    Queries

    Modify Groups

  • David Tenenbaum – NGEN13 – Lund University

    Querying and Selecting Using Python Code

    • Querying is one of the easier things to learn how to

    do in Python code if already know how to use queries

    in ArcGIS

    • It fits into the original Geoprocessing framework

    that Python was designed to control in ArcGIS

    • There are arcpy methods for both

    – Select by Location – arcpy.SelectLayerByAttribute_management

    – Select by Attributes – arcpy.SelectLayerByLocation_management

    • The functionality mirrors that of the tool interfaces,

    and the tool interfaces can even export Python code

    to help you learn how to code it yourself …