Searching...
English
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Python Scripting for ArcGIS Pro

Python Scripting for ArcGIS Pro

by Paul Zandbergen 2020 412 pages
4.13
23 ratings
Listen
2 minutes
Try Full Access for 7 Days
Unlock listening & more!
Continue

Key Takeaways

1. Python is the Essential Scripting Language for ArcGIS Pro Automation

Python scripting allows you to automate tasks in ArcGIS® Pro that would be cumbersome using the regular menu-driven interface.

Automate tedious tasks. Python has become an indispensable skill for GIS professionals, enabling the automation of repetitive and complex tasks within ArcGIS Pro. Imagine converting a thousand shapefiles to geodatabase feature classes; manually running the tool repeatedly is inefficient and error-prone. Python scripting offers a robust solution, accomplishing such tasks with just a few lines of code, saving immense time and effort.

Versatile and accessible. Python's popularity stems from its simplicity, open-source nature, and cross-platform compatibility. It's an interpreted, object-oriented language, making it easier to learn and more portable than compiled languages like C++. While Esri didn't develop Python, it has fully embraced it as the preferred scripting language for ArcGIS Pro, which exclusively uses Python 3, a significant update from Python 2 used in older ArcGIS Desktop versions.

Beyond GIS applications. The skills acquired in Python scripting for ArcGIS Pro extend far beyond the GIS realm. Python is a general-purpose language used in web development, data science, machine learning, and system automation. This versatility means your investment in learning Python will benefit you in numerous other professional and personal endeavors, making it a highly valuable asset in today's technology-driven world.

2. Mastering Python Editors and the ArcGIS Pro Python Window

A Python editor has a menu-driven interface and tools for organizing and testing Python scripts to make working with Python easier.

Choose your workspace. While Python can be run from a command prompt, specialized Python editors, also known as Integrated Development Environments (IDEs), offer a more productive environment for writing and testing scripts. Popular choices include IDLE (the default Python editor), Spyder (favored in scientific computing), and PyCharm (a feature-rich professional IDE). Each offers syntax highlighting, error checking, and debugging tools, though their setup and interface may vary.

Seamless ArcGIS Pro integration. ArcGIS Pro features a built-in Python window, acting as an interactive interpreter directly within the application. This window is ideal for quickly testing code snippets, experimenting with syntax, and executing geoprocessing tools. It offers autocompletion, syntax assistance, and integrates with geoprocessing history, allowing users to drag and drop data layers or previously run tools to generate Python code.

Configuration is key. To ensure your chosen IDE works correctly with ArcGIS Pro, proper configuration is crucial. This often involves setting up a virtual environment, like arcgispro-py3, which bundles the specific Python version and necessary packages for ArcGIS Pro. For Spyder, this typically means cloning the default environment via ArcGIS Pro's Python Package Manager. PyCharm, installed separately, requires manual configuration to point to the correct ArcGIS Pro Python interpreter, offering flexibility for managing multiple environments.

3. ArcPy: Your Gateway to Geoprocessing Tools and Data Management

The geoprocessing functionality of ArcGIS Pro can be accessed through Python using the ArcPy package.

Unlocking ArcGIS Pro's power. ArcPy is the cornerstone for Python scripting in ArcGIS Pro, providing a comprehensive package of modules, functions, and classes that expose all geoprocessing tools. Importing arcpy is usually the first step in any script, as it not only makes these tools available but also performs crucial license checks, ensuring you have the necessary ArcGIS Pro product level and extensions (like Spatial Analyst or 3D Analyst).

Consistent tool access. Geoprocessing tools in ArcPy are accessed by their name and toolbox alias, such as arcpy.Clip_analysis() or arcpy.analysis.Clip(). This consistent syntax, along with the ability to pass parameters as variables, makes scripts flexible and reusable. ArcPy also offers "nontool functions" like arcpy.Exists() or arcpy.ListFeatureClasses(), which perform essential data management tasks not available as standalone geoprocessing tools.

Beyond basic parameters. Complex tool parameters, such as spatial references or environment settings, are handled efficiently using ArcPy classes. For instance, arcpy.env.workspace sets the default data location, and arcpy.SpatialReference() creates objects representing coordinate systems, simplifying their use in tool parameters. ArcPy also provides robust mechanisms for retrieving geoprocessing messages (arcpy.GetMessages()) and managing licenses (arcpy.CheckExtension()), crucial for building reliable and informative scripts.

4. Effective Data Exploration and Listing with ArcPy Functions

ArcPy includes several functions for exploring spatial data, including checking for the existence of datasets and describing datasets in a workspace.

Validate and understand your data. Before processing, scripts often need to verify data existence and properties. arcpy.Exists() checks if a dataset, workspace, or layer exists, preventing runtime errors. For deeper insights, arcpy.Describe() and arcpy.da.Describe() (the recommended, dictionary-based version) dynamically return dataset properties like shapeType, datasetType, and spatialReference, crucial for validating inputs against tool requirements.

Inventorying for automation. Batch processing, a core use case for scripting, relies on ArcPy's List*() functions to inventory data within a workspace. Functions like arcpy.ListFeatureClasses(), arcpy.ListRasters(), and arcpy.ListFields() return Python lists of elements, which can then be iterated over using for loops to apply operations to each item. These functions support wildcards and type filters for precise selection.

Navigating complex structures. Handling diverse data formats (shapefiles, geodatabases) and nested folder structures can be challenging. arcpy.da.Walk() is a powerful function designed to traverse these complex hierarchies, returning tuples of directory paths, folder names, and filenames at each level. This allows scripts to systematically locate and process all relevant data, regardless of its location or type, making batch operations highly efficient.

5. Robust Scripting Through Debugging and Error Handling

No matter how careful you are in writing code, errors are bound to happen.

Anticipate and identify errors. Scripting inevitably involves encountering errors, which fall into three main categories: syntax errors (preventing execution), exceptions (stopping execution mid-process), and logic errors (producing undesired results). Python IDEs like Spyder and PyCharm offer interactive syntax checking, highlighting issues like missing colons or inconsistent indentation as you type, significantly aiding in early error detection.

Systematic debugging strategies. When exceptions or logic errors occur, debugging becomes essential. Effective methods include:

  • Reviewing error messages: ArcPy's ExecuteError exceptions often provide specific error codes and descriptions, guiding troubleshooting.
  • Adding print statements: Temporarily inserting print() calls helps track variable values and execution flow.
  • Commenting out code: Isolating problematic sections by commenting them out (##) helps pinpoint the source of an error.
  • Using a debugger: IDEs provide debuggers that allow stepping through code line-by-line, setting breakpoints, and inspecting variable states, offering fine-grained control over execution.

Proactive error handling. Beyond debugging, robust scripts incorporate try-except statements to gracefully handle anticipated and unanticipated exceptions. A try block encloses code that might raise an exception, while except blocks catch specific (named) or general (unnamed) exceptions, preventing script termination. The finally block ensures cleanup tasks, like releasing data locks, always execute. This approach makes scripts more user-friendly and resilient to unexpected inputs or conditions.

6. Manipulating Tabular Data and SQL Expressions with Cursors

A cursor is a database technology term for accessing a set of records in a table.

Direct data interaction. Cursors are fundamental for interacting with tabular data in ArcGIS Pro, allowing scripts to read, insert, update, or delete records (rows) in tables or feature classes. The arcpy.da module provides three efficient cursor classes: SearchCursor (for retrieving rows), InsertCursor (for adding new rows), and UpdateCursor (for modifying or deleting existing rows). Using with statements with cursors is best practice, ensuring proper closure and release of data locks.

Powerful querying with SQL. Cursors, particularly SearchCursor and UpdateCursor, can incorporate SQL WHERE clauses to filter records based on attribute criteria. The arcpy.AddFieldDelimiters() function is crucial here, automatically adding the correct field delimiters (e.g., double quotes for shapefiles, no quotes for enterprise geodatabases) to ensure valid SQL syntax across different data formats. This allows for precise data selection directly within your Python script.

Beyond simple queries. While WHERE clauses are common, cursors also support additional SQL clauses like DISTINCT (for unique values), ORDER BY (for sorting), and GROUP BY (for aggregation), offering more control over the returned data. Furthermore, Python expressions can be used directly within ArcGIS Pro's Calculate Field tool, allowing complex calculations, string manipulations, and even custom Python functions (via code blocks) to update field values, providing immense flexibility for data transformation.

7. Constructing and Modifying Geometries Programmatically

You can work with geometry objects and their properties by setting a cursor on the geometry field, typically named Shape, of a feature class.

Accessing feature anatomy. Every feature in a feature class is defined by its geometry, composed of vertices (x,y coordinates). ArcPy allows direct interaction with these geometries using cursors set on the SHAPE@ token, which provides the full geometry object. For specific properties like length or centroid coordinates, geometry tokens like SHAPE@LENGTH or SHAPE@XY offer faster access, avoiding the overhead of loading the entire geometry.

Building geometries from scratch. ArcPy provides a suite of geometry classes to construct new features:

  • arcpy.Point(): Creates individual vertices.
  • arcpy.Array(): Collects multiple Point objects to form parts of a line or polygon.
  • arcpy.PointGeometry(), arcpy.Polyline(), arcpy.Polygon(): Assemble Point or Array objects into complete geometry features, optionally assigning a spatial reference.
    This programmatic construction is vital for tasks like creating features from tabular coordinate data.

Reading and writing complex features. Scripts can iterate through existing feature classes to extract vertex coordinates, handling complexities like multipart features (e.g., Hawaii's islands as one feature) and polygons with holes (distinguishing exterior and interior rings separated by null points). Conversely, new geometries can be written to a feature class using an InsertCursor or by directly copying geometry objects with tools like CopyFeatures(). This fine-grained control over geometry allows for advanced data creation and manipulation.

8. Advanced Raster Processing with Spatial and Image Analyst Modules

ArcPy includes the arcpy.sa (Spatial Analyst) and arcpy.ia (Image Analyst) modules, which provide access to many geoprocessing tools to work with imagery and raster data.

Specialized raster capabilities. Rasters, representing surfaces as grids of cells, are central to many GIS analyses. ArcPy extends Python's capabilities for raster data through the arcpy.sa (Spatial Analyst) and arcpy.ia (Image Analyst) modules. These modules provide access to hundreds of specialized geoprocessing tools and integrate map algebra directly into Python, allowing for powerful cell-by-cell operations using intuitive operators like +, *, and &.

Working with raster objects. The arcpy.Raster() class creates raster objects, which can be inputs or outputs of geoprocessing tools and map algebra expressions. These objects have properties like bandCount, pixelType, and spatialReference, and a crucial save() method to make temporary raster outputs permanent. This object-oriented approach streamlines complex raster workflows, allowing intermediate results to remain in memory for efficiency.

Fine-grained control and advanced functions. Beyond standard tools, the arcpy.sa and arcpy.ia modules offer:

  • Classes for tool parameters: Such as RemapValue or NbrRectangle to define reclassification tables or neighborhood shapes.
  • Raster Cell Iterator (RCI): A powerful feature allowing iteration through individual raster cells (raster_object[i, j]) to read and write values, enabling highly customized cell-based processing not possible with standard tools.
  • Raster functions: Many ArcGIS Pro raster functions (e.g., WindChill()) are replicated as arcpy.sa or arcpy.ia functions, offering efficient, on-the-fly processing capabilities.
  • NumPy integration: RasterToNumPyArray() and NumPyArrayToRaster() facilitate conversion between rasters and NumPy arrays, opening doors to advanced scientific computing and statistical analysis using other Python packages like SciPy.

9. Automating Mapping Workflows with the ArcPy.mp Module

The ArcPy mapping module automates ArcGIS Pro workflows to speed up repetitive tasks.

Programmatic map manipulation. The arcpy.mp module is designed to automate mapping tasks within ArcGIS Pro projects (.aprx files) and layer files (.lyrx). While it cannot create new projects or most map elements from scratch, it excels at modifying existing ones. This includes tasks like updating data sources, changing symbology, generating reports, and exporting layouts, making it invaluable for repetitive cartographic production.

Project-centric workflow. Interaction begins by referencing an ArcGISProject object, either from a file path (arcpy.mp.ArcGISProject("C:/path/to/project.aprx")) or the current ArcGIS Pro session ("CURRENT"). This object provides access to project properties (e.g., defaultGeodatabase) and methods to list and manage its contents, such as listMaps(), listLayouts(), and importDocument() for integrating older map documents.

Controlling layers and symbology. Within a map, Map.listLayers() retrieves Layer objects, which represent symbolized data sources. These objects allow modification of display properties like transparency and visible status. For detailed symbology control, the Layer.symbology property provides access to Symbology objects, which can be updated using updateRenderer() (for feature layers) or updateColorizer() (for raster layers) to apply graduated colors, unique values, or even specific symbols from a gallery.

Layout management and export. The arcpy.mp module also enables automation of layout tasks. ArcGISProject.listLayouts() provides Layout objects, which in turn offer listElements() to access and modify individual layout components like MapFrameElement (for map frames), TextElement (for titles), and LegendElement. Crucially, Layout objects include methods like exportToPDF(), exportToJPEG(), and exportToPNG(), allowing for automated map series generation and output.

Last updated:

Want to read the full book?
Listen2 mins
Now playing
Python Scripting for ArcGIS Pro
0:00
-0:00
Now playing
Python Scripting for ArcGIS Pro
0:00
-0:00
1x
Voice
Speed
Dan
Andrew
Michelle
Lauren
1.0×
+
200 words per minute
Queue
Home
Swipe
Library
Get App
Create a free account to unlock:
Recommendations: Personalized for you
Requests: Request new book summaries
Bookmarks: Save your favorite books
History: Revisit books later
Ratings: Rate books & see your ratings
250,000+ readers
Try Full Access for 7 Days
Listen, bookmark, and more
Compare Features Free Pro
📖 Read Summaries
Read unlimited summaries. Free users get 3 per month
🎧 Listen to Summaries
Listen to unlimited summaries in 40 languages
❤️ Unlimited Bookmarks
Free users are limited to 4
📜 Unlimited History
Free users are limited to 4
📥 Unlimited Downloads
Free users are limited to 1
Risk-Free Timeline
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Feb 2,
cancel anytime before.
Consume 2.8× More Books
2.8× more books Listening Reading
Our users love us
250,000+ readers
Trustpilot Rating
TrustPilot
4.6 Excellent
This site is a total game-changer. I've been flying through book summaries like never before. Highly, highly recommend.
— Dave G
Worth my money and time, and really well made. I've never seen this quality of summaries on other websites. Very helpful!
— Em
Highly recommended!! Fantastic service. Perfect for those that want a little more than a teaser but not all the intricate details of a full audio book.
— Greg M
Save 62%
Yearly
$119.88 $44.99/year/yr
$3.75/mo
Monthly
$9.99/mo
Start a 7-Day Free Trial
7 days free, then $44.99/year. Cancel anytime.
Scanner
Find a barcode to scan

We have a special gift for you
Open
38% OFF
DISCOUNT FOR YOU
$79.99
$49.99/year
only $4.16 per month
Continue
2 taps to start, super easy to cancel
Settings
General
Widget
Loading...
We have a special gift for you
Open
38% OFF
DISCOUNT FOR YOU
$79.99
$49.99/year
only $4.16 per month
Continue
2 taps to start, super easy to cancel