Module 01

Intro to ArcGIS Pro & ArcPy

Yesterday in the Great Smokies
Webcam archive
GEO 409: Advanced GIS

Virtual field trip to

Scuttlehole Gap, Rockcastle River

36.963583,-84.346543

Goals

  • Set up our local workspace.
  • Fire up ArcGIS Pro.
  • Run a few Python scripts.
  • Learn basic Python
    • data types
    • assign and access variables
    • if/else statements

Our lab

  • Find GNIS points near selected locations.
    • Geographic Names Information System (GNIS).
    • Use Python in ArcGIS Pro to code solution.
    • Do it in class and submit as assignment.

Getting started

  • Set up GitHub.com user account (while mindful of the username).
  • Accept Lesson Invitation in Module 01.
  • Lesson and lab located at github.com/uky-gis/geo409-mod-01-username

I. Our workspace

I got data but

but where is it on my computer?

Perennial challenge

  • Data hygiene
  • concerns keeping data organized on your computer.

Your local computer

  • Create top-level directory for all projects
  • We call this the root directory
  • C:\YodasGIS (Windows OS)
  • /Users/yoda/YodasGIS (macOS)
  • Put projects inside this directory
  • Separate downloaded from created assets

Example directory structure

Repo?

  • A repository ('repo') is a project stored as a folder that is synchronized to a remote server. (More soon)

Toaster problem

  • Why does it have a cancel button?
  • Because we don't trust it automatically
  • and so goes your computer.

At the very least

  • Be able to find the absolute file path and file extension
  • of an asset on your Windows computer.
  • C:\YodasGIS\data\N042E254_2020.tif

Windows File Explorer

  • Right click on the file
  • click Properties.

ArcGIS Pro

  • Locate asset in Catalog window
  • hover mouse cursor over asset.

Folder & file naming conventions

  • Semantic name, e.g., describe contents.
  • No spaces; replaces with hyphen, underscore, or use 'camelCase'.
  • Always use a file extension for file names.
  • Always case-sensitive.
  • How the Smithsonian does it

Most common errors

  • Not knowing where the asset is located.
  • Not providing the correct name.

In-class task

  • Create root GIS and data folders.
  • Download and extract this database.
  • Move ky_small_scale.gdb to data folder:
  • C:\YodasGIS\data\ky_small_scale.gdb

II. ArcGIS Pro

CHECK YOUR EMAIL:

Look for “An invitation to join an ArcGIS Online organization, University of Kentucky.” to find your ArcGIS Pro login.

ArcGIS Pro

  • Released by Esri 2015 as flagship desktop GIS software
  • Combines 2D and 3D capabilities
  • and replaces ArcGIS Desktop
    • a collection of desktop GIS software.
  • Explore interface

Before visualizing data

  • explore its:
    • file path
    • data type
    • CRS
    • attributes

When visualizing data

  • understand its:
    • extent
    • scale
    • error

In-class task

  • Fire up ArcGIS Pro and customize app for your workspace.
  • Create a new map project called, "module-01".
  • Inspect ky_small_scale.gdb.
  • Visualize and symbolize a few layers.

III. Python & ArcPy

Python ✊🐍

  • Created by Guido van Rossum in 1990s.
  • One of the most popular programming languages.
  • Designed to be easy to read and write.
  • Named after Monty Python; not a constrictor.

ArcPy

  • ArcGIS Pro's Python API (application programing interface)
  • exposes ArcGIS Pro's functionality to Python.

Python in ArcGIS Pro

  • We'll use Jupyter Notebook (or just Notebook)
    • which creates cells of Markdown content and Python code
    • with interactive execution cell by cell
    • and saved as a text file with the .ipynb file extension.

Programming goals

  • We'll cover enough to work in ArcGIS Pro
  • to script/automate geoprocessing workflows.
  • Let's give Python a spin in an ArcGIS Pro Notebook.

In-class task

  • Create a Notebook called 'practice.ipynb' in module-01.
  • Copy the code in the next slide into the first Notebook cell.
  • Run the cell; output and a new cell should appear.
  • Each slide is a new cell.

Want to learn more?

  • Jump down to the Learning to code 🧐 section and continue on your own in this Notebook.

How much of x is near y?

  • How many arches are withing one mile of Miguel's Pizza?
  • Let's find out with ArcPy.

In-class task

  • Create a Notebook called 'arches-near-pizza.ipynb' in module-01.
  • Copy the code in the next slides into the Notebook.
  • Run the cell-by-cell.

Pseudocode

  • Describing code without using the language.
  • Helps understand the flow of the code.

Our Pseudocode I

  • Import needed tools.
  • Connect our input/output database.
  • Create our input/output coordinate systems.
  • We call this setting up our environment.

Our Pseudocode II

  • Create point for desired location.
  • Copy it to our database.
  • Buffer point to desired distance.
  • Clip features from another layer within this distance.
  • We call this our analysis.

What could go wrong? 💣

  • TYPOS!
  • Misspelled string for the workspace property.
  • We'll discover a way list contents of a workspace.
  • Check all strings.

What could go wrong? 💣

  • SELECTIONS!
  • If features are selected (highlighted in cyan) in ArcGIS Pro, they will be used in the script.
  • Clear all selections before running a script.

Learning to code 🧐

First steps

  • Use code editor to highlight syntax
  • distinguish data, keywords, and comments
  • always case-sensitive
  • spacing matters.

Python I

Practice along

  • Create/open practice.ipynb notebook in ArcGIS Pro
  • copy and paste examples from lesson README.md.
  • TIP: keystroke SHIFT + ENTER runs the cell.
  • TIP: have only one notebook open at a time in ArcGIS Pro.

Required viewing

  • Variables, expressions, and statements P4E
  • Conditional Execution P4E
  • Videos embedded in next four slides 🛎️

Value

  • A value is data that we use in our program
  • All values have a type
  • The type of value determines what can be done in the program.

Basic types

  • String (of characters)
  • Number types
    • Integer (1)
    • Float (1.0)
  • Boolean (True/False)
  • None (null)

Levels of measurement

  • A number doesn't always represent a quantity
  • Nominal (zipcode)
  • Ordinal (basketball ranking)
  • Interval (temperature of gym)
  • Ratio (weight of basketball)

Errors

  • Anticipate and prevent
  • Look at last line in Python error message
  • Use print() function on variables

Syntax errors

  • Incorrect sequence of characters
  • Most common when beginning to code
  • Check spelling, everything is case-sensitive

Exceptions

  • Syntactically correct but still flawed.
  • "John is a married bachelor." Can't be true!
  • Division by zero

Logic errors

  • Syntactically correct and executes properly.
  • Result is not correct, though not immediately apparent
  • Order of operations in math calculations
    (10 + 20 + 30) / 3 vs. 10 + 20 + 30/3

Lab 2

Review

  • Open Lab-02.ipynb Jupyter Notebook in ArcGIS Pro
  • Five code exercises
  • Application to filter GNIS data
    • US Place name database (GNIS) 2.5 M records Download National File
    • Import Python csv module and work on real data

Python modules

Namespace

  • Uniquely identify resources
  • Access functions in module with dot notation
  • E.g., csv.reader() to access the read-in function.

STOP