Python and ArcGIS Pro

Python logo

This tutorial will give a brief introduction to the use of the Python programming language in ArcGIS Pro.

Python is an cross-platform, open-source, general purpose programming language developed by Dutch programmer Guido van Rossum and first released in 1991. Compared to many other computer languages, Python is comparatively easy to use and is commonly used for interacting with and customizing software.

ArcPy is a module that provides functions, classes, and methods for performing "geographic data analysis, data conversion, data management, and map automation with Python" (ESRI 2022).

There are some significant limits to what you can do with ArcPy, and it is primarily useful for automating detailed, repetitive, or tedious tasks (Zandbergen 2020, pp 356).

Python code can be executed in a variety of different ways in an ESRI environment:

Python Window Commands

The ArcGIS Pro Python window allows a user to type in and run individual Python commands.

You start the Python window from Analysis, Python, Python window.

In this example:

aprx = arcpy.mp.ArcGISProject('current')

streetmap = aprx.listMaps()[0]

streetmap.addDataFromPath("https://services.arcgis.com/GL0fWlNkwysZaKeV/arcgis/rest/services/Minn_2020_Power_Plants/FeatureServer")
Adding a feature service layer from the Python console

Python Window Scripts

Tasks in ArcGIS Pro commonly require sequences of operations, and those operations can be performed using sequences of Python expressions. A script is a sequence of commands collected into a single text file that can be run by the software. Scripts can be used to organize complex sequences of commands (workflows) and automate tedious tasks.

The advantages of using scripts instead of the ArcGIS Pro graphical user interface (GUI) include:

However, there are disadvantages as well:

Script files can be executed from the ArcGIS Pro Python console by right-clicking on the console prompt line and selecting Load Data.

Executing a script from the ArcGIS Pro Python console

Standalone Scripts

You may encounter situations where you would like to be able to run a script without having to go through the effort of starting ArcGIS Pro and clicking buttons to run your script. An example might be an automation of a the same task on multiple files.

Running a standalone ArcPy script outside of the ArcGIS Pro user interface

Notebook Scripts

Notebooks are interleaved cells of code and descriptive text that are useful for presenting the results of data analyzed with Python code.

Notebooks are good places to put scripts that are specific to particular projects, since notebooks are saved with project packages.

A Python notebook

Toolbox Scripts

Scripts that represent generalizable tasks can be incorporated into tools with parameters that allow execution in the same manner as native tools from the ArcGIS Pro toolboxes.

Toolboxes are good places to put scripts that can be useful across multiple different projects.

A Python toolbox script

ArcGIS API

ArcGIS API for Python is a "library for performing GIS visualization, analysis, data management, and GIS system administration tasks" (ESRI 2022).

ArcGIS API for Python

Example Code

The following is sequence of Python code that demonstrates usage of ArcPy functions.

The imagined scenario is that you work for a city department of transportation. The DOT is planning to resurface a street and wants to mail flyers that businesses and residential building managers can post so customers and residents can plan for disruptions to access. You need to create a list of addresses of businesses and residences on or near the construction project.

Acquire the Data

The data used in this example will be address points and street midlines from the Lake County, Illinois Open Data and Records Hub https://data-lakecountyil.opendata.arcgis.com/.

Downloading Lake County data

Start a New Project

All the source and output data will be kept together in a single project.

Starting a new project

Start the Script

Start a new script and save it into the main project directory.

# Example join script
# Michael Minn - 8/31/2022

import arcpy

aprx = arcpy.mp.ArcGISProject('current')

street_map = aprx.listMaps()[0]
Starting a new script

Import the Shapefiles

Shapefiles can be added to maps by passing the name of the .shp file in the shapfile to the map function addDataFromPath().

data_path = "\\\\192.168.100.3\\DeptUsers\\minn2\\Downloads"

street_path = data_path + "\\Street_Centerlines.shp"

street_layer = street_map.addDataFromPath(street_path)

street_layer.name = "streets"

addr_path = data_path + "\\Address_Points.shp"

addr_layer = street_map.addDataFromPath(addr_path)

addr_layer.name = "addresses"
Adding a shapefile

Select the Join Feature

A definition query is "a request that examines feature or tabular attributes based on user-selected criteria and displays only those features or records that satisfy the criteria" (ESRI 2022).

This definition query selects the 700 - 900 blocks of North Butrick Street.

The FromAddr_L and ToAddr_L fields in this data set indicate the range of street numbers on the left side of each street segment feature. The exact names of these parameters may vary depending on your data source.

street_layer.definitionQuery = "(WHOLE_NAME = 'N Butrick St') " \
				"AND (FromAddr_L >= 700) " \
				"AND (ToAddr_L <= 999) " \
				"AND (IncMuni_L = 'WAUKEGAN')"
Setting a definition query

Select and Export

The SelectLayerByLocation() tool can be used to select the address points (addr_layer) within 50 meters of the construction site (the selected street segments in the street_layer).

The ExportFeatures() tool can them be used to export the selected address points to a new feature class. By default, the out_features parameter (second parameter) is placed in the project database under the given name.

The listLayers() function is then used to find the original address point layer and hide it so that only the new layer of affected addresses is visible.

arcpy.management.SelectLayerByLocation(addr_layer, "WITHIN_A_DISTANCE", street_layer, "50 meters")

arcpy.conversion.ExportFeatures(addr_layer, "affected")

street_map.listLayers("addresses")[0].visible = 0
Selecting and exporting addresses affected by construction

Export the Attributes

You can export the attribute table for the affected locations to a .csv file of addresses that can be imported into Word to create cover letters or mailing labels.

arcpy.conversion.TableToTable("affected", data_path, "affected.csv")
Exporting attributes

Google Street View

If you want to view the area in Google Maps street view for ground truthing, Right-click on the map and select Copy Coordinates and copy those coordinates into the Google Maps search bar.

Viewing the area in Google Maps street view

Load Code

Although the videos above demonstrate running sets of commands interactively from the console, the point of collecting them in a script is to be able to run them directly from the file.

You can run an entire file from the Python wiindow by right-clicking on the console prompt, selecting Load Code, and pressing the enter key when the commands load.

Running a script from the ArcGIS Pro Python window

Notebook

Although notebooks are commonly used for presenting data analysis, they can also be used to store special purpose scripts so they are saved as part of project packages.

Running a script from the ArcGIS Pro Python window

Standalone Scripts

There may be occasions where you would like to be able to run scripts without having to start and monitor ArcGIS Pro.

While the simple script used in this tutorial is not a particularly useful example, a few tweaks to the code will allow it to be run as a "standalone" script.

# Standalone join script
# Michael Minn - 8/31/2022

import arcpy

aprx = arcpy.mp.ArcGISProject('\\\\192.168.100.3\\DeptUsers\\minn2\\Documents\\ArcGIS\\Projects\\Street Work\\Street Work.aprx')
                    
street_map = aprx.listMaps()[0]


data_path = "\\\\192.168.100.3\\DeptUsers\\minn2\\Downloads"

street_path = data_path + "\\Street_Centerlines.shp"

street_layer = street_map.addDataFromPath(street_path)

street_layer.name = "streets"

addr_path = data_path + "\\Address_Points.shp"

addr_layer = street_map.addDataFromPath(addr_path)

addr_layer.name = "addresses"

street_layer.definitionQuery = "(WHOLE_NAME = 'N Butrick St') AND (FromAddr_L >= 700) AND (ToAddr_L <= 999)"

arcpy.management.SelectLayerByLocation(addr_layer, "WITHIN_A_DISTANCE", street_layer, "50 meters")

arcpy.conversion.ExportFeatures(addr_layer, data_path + "\\affected.shp")

arcpy.conversion.TableToTable(data_path + "\\affected.shp", data_path, "affected.csv")

When ArcPy is installed, a version of IDLE with an appropriate run time environment is also installed that will allow "standalone" scripts to run outside of ArcGIS Pro.

Running a standalone script