\[ \newcommand\si[1]{\mathrm{#1}} \newcommand\SI[2]{#1\,\si{#2}} \newcommand\matr[1]{\mathbf{#1}} \DeclareMathOperator*{\argmax}{arg\,max} \DeclareMathOperator*{\argmin}{arg\,min} \]

Python in ArcGIS Pro exercises

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   How to practice these exercises

Please type every single letter when you write code. Do NOT copy and paste any code from my slides. Remember your eyes and fingers learn. Copy and paste is like trying to learn a new natural language without speaking it.

Will there be any new materials? Yes, but most of them are not important at this point. They are just there to complete our exercises. I added NEW SYNTAX WARNINGs and tried to explain new syntax, but don’t worry about it even if you don’t understand what it means. Still, type it.

2   Folder structure for these exercises

If you’re not familiar with file management (e.g., where to locate certain files and how to copy files to a certain folder), please use the same folder structure in this exercise and follow my instructions as is.

  1. Open File Explorer.
    1. Windows Key + E
  2. Move to the root of the C drive.
    1. Alt+D
    2. Type C:\
    3. Hit Enter
  3. Create a new folder called PythonExercises.
    1. Right click inside the main panel of File Explorer where you can see the list of folders and files in the C drive
    2. New ⇒ Folder
    3. Rename New Folder to PythonExercises
  4. Now, do you see PythonExercises under C:\?

3   Add field script

In this exercise, we want to create a Python script that can add a new field to an existing Shapefile.

3.1   How to create add_field.py

Do you know how to export history to a Python script?

  1. Using the same technique from this section, create a new folder called Field inside C:\PythonExercises.
  2. Download Counties_Georgia.zip and extract the Counties_Georgia Shapefile into C:\PythonExercises\Field.
    • You should have C:\PythonExercises\Field\Counties_Georgia.shp and its auxiliary files in the same folder. I didn’t mean to extract all into C:\PythonExercises\Field\Counties_Georgia.
  3. Add C:\PythonExercises\Field\Counties_Georgia.shp to the map.
  4. Find the “Add Field” geoprocessing tool. Remember we’re not using the Field ⇒ Add icon in the attribute table.
    • Input Table: Counties_Georgia
    • Field Name: Test
    • Field Type: Double
  5. Run.
  6. Go to Analysis ⇒ History.
  7. Right click on the “Add Field” history.
  8. Save As Python Script to C:\PythonExercises\Field\add_field.py.
  9. Open C:\PythonExercises\Field\add_field.py in a text editor. Do you see this code?
    import arcpy
    arcpy.management.AddField("Counties_Georgia", "Test", "DOUBLE", None, None, None, '', "NULLABLE", "NON_REQUIRED", '')

3.2   How to modify add_field.py

Now, let’s try to make the script more useful.

# we need the sys module to access arguments from the command line
import sys
# we need the arcpy module to run ArcGIS Pro tools
import arcpy

# path to a feature class as the first argument
fc_path = sys.argv[1]
# field name as the second argument
field_name = sys.argv[2]
# field type as the third argument
field_type = sys.argv[3]

# run Add Field
arcpy.management.AddField(fc_path, field_name, field_type, None, None, None, '', "NULLABLE", "NON_REQUIRED", '')

3.3   How to run add_field.py

  1. Make sure to close ArcGIS Pro (preferred) or remove the layer from the map (sometimes, ArcGIS Pro may still lock the Shapefile).
  2. Open a cmd window.
    1. Windows Key + R
    2. Type cmd
    3. Hit Enter
  3. Change the drive if you’re not already in C:.
    C:
  4. Change the directory to your script folder C:\PythonExercises\Field.
    cd \PythonExercises\Field
  5. Run the script.
    python add_field.py Counties_Georgia.shp TestText Text
  6. You should know how to check whether or not the script worked.

4   Calculate Field script

In this exercise, we want to create a Python script that can populate a field using simple algebra without code blocks.

4.1   How to create calc_field.py

Oh! You still don’t know? Here is how.

  1. Add the already extracted Counties_Georgia Shapefile to the map.
  2. Find the Calculate Field tool from the Geoprocessing tab, pane, panel (?), whatever...
    • Input Table: Counties_Georgia
    • Field Name: TestText (from the Section 3 exercise)
    • TestText = “Test”
  3. Run.
  4. Go to Analysis ⇒ History.
  5. Right click on the “Calculate Field” history.
  6. Save As Python Script to C:\PythonExercises\Field\calc_field.py.
  7. Open C:\PythonExercises\Field\calc_field.py in a text editor. Do you see this code?
    import arcpy
    arcpy.management.CalculateField("Counties_Georgia", "TestText", '"Test"', "PYTHON3", '', "TEXT")

4.2   How to modify calc_field.py

Again, let’s make it more useful.

# we need the sys module to access arguments from the command line
import sys
# we need the arcpy module to run ArcGIS Pro tools
import arcpy

# path to a feature class as the first argument
fc_path = sys.argv[1]
# field name as the second argument
field_name = sys.argv[2]
# algebraic expression as the third argument
expr = sys.argv[3]
# otional code block as the fourth argument
# NEW SYNTAX WARNING!!!
# ternary operator
#   <true_expression> if <condition> else <false_expression>
# if you get it, great! if not, just type it for now
block = sys.argv[4] if len(sys.argv) > 4 else ''

# the last argument is used only when the specified field doesn't exist
# according to Esri's help at
# https://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-field.htm
# set it to None
#   arcpy.management.CalculateField(fc_path, field_name, expr, "PYTHON3", block, None)
# or simply remove it
arcpy.management.CalculateField(fc_path, field_name, expr, "PYTHON3", block)

4.3   How to run calc_field.py

  1. Make sure to close ArcGIS Pro (preferred) or remove the layer from the map (sometimes, ArcGIS Pro may still lock the Shapefile).
  2. Open a cmd window.
    1. Windows Key + R
    2. Type cmd
    3. Hit Enter
  3. Change the drive if you’re not already in C:.
    C:
  4. Change the directory to your script folder C:\PythonExercises\Field.
    cd \PythonExercises\Field
  5. Run the script.
    python calc_field.py Counties_Georgia.shp TestText "'Test Test'"
    • In the above example, make sure to quote Test Test twice with different quotes. Why? You want to pass 'Test Test' to Calculate Field, not Test Test because the TestText field is in Text so you need to quote its value. The first quote will be stripped off by the command line.
  6. Again, you know how to check whether or not the script worked, right?

5   Summary statistics script

In this exercise, let’s see how to extract field statistics.

5.1   How to create field_stats.py

You know how, right?

  1. Add the same Counites_Georgia Shapefile to the map.
  2. Find the Summary Statistics tool.
    • Input Table: Counties_Georgia
    • Output Table: Counties_Georgia_Statistics
    • FieldStatistic Type
      totpop10Mean
      totpop10Standard deviation
  3. Run.
  4. Go to Analysis ⇒ History.
  5. Right click on the “Summary Statistics” history.
  6. Save As Python Script to C:\PythonExercises\Field\field_stats.py.
  7. Open C:\PythonExercises\Field\field_stats.py in a text editor. Do you see this code?
    import arcpy
    arcpy.analysis.Statistics("Counties_Georgia", r"C:\Users\geni\AppData\Local\Temp\ArcGISProTemp30748\bfb83c98-7e80-4565-8e78-fc5db62d0f00\Default.gdb\Counties_Georgia_Statistics", "PopDens MEAN;PopDens STD", None)

5.2   How to modify field_stats.py

# we need the sys module to access arguments from the command line
import sys
# we need the arcpy module to run ArcGIS Pro tools
import arcpy

# overwrite existing files
arcpy.env.overwriteOutput = True

# path to a feature class as the first argument
fc_path = sys.argv[1]
# path to the output table as the second argument
table_path = sys.argv[2]
# field statistics to calculate as the third argument
field_stats = sys.argv[3]

arcpy.analysis.Statistics(fc_path, table_path, field_stats, None)

# NEW SYNTAX WARNING!!!
# open file for reading
f = open(table_path)
# print each line
for line in f:
    # each line from f already contains a new line, so don't print a new line
    # again (end='')
    print(line, end='')
# close file
f.close()

5.3   How to run field_stats.py

For output, you have to type .\ to create it in the current folder and use the .csv extension to be able to read it without using Excel.

python field_stats.py Counties_Georgia.shp .\totpop10_stats.csv "totpop10 MEAN; totpop10 STD"

6   Revisit Homework 1

6.1   Calculate the areas of counties in $\text{km}^2$

Use the same Counties_Georgia Shapefile from the above exercises, not the one you submitted for Homework 1 because the latter already has the SqKm field.

python add_field.py Counties_Georgia.shp SqKm Double
python calc_field.py Counties_Georgia.shp SqKm "2.58999 * !Sq_Miles!"

Did it work for you?

6.2   Calculate the population density in $\text{people}/\text{km}^2$

python add_field.py Counties_Georgia.shp PopDens Double
python calc_field.py Counties_Georgia.shp PopDens "!totpop10! / !SqKm!"

Did it (not?) work again?

6.3   Calculate the mean and standard deviation of the population density

python field_stats.py Counties_Georgia.shp .\PopDens_stats.csv "PopDens MEAN; PopDens STD"

Did you get this output?

FID,FREQUENCY,MEAN_PopDens,STD_PopDens
,159,72.526758495576033,143.460174992006614

FID is empty, FREQUENCY is 159, and the mean and standard deviation of the population density are $\SI{72.53}{\text{people}/\text{km}^2}$ and $\SI{143.46}{\text{people}/\text{km}^2}$, respectively.

6.4   Calculate the population density category

python add_field.py Counties_Georgia.shp PopDensCat Text
python calc_field.py Counties_Georgia.shp PopDensCat "'Low' if !PopDens! < 72.53 - 143.46 else 'Medium' if !PopDens! < 72.53 + 143.46 else 'High'"

Did it work?

NEW SYNTAX WARNING!!!

Nested ternary operators!

Here, we want to assign ‘Low’ if !PopDens! is less than $72.53 - 143.46$.

'Low' if !PopDens! < 72.53 - 143.46

Else we want to assign ‘Medium’ if !PopDens! is less than $72.53 + 143.46$.

else 'Medium' if !PopDens! < 72.53 + 143.46

Otherwise, we want to assign ‘High’.

else 'High'