Introduction to ArcPy
Introduction
One of the key advantages of learning Python is gaining access to the vast ecosystem of third-party packages. We’ve already explored a few, but now we’re going to dive into one of the most powerful and comprehensive: the ArcPy package.
By simply adding the line import arcpy to a script or notebook, we unlock nearly all the capabilities of ArcGIS Pro—plus more—directly from our coding environment. That’s exciting, but it can also feel overwhelming. How do we begin to learn such a large and complex package?
The good news is that ArcPy is well-organized and well-documented. We don’t need to learn everything at once. By understanding how the package is structured and recognizing patterns in how its functions and classes are organized (we’ll define those terms soon), we can quickly build a working familiarity. Combined with our knowledge of spatial analysis, this will enable us to start performing GIS tasks through code in no time.
So, in this section, we examine several examples of ArcPy in action, starting with a few simple notebook exercises, then revisiting our Hurricane Tracking Tool workflow as it looks in code. Then, we’ll move over to VS Code and explore how Python scripts and the ArcPy package can [1] automate geospatial tasks and [2] create ArcGIS Pro scripting tools. And in each of these tasks, we’ll explore various aspects of the ArcPy package such that you can feel competent integrating it into other coding projects you wish!
Let’s start by running a simple ArcPy command to get a feel for how it works.
For example, we can use ArcPy to list the feature classes in a workspace (a folder or geodatabase):
import arcpy
arcpy.env.workspace = "C:/GISData/MyProject.gdb"
feature_classes = arcpy.ListFeatureClasses()
print(feature_classes)
This snippet sets the workspace and prints out the names of all the feature classes it contains. Simple, but powerful! You’re already tapping into ArcGIS functionality with just a few lines of code.
Before we jump into writing more complex scripts, let’s clarify a few key ideas.
In Python—and in ArcPy specifically—we often work with functions and classes:
- A function is like a reusable action. For example,
arcpy.ListFeatureClasses()
is a function that retrieves feature classes from a workspace. - A class is a kind of blueprint for an object. ArcPy includes classes for things like spatial references, geometries, and cursors.
Understanding these building blocks will help us navigate the ArcPy documentation and use the tools more effectively.
Since ArcPy mirrors many of the tools you’re already familiar with from ArcGIS Pro, you’ll recognize a lot as we go. Many ArcGIS tools have a direct counterpart in ArcPy, often with similar names and parameters.
We’ll focus on:
- Running tools (like Buffer, Clip, Select) through ArcPy
- Managing data (listing, copying, updating feature classes)
- Automating workflows with loops and logic
- Handling spatial references and geometry objects
You don’t need to memorize everything—learning how to look things up in the documentation is just as important as knowing what tools exist.