ArcPy Final Assignment

Mack Wade

 

Get Started with Python in ArcGIS Pro

In this course I learned how to write code to determine the number of features for all the feature classes in a workspace. This course also introduced some of the basics of Python syntax as I wrote code into the Python window in ArcGIS Pro.  

In order to begin this project I had to download data from ESRI’s website. I really enjoyed this course and chose to do it because of the downloadable data that I could use as I followed along, unlike the book which did not provide downloadable datasets for it’s walkthroughs. 

The course provided a few shapefiles to work out of, but for this exercise I used the Ambulances.shp. 

First, I ran a tool using Python. In Arc there are many ways and routes to complete one task. This task was to find the count of the items in the shapefile. You can do this in the attribute table or by using the Get Count tool in the Toolbox. You can also find the count by running a script in the Python window. 

To do this you take go to the toolbox history > right click the Get Count tool > Send to Python Window. After sending the window, I manually entered “Ambulances” since that’s the shapefile I want to be working out of. The script looked like this:

arcpy.management.GetCount(“ambulances”)

After completing this task, I learned that when completing a script in the Python window, the history is updated to include the task. 

The next task that I practiced was inputing an assignment statement. 

y = 73 

Then, 

x = 37

Then,

x * y 

Then, after pressing enter, the Python window answered with 2701

Next, I did a similar thing but with Temperature. 

temp_c = 17

temp_f = temp_c * 9 / 5 + 32

print(temp_f)

The Python window responded with 62.6

The next assignment was learning how to use the Python window as a tool to help you when writing code. You can do this by hovering over the prompt and reading the information that the window provides. 

There is also a system of code completion. For example, if you want the “Get Count” tool, you can typer “Get” in the Python Window and suggestions will pop up to help you finish the prompt easier. 

I really enjoyed this tutorial. I thought that it was very easy to follow and was helpful. I see myself using more of ESRI’s resources in the future if or when needed when writing scripts. I think that if any student was interested in this course in the future these tutorials would be a better resource than the book we used.

I had some screen shots from this assignment but they did not get added into the blog format. If you would like me to send those over to you. let me know.

Wade: Chapter 8: Manipulating Spatial and Tabular Data

 

Mack Wade

Chapter 8: manipulating Spatial and Tabular Data

 

Terms:

Comma separated Value (CSV): a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. 

Data Lock: There are two states of a lock i.e locked and unlocked. A lock is a class in the threading module whose object is generated in the unlocked state and has two primary methods i.e acquire() and release() .

Mode:  refers to the most frequently occurring number found in a set of numbers.

Parsing: the processing of a piece of python program and converting these codes into machine language.

Postfix clause: An SQL postfix clause is positioned in the second position and will be appended to the SELECT statement, following the where clause. The SQL postfix clause is most commonly used for clauses such as ORDER BY.

Prefix clause: The PREFIX clause declares any abbreviations for URIs that you want to reference in a query. You can declare prefixes to simplify query text if your data includes long URI names. If you do not declare prefixes, you must include the full URI names in the query.

SQL clause: Clauses are in-built functions available to us in SQL. With the help of clauses, we can deal with data easily stored in the table. Clauses help us filter and analyze data quickly. When we have large amounts of data stored in the database, we use Clauses to query and get data required by the user.

SQL expression: An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. An expression generally assumes the datatype of its components.

SQL keyword: the reserved words that are used to perform various operations in the database. There are many keywords in SQL and as SQL is case insensitive, it does not matter if we use for example SELECT or select.

Structured query language (SQL): a programming language designed to get information out of and put it into a relational database. Queries are constructed from a command language that lets you select, insert, update and locate data.

Triple Quotes: allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. The syntax for triple quotes consists of three consecutive single or double quotes.

Review Questions

  1. What are the three different cursors, and what purpose does each one serve?

cursor.fetchall() fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if there is no record to fetch.

cursor.fetchmany(size) returns the number of rows specified by size argument. When called repeatedly, this method fetches the next set of rows of a query result and returns a list of tuples. If no more rows are available, it returns an empty list.

cursor.fetchone() method returns a single record or None if no more rows are available

  1. Explain how data locks occur and how they can be removed.
    1. A lock is a class in the threading module whose object generated in the unlocked state and has two primary methods i.e acquire() and release() .
  2. When writing SQL expressions, why are quotation marks sometimes an issue? 
    1. Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren’t used in SQL, but that can vary from database to database. Stick to using single quotes.

Wade: Chapter 7: Debugging and Error Handling

Chapter 7: Debugging and Error Handling

Terms:

Breakpoint:  used to interrupt a running program immediately before the execution of a programmer-specified instruction. This is often referred to as an instruction breakpoint. … Breakpoints can also be used to interrupt execution at a particular time, upon a keystroke etc.

Commenting out code: to use comment syntax to remove something from the parsed code.

Custom class:  a developer defined class, based on one of the stock classes 

Debugging: the complete control over the program execution.

Error handling: the process of responding to the occurrence of exceptions – anomalous or exceptional conditions requiring special processing – during the execution of a program.

Exception:

Exception object: 

An event that occurs during the execution of a program that disrupts the normal flow of instructions is called an exception. 

Logic error: a mistake in a program’s source code that results in incorrect or unexpected behavior

Traceback: a report containing the function calls made in your code at a specific point.

Wade: Chapter 6: Exploring Spatial Data

Chapter 6: Exploring Spatial Data

Terms:

Dynamic (property): terminologies for attributes that are defined at runtime, after creating the objects or instances.

List comprehension: an easy and compact syntax for creating a list from a string or another list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than processing a list using the for loop.

System path

Wildcard: a symbol used to replace or represent one or more characters. Wildcards are used in computer programs, languages, search engines, and operating systems to simplify search criteria.

 

Review Questions:

  1. What are the key differences and similarities between the describe () and da.describe () functions?
    1. The Describe function returns a dictionary with multiple properties, such as data type, fields, indexes, and many others. The dictionary’s keys are dynamic, meaning that depending on what data type is described, different properties will be available for use.
  2. Explain the difference between system and catalog paths, and how they affect exploring data in a folder.
    1. The main difference between an absolute and a relative path is that an absolute path specifies the location from the root directory whereas relative path is related to the current directory.
  3. What is list comprehension, and when would you consider usng it.
    1. List Comprehension is an easy and compact syntax for creating a list from a string or another list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than processing a list using the for loop.

Wade: Chapter 5: Geoprocessing Using Python

Terms

Class: like an outline for creating a new object

Environment: a tool that helps to keep dependencies required by different projects separate

Factory Code:Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes. Factory Method defines a method, which should be used for creating objects instead of direct constructor call ( new operator).

Function: a block of code that only runs when it is called.

Hard-Coded: A part of a program that has been declared as unchanging

Instance: An individual object of a certain class.

Method: a function that “belongs to” an object.

Namespace: a system that has a unique name for each and every object in Python

Object: Everything is in Python treated as an object, including variable, function, list, tuple, dictionary, set, etc. Every object belongs to its class.

Package: A package is basically a directory with Python files and a file with the name __init__ . py. This means that every directory inside of the Python path, which contains a file named __init__ . py, will be treated as a package by Python.

Property: the main purpose of Property() function is to create property of a class.

Well-known ID (WKID): a unique number assigned to a coordinate system.

Well-known text (WKT): an Open Geospatial Consortium (OGC) standard that is used to represent spatial data in a textual format.

Workspace: supports the embedding and running of Python scripts within a workflow.

 

Review Questions

 

  • Explain some of the uses of the “Result” object. 
    • The result object can be used as an input to another function. The result object also has properties and methods.

 

 

  • Why are classes used as input parameters for geoprocessing tools?
    • Classes are often used as shortcuts for tool parameters that would otherwise have a more complicated equivalent.

 

  • What are some of the typical environments set in a script?

 

      1. ClearEnvironment.
      2. ListEnvironments.
      3. LoadSettings.
      4. ResetEnvironments.
      5. SaveSettings

 

  • Explain how Pro is licensed and how this impacts handling licensing when writing scripts.
    • You must have a license to run geoprocessing tools, which includes running a stand-alone python script that uses these tools. You will receive an error message if you try to run a tool that you do not have a license for, such as Spatial Analyst tools.

Wade: Chapter 4: Learning Python Language Fundamentals

Mack Wade

Chapter 4: Learning Python Language Fundamentals

Terms

Boolean: denoting a system of algebraic notation used to represent logical propositions, especially in computing and electronics.

Boolean Expression: In computer science, a Boolean expression is an expression used in programming languages that produces a Boolean value when evaluated. A Boolean value is either true or false.

Boolean Logic: In mathematics and mathematical logic, Boolean algebra is the branch of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0, respectively.

Boolean OPerator: Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.

Built in Operator: These operators are used to perform arithmetic computations on their operands. This operator returns the result of adding the two operands (operand1 and operand2).

Camel Case: Camel case is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, and the first word starting with either case. Common examples include “iPhone” and “eBay”

Casting: Casting is when you convert a variable value from one type to another.

Comparison Operator: Comparison operators — operators that compare values and return true or false . The operators include: > , < , >= , <= , === , and !== 

Condition: The boolean expression in a conditional statement that determines which branch is executed

Docstring: a string literal specified in source code that is used, like a comment, to document a specific segment of code

Dot Notation: You can access properties on an object by specifying the name of the object, followed by a dot (period) followed by the property name.

Escape character: a character that invokes an alternative interpretation on the following characters in a character sequence.

Float: a data type composed of a number that is not an integer, because it includes a fraction represented in decimal format.

Floor Division: a normal division operation except that it returns the largest possible integer.

F-String: provide a way to embed expressions inside string literals, using a minimal syntax

Immutable: unchanging over time or unable to be changed.

Indexing: a way to refer the individual items within an iterable by its position

Modulus: The Python modulo operator calculates the remainder of dividing two values. This operator is represented by the percentage sign (%). The syntax for the modulo operator is: number1 % number2. The first number is divided by the second then the remainder is returned

Parameter: he variable listed inside the parentheses in the function definition.

Sentry Variable: a variable used in the condition and it is compared to some other value or values

Snake Case: refers to the style of writing in which each space is replaced by an underscore character, and the first letter of each word written in lowercase. It is a commonly used naming convention in computing, for example for variable and subroutine names, and for filenames.

String: an immutable sequence data type. It is the sequence of Unicode characters wrapped inside single, double, or triple quotes

True Division: When dividing something by 1, the answer will always be the original number

Unicode:  Unicode, formally the Unicode Standard, is an information technology standard for the consistent encoding, representation, and handling of text expressed in most of the world’s writing systems.

Whitespace: characters which are used for spacing, and have an “empty” representation. In the context of python, it means tabs and spaces

Zero-Based Language: Zero-based numbering is a way of numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1 as is typical in everyday non-mathematical or non-programming circumstances

Review Questions

  1. What are the main data types in Python?
    1. The most common ones are float (floating point), int (integer), str (string), bool (Boolean), list, and dict (dictionary). float – used for real numbers. int – used for integers. 
  2. What are unicode strings?
    1. A Unicode string is a sequence of zero or more code points.
  3. What is dot notation?
    1. In general, dot notation tells Python to look inside the space that is before the dot for code to execute. You can use dot notation to access the specific version of a certain function that is defined in a different class or a different module.
  4. Name three methods of string objects in Python.
    1. The static method, the class method, and the instance method.
  5. What are some of the key similarities and differences between lists and tuples in python.
    1. list and tuple are a class of data structure that can store one or more objects or values. A list is used to store multiple items in one variable and can be created using square brackets. Similarly, tuples also can store multiple items in a single variable and can be declared using parentheses.
  6. Describe the use of Boolean expressions to evaluate a condition.
    1. A Boolean expression is a logical statement that is either TRUE or FALSE . Boolean expressions can compare data of any type as long as both parts of the expression have the same basic data type. You can test data to see if it is equal to, greater than, or less than other data.
  7. Describe the steps to create a dictionary and add new items to the dictionary.
    1. A Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value.
  8. Describe how branching is implemented in Python code.
    1. Branching statements in Python are used to change the normal flow of execution based on some condition. The return branching statement is used to explicitly return from a method. A break branching statement is used to break the loop and transfer control to the line immediately outside of loop. 
  9. What is a compound statement in Python? 
    1. Compound statements are made up of two or more program statements that are executed together. This usually occurs while handling conditions wherein a series of statements are executed when a TRUE or FALSE is evaluated. Compound statements can also be executed within a loop.
  10. Describe the two main looping structures in Python.
    1. “For” and “While” For loops can iterate over a sequence of numbers using the “range” and “xrange” functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient.For loops can iterate over a sequence of numbers using the “range” and “xrange” functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient.
  11. Why should you add comments to your scripts?
    1. Using comments in any script or code is very important to make the script more readable. Comments work as a documentation for the script. The reader can easily understand each step of the script if it is properly commented by the author.

Wade: Chapter 3: Geoprocessing in ArcGIS Pro

Mack Wade

Chapter 3: Geoprocessing in ArcGIS Pro

This chapter was pretty easy to follow along to. I had never used modelbuilder before so that was interesting. 

  • Batch Mode: Batch mode is a network round trip-reduction feature that, as its name implies, batches up data-related operations in order to perform them in more coarse-grained chunks.
  • Batch Processing: Computerized batch processing is the running of “jobs that can run without end user interaction, or can be scheduled to run as resources permit.
  • Current Workspace: Current Workspace—The workspace from which inputs are taken and outputs are placed when running tools. 
  • Geoprocessing: Geoprocessing is a framework and set of tools for processing geographic and related data.
  • Iteration: repetition of a mathematical or computational procedure applied to the result of a previous application, typically as a means of obtaining successively closer approximations to the solution of a problem
  • ModelBuilder: ModelBuilder is a visual programming language for building geoprocessingworkflows

Review Questions

  • Describe some of the general elements of the geoprocessing framework in ArcGIS Pro.
    • A collection of tools, methods to find and execute tools, environment settings and other geoprocessing options that control how tools are run, python window, geoprocessing history 
  • What are the three types of tools in ArcGIS Pro
    • Built tools, script tools, model tools, also system tools and custom tools
  • Explain the difference between system tools and custom tools
    • System tools are created by Esri
    • Custom tools are created by a user or third party
  • What are the strengths and limitations of batch processing in Pro
    • Geoprocessing tools can be run in a batch mode that allows you to run the tool multiple times using many input datasets or different parameter settings. This makes it possible to run a tool many times with very little interaction. 
  • What are some of the similarities and differences between models and tools and script tools in Pro
    • Model tools help you execute multiple tools at the same time
    • script tools help you create your own detailed tool by connecting applications

Wade: Chapter 2: Working With Python Editors

Mack Wade

Chapter 2: Working with Python Editors

Notes

This chapter wasn’t too hard to follow along to. I’m pretty familiar with Pro and some of the terms used. I only followed along with the Python window in Pro. 

Key Terms:

  • Command Line Interface: A command-line interface processes commands to a computer program in the form of lines of text.
  • Dependency: to refer when a piece of software relies on another one. Simply put, if Program A requires Program B to be able to run, Program A is dependent on Program B.
  • Environment: a hardware platform and the operating system that is used in it
  • Interactive Interpreter: Python interpreter runs each line of your code directly. This allows you to use the Python interpreter interactively, simply by typing python at a command prompt.
  • Module: A module is a software component or part of a program that contains one or more routines
  • Package Manager: A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer’s operating system in a consistent manner.
  • Printing: refers to writing text on the screen
  • Prompt: signals where you can type code
  • PyCharm: PyCharm is an integrated development environment used in computer programming
  • Python editor: has a menu-driven interface and tools for organizing and testing Python scripts to make working with Python easier
  • Python environment: A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. 
  • Python Package manager: Python Package Manager is a Python utility intended to simplify the tasks of locating, installing, upgrading and removing Python packages. It can determine if the most recent version of a software package is installed on a system, and can install or upgrade that package from a local or remote host.
  • Python Shell: Python provides a Python Shell, which is used to execute a single Python command and display the result.

 

Review Questions

  • Which Python editor comes installed with every version of Python? 
    • IDLE comes installed with every version of python. 
  • What are some of the recommended Python editors to work with in Pro?
    • Spyder, Pycharm
  • What is syntax highlighting?
    • Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.
  • What is the file extension for a Python script file?
    • .py 
  • How do you open the Python window in Pro?
    • Analysis Tab > Python > Python window
  • What are some typical examples of autocompletion prompts in the python window, and how do they help you write proper code
    • They assist in reducing the amount of writing you need to do

Wade: Chapter 1: Introducing Python

 

Mack Wade

Chapter 1: Introducing Python

Notes

  • Python is a free + open source software (FOSS)
  • Programming is building elements from scratch
  • Scripting is the glue that allows elements to work together
  • Python is both a scripting and programming language
  • Python is the preferred programming language for ArcGIS Pro (Pro uses Python 3)

Terms

  • Back Porting – When a software patch or update is taken from a recent software version and approved to an older version of the same software
  • Backward Compatibility – a property of a system, product, or technology that allows for interoperability with an older legacy system, or for input designed for such a system
  • Cross Platform – Any software application that works on multiple operating systems
  • Interpreted Language – A programming language which are generally interpreted, without compiling a program into a machine
  • Object Oriented – A methodology which enables a system to be modeled as a set of objects which can be controlled and manipulated in a modular manner
  • Object Oriented Programming (OOP) – A programming paradigm that relies on the concept of classes and objects
  • Syntax – The set of rules that defines the conditions of symbols that are considered to be correctly structured statements or expressions in that language

 

Review Questions

  • What are some of the key features of Python that make it suitable as a scripting language to work with ArcGIS Pro? 
    • Python is the preferred language for Pro, and has its own module, ArcPy,  that allows python to translate GIS functions. You can also run Python on the Python window in Pro that allows you to directly run code and see results in the map. 
  • What does it mean that Python is an interpreted language? 
    • Python is called an interpreted language because it goes through an interpreter, which turns code into the language understood by the computer’s processor
  • What are some of the differences between scripting and programming?
    • Scripting
      • To automate certain tasks in a program
      • Extracting info from a data set
      • Less code intensive as compared to traditional programming languages
    • Programming
      • Typically run inside a parent program 
      • More compatible while integrating code with mathematical models
  • Which Version of Python is used with ArcGIS Pro?
    • Python 3
  • What are some of the reasons to invest time and effort into learning Python?
    • Python is very easy to learn as a beginner. Python can also automate tasks that would otherwise be performed manually and would take up time.