\[ \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\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)

    ))

  10. 1   Revisit Homework 1
  11. 1   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()

    2   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"

    1   Revisit Homework 1

    1.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?

    1.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?

    1.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.

    1.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'