Python in ArcGIS Pro 2

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

1   Geoprocessing tools

geoprocessing-toolboxes.png

One of major ways to extend the capability of ArcGIS Pro.

Can be invoked by Python.

2   Let’s try hydrology

  1. Download nc_spm_08_grass7_exercise.zip from GIS data.
  2. Add elevation.tif.
  3. Fill (Spatial Analyst)
  4. Flow Direction (Spatial Analyst)
  5. Flow Accumulation (Spatial Analyst)
  6. Raster Calculator (Spatial Analyst)

3   Geoprocessing history to Python code

geoprocessing-history-to-python-code.png

4   Let’s look into auto-generated Python code

import arcpy
out_surface_raster = arcpy.sa.Fill("elevation.tif", None); out_surface_raster.save(r"C:\Users\hcho\AppData\Local\Temp\ArcGISProTemp24128\dd082615-93e5-456a-84c0-8c877b5bb9db\Default.gdb\Fill_tif1")

Some clean up

import arcpy

elev = "elevation.tif"
fill = r"C:\Users\hcho\AppData\Local\Temp\ArcGISProTemp24128\dd082615-93e5-456a-84c0-8c877b5bb9db\Default.gdb\Fill_tif1"

out_surface_raster = arcpy.sa.Fill(elev, None)
out_surface_raster.save(fill)

5   ArcPy

The core Python module for accessing ArcGIS Pro features from your Python code.

You have to use Esri’s Python environment.

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

Fill in Python

# to be able to pass command line arguments to Python
import sys

# ArcGIS Pro core module
import arcpy

# environment settings
# overwrite existing outputs
arcpy.env.overwriteOutput = True

# arguments from the user
elev = sys.argv[1]     # first argument
fill = sys.argv[2] # second argument

# run Fill
out_fill = arcpy.sa.Fill(elev, None)

# save the filled elevation raster
out_fill.save(fill)

6   Homework: Flow accumulation script

Create a Python script FirstLastname_facc.py that takes a flow direction file as input and a full path for flow accumulation output from the command line, and executes the Flow Accumulation (Spatial Analyst) tool. Upload this file only (FisrtLastname_facc.py) to D2L. Don’t upload a ZIP file. You’re warned!

Penalties:

  1. Filename FirstLastname_facc.py -5
  2. It should take arguments from the command line. -30
  3. It requires any debugging? -5 per line added, removed, or edited; the command line penalty is separate.