Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

The following step-by-step instructions and screenshots are based on the Windows 10 operating system with the Windows Classic desktop theme and ArcGIS Pro 3.0.0 software. If your personal system configuration varies, you may experience minor differences from the instructions and screenshots. Some of the features covered in this tutorial may not be available if using an older version of ArcGIS Pro.

Dataset for this course



Python Crash Course

What is Python?

Python is a high-level scripting programming language often used for data analysis, among numerous other applications. In ArcGIS Pro, Python is used to define many of the tools in the background, and can also be utilized by the user to automate part or all of a data analysis project in ArcGIS Pro.  This automation is done by writing Python scripts, which are instructions written by the programmer for Python to carry out. 

Setting Up a Python Script in ArcGIS Pro

The easiest (and most convenient) way to write Python scripts in ArcGIS Pro is to use notebooks in ArcGIS Pro. We will first set up a notebook in ArcGIS Pro to go over some Python basics, and learn more about notebooks later on. 


(setup notebook)

Python Data Types

The building blocks of a program are variables, which store data in a variety of forms. This data can be made available to the programmer and used later in the program in multiple ways.

Numbers

First, let's work with numbers in Python. By typing the code shown below, we can store the number 3 in variable a, and the number 4 in variable b.

...

We can check the values of these variables by using the print function as shown below.


Click the "Run" button to run the code in the current cell as indicated below.

...

Now, we can run some basic arithmetic operations with these numbers. The operations shown below correspond to addition, subtraction, multiplication, and division, respectively. After printing each of the corresponding variables, we can see the results. 

Image Modified


We can treat decimal numbers in a very similar way, and combine them with integers. 

Image Added

Booleans

(add floats next)

Booleans

Strings

Lists

...

Before we start looking at our next data type (Booleans), we can start a new cell to separate our previous code with our new code. To do this, press the "+" button in the Notebook menu. 

Image Added

Boolean variables are the simplest type of variables- any Boolean is either True or False. We can code them as such here:

Image Added


We can combine them using keywords "and" and "or". If two Boolean variables i and j are both True, then "i and j" results in True. In any other case, "i and j" results in False. 

Image Added

If either of the two Boolean variables i and are True, then "i or j" results in True. If both i and j are False, then "i or j" results in False. 

Image Added

Lists

Python lists store multiple other Python variables inside one variable- simply put, they are simply lists of variables. It is recommended, though not enforced, that all of these variables are the same type.

Strings

Strings in Python are simply lists of characters, which are letters, numbers, or other symbols enclosed in quotes. 

Dictionaries

Dictionaries in Python 

Functions

Conditional Statements

Loops

Using Python in ArcGIS


Field Calculations


Fields in ArcGIS Pro

Why Use Python?

Switching Data Types

Defining a Field from Other Fields

Notebooks

Jupyter Notebooks

Jupyter Notebooks in ArcGIS Pro

Automating Workflow Using Jupyter Notebooks

Here, we'll go through a short sample project to practice using some simple tools using Python in ArcGIS Pro notebooks.


Our goal for this project will be to take a collection of schools throughout the United States, and create a layer containing the average center of these schools for each state / territory. The approach we'll take isn't necessarily the most efficient way of doing this, but will allow us to look at a wide variety of ways to use Python in ArcGIS Pro. 


The collection of schools we'll be working with can be found at O:\GDCTraining\1_Short_Courses\Python_and_ArcGIS\StateSchools.gdb\PublicSchools10K. The layer of US states / territories can be found at O:\GDCTraining\1_Short_Courses\Python_and_ArcGIS\StateSchools.gdb\USStates. The original source of the schools is from (hifld-geoplatform.opendata.arcgis.com/datasets/public-schools), which was reduced to a collection of 10,000 schools for the sake of time, as these tools can take a long time to run, regardless of whether or not Python is being used. Alternatively, these datasets can be downloaded from this file




screengrabs:

Image Added

Image Added


Image Added

Image Added(go through extended example)