Python in ArcGIS Pro 2
Institute for Environmental and Spatial Analysis...University of North Georgia
Contents
1 Geoprocessing tools
2 Let’s try hydrology
- Download nc_spm_08_grass7_exercise.zip from GIS data.
- Add elevation.tif.
- Fill (Spatial Analyst)
- Flow Direction (Spatial Analyst)
- Flow Accumulation (Spatial Analyst)
- Set Null (Spatial Analyst)
3 Geoprocessing history to Python code
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
Use this batch file.
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:
- Filename
FirstLastname_facc.py
-5 - It should take arguments from the command line. -30
- It requires any debugging? -5 per line added, removed, or edited; the command line penalty is separate.