Python Scripting for ArcGIS Pro
()
About this ebook
Unlock the power of Python in ArcGIS® Pro with this definitive, easy-to-follow guide designed for users with limited programming or scripting experience.
Get started learning to write Python scripts to automate tasks in ArcGIS Pro with Python Scripting for ArcGIS Pro. This book begins with the fundamentals of Python programming and then dives into how to write useful Python scripts that work with spatial data in ArcGIS Pro. You’ll learn how to use geoprocessing tools; describe, create, and update data; and execute specialized tasks. With step-by-step instructions, practical examples, and insightful guidance, you’ll be able to write scripts that will automate and improve your ArcGIS Pro workflows.
This third edition has been revised for ArcGIS Pro 3.2 and Python 3.9.18 and includes updated images; a fully updated chapter 2; and expanded chapters 4, 8, 9, and 10.
The key topics you will learn include:
- Python fundamentals
- Setting up a Python editor
- Automating geoprocessing tasks using ArcPy™
- Exploring and manipulating spatial and tabular data
- Working with geometries using cursors
- Working with rasters and map algebra
- Map scripting
- Debugging and error handling
Helpful points to remember, key terms, and review questions are included at the end of each chapter to reinforce your understanding of Python. Corresponding data and tutorials are available online.
Whether you’re new to Python or already have some experience, Python Scripting for ArcGIS Pro is the go-to resource for learning the versatility of Python coding to solve problems and enhance productivity and efficiency in ArcGIS Pro.
Paul A. Zandbergen
Dr. Paul A. Zandbergen is a professor in the GIS program at Vancouver Island University. His teaching includes courses in introductory GIS, spatial analysis and modeling, spatial statistics, and GIS programming. His research focuses on the robustness of spatial analytical techniques in the areas of crime analysis, environmental science, public health, spatial ecology, and water resources. His research has been funded by the National Science Foundation, the National Institute of Justice, and the National Institutes of Health. He has published over 50 journal articles and book chapters. He is the author of Python Scripting for ArcGIS, published in 2013 with Esri Press. He lives with his family in Vancouver, British Columbia, Canada.
Related to Python Scripting for ArcGIS Pro
Related ebooks
Advanced Python Scripting for ArcGIS Pro Rating: 0 out of 5 stars0 ratingsPro Angular 6 Rating: 0 out of 5 stars0 ratingsGetting to Know ArcGIS Pro 2.8 Rating: 3 out of 5 stars3/5Python High Performance, Second Edition: Build high-performing, concurrent, and distributed applications Rating: 0 out of 5 stars0 ratingsHands-On Functional Programming in Rust: Build modular and reactive applications with functional programming techniques in Rust 2018 Rating: 0 out of 5 stars0 ratingsRPG TnT: 101 Dynamite Tips 'n Techniques with RPG IV Rating: 5 out of 5 stars5/5Graph Data Processing with Cypher: A practical guide to building graph traversal queries using the Cypher syntax on Neo4j Rating: 0 out of 5 stars0 ratingsPro Angular 9: Build Powerful and Dynamic Web Apps Rating: 0 out of 5 stars0 ratingsBeginning Perl Programming: From Novice to Professional Rating: 0 out of 5 stars0 ratingsModern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools Rating: 0 out of 5 stars0 ratingsBeginning Rust Programming Rating: 0 out of 5 stars0 ratingsCreating ASP.NET Core Web Applications: Proven Approaches to Application Design and Development Rating: 0 out of 5 stars0 ratingsGetting to Know Web GIS Rating: 5 out of 5 stars5/5Odoo 10 Development Essentials Rating: 0 out of 5 stars0 ratingspytest Quick Start Guide: Write better Python code with simple and maintainable tests Rating: 0 out of 5 stars0 ratingsHands-On Reactive Programming with Python: Event-driven development unraveled with RxPY Rating: 0 out of 5 stars0 ratingsPractical Oracle JET: Developing Enterprise Applications in JavaScript Rating: 0 out of 5 stars0 ratingsPractical API Architecture and Development with Azure and AWS: Design and Implementation of APIs for the Cloud Rating: 0 out of 5 stars0 ratingsGIS Tutorial for ArcGIS Pro 3.1 Rating: 0 out of 5 stars0 ratingsEssential TypeScript 4: From Beginner to Pro Rating: 0 out of 5 stars0 ratingsRust Servers, Services, and Apps Rating: 0 out of 5 stars0 ratingsPractical Network Automation,: A beginner's guide to automating and optimizing networks using Python, Ansible, and more Rating: 0 out of 5 stars0 ratingsPython Data Analytics: With Pandas, NumPy, and Matplotlib Rating: 2 out of 5 stars2/5Q Tips: Fast, Scalable, and Maintainable Kdb+ Rating: 0 out of 5 stars0 ratingsAI as a Service: Serverless machine learning with AWS Rating: 1 out of 5 stars1/5Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software Rating: 0 out of 5 stars0 ratings
Internet & Web For You
How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsHow to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsNo Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Wordpress for Beginners: The Easy Step-by-Step Guide to Creating a Website with WordPress Rating: 5 out of 5 stars5/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Podcasting For Dummies Rating: 4 out of 5 stars4/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Wireless Hacking 101 Rating: 5 out of 5 stars5/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5Cybersecurity All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsThe Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Lying and Lie Detection: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsAn Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5
Reviews for Python Scripting for ArcGIS Pro
0 ratings0 reviews
Book preview
Python Scripting for ArcGIS Pro - Paul A. Zandbergen
Chapter 1
Introducing Python
1.1 Introduction
Python is a programming language that is both simple and powerful. For those who have struggled to learn programming languages, this fact may come as a pleasant surprise. This chapter describes some of the main features of Python and its use as a programming language to write scripts for ArcGIS® Pro. The logic and structure of Python Scripting for ArcGIS Pro, third edition, is described, followed by examples of how Python is used.
This book represents a completely updated and revised version of Python Scripting for ArcGIS, published by Esri Press (Redlands, California) in 2013, which was written for ArcGIS Desktop 10.x. The current book is written for ArcGIS Pro. Not only does it incorporate the changes in the ArcGIS software and the Python programming language, but it also includes functionality not previously available in ArcGIS.
1.2 Exploring the features of Python
Python has several features that make it the programming language of choice for working with ArcGIS Pro. Among them:
It is simple and easy to learn. Python is easy to learn compared with other highly structured programming languages such as C++. The syntax, or rules that define the combinations of symbols that form correctly structured statements or expressions, is simple, which gives you more time to focus on solving problems rather than learning the language itself. Key terms used in each chapter appear in blue and are included alphabetically at the end of the chapter.
It is free and open source. Python is free and open-source software (FOSS). You can freely distribute copies of the software, read the source code, make changes to it, and use pieces of it in new free programs. One of the reasons Python works so well is that it has been created, and is constantly being
improved, by an active and dedicated user community. The FOSS nature of Python makes it possible for Esri® to distribute Python within its ArcGIS Pro software.
It is cross-platform. Python is supported on different platforms, including Windows, macOS, and Linux. Python programs work on any of these platforms with minimal change or often no change. Because ArcGIS Pro runs only on Windows, it may not seem like a big advantage, but the user community for Python is large, in part because of its cross-platform nature.
It is interpreted. Many programming languages require that a program be converted from the source language, such as C++, into binary code that the computer can understand. This conversion requires a compiler with various options. Python is an interpreted language, which means it does not need compilation to binary code before you run it. You simply run the program directly from the source code, which makes Python easier to work with and much more portable than some other programming languages.
It is object oriented. Python is an object-oriented programming (OOP) language. An object-oriented program involves a collection of interacting objects, as opposed to the conventional list of tasks. Many modern programming languages support object-oriented programming.
These features make Python one of the most popular programming languages in the world, and its popularity continues to grow. It has become a widely used language for teaching introductory computer programming courses in colleges and universities. The popularity of Python also means numerous resources are available for learning it.
Python has many other benefits, including that its open-source nature leads to the development of numerous third-party packages that can be installed to add functionality.
It is important to recognize that Python was not created to work specifically with GIS software or geospatial datasets. It is a general-purpose programming language used for numerous tasks, from developing web apps to creating games and from automating operating system procedures to developing machine learning algorithms. This versatility means that the Python skills you learn in this book will also be helpful for tasks other than those related to GIS. And perhaps because of its general-purpose nature, Python is widely used in the geospatial community. This adaptability is, in part, because Python is easy to learn and can be used for many different tasks, but it is also because Python has a large developer community, and with it, a growing library of packages to extend its functionality, including for geospatial tasks. Esri has fully embraced Python as one of the key programming languages to work with ArcGIS Pro. Several other GIS software applications also use Python as a programming language, and your investment in learning Python will pay off beyond its use in ArcGIS Pro.
1.3 Scripting versus programming
Although Python is a programming language, it is often referred to as a scripting language. So what is the difference? In general, a scripting language refers to automating certain functionality within another program, whereas a programming language involves the development of more sophisticated
multifunctional applications. Scripting is a programming task that allows you to connect diverse existing components to accomplish a new, related task. Scripting is the glue that allows you to put various existing elements together. Programming, on the other hand, allows you to build components from scratch, as well as the applications that incorporate these components. Languages that work with these lower-level primitives and the raw resources of the computer are referred to as system languages. Examples of system languages include C++ and .NET languages such as C#. Scripting languages use built-in higher-level functions and mask some of the details that a system language manages. Examples of scripting languages include Python, Perl, PHP, R, and Ruby.
Esri, for example, relies primarily on C++ as the programming language to create ArcGIS Pro software and all the components, or objects, in the software. You can then use .NET languages to write your own software that uses these same objects as well as create your own objects. However, you can also use scripting to access the existing functionality of ArcGIS Pro and connect those functions in new ways to extend that functionality.
One of the strengths of Python is that it is both a scripting language and a programming language, although it does not have quite the depth of a system language such as C++. You can use it for relatively simple scripts as well as more advanced programming tasks. The focus of this book is writing Python scripts to carry out tasks in ArcGIS Pro. Python can also be used for application development, but these aspects of using Python are not addressed in this book. Python is used here as an interpreted language to work directly with the existing functionality in ArcGIS Pro.
1.4 Using scripting in ArcGIS Pro
ArcGIS Pro provides support for the use of Python as a scripting language. Python can access all the tools available in ArcGIS Pro, including those that are part of an extension. This feature makes Python scripting an attractive and efficient method for automating tasks. Although the same automation can be accomplished using a system language such as C++ or a .NET language, scripting often requires much less effort.
Python scripting has become a fundamental tool for GIS professionals to extend the functionality of ArcGIS Pro and automate workflows. In earlier releases of ArcMap™ software, a typical approach was to use the built-in Visual Basic for Applications (VBA) programming tools. With ArcMap 10.x, VBA was largely replaced with Python, and the use of Python is only strengthened within ArcGIS Pro. Although application development will continue to employ languages such as those used by .NET, Python has several advantages, especially for GIS professionals who are not full-time programmers.
As the scripting language of choice to work with ArcGIS Pro, Python is included in every ArcGIS Pro installation. Python is also directly embedded in many tools in ArcGIS Pro. For example, Python is one of the standard expression types for field calculations. As another example, several geoprocessing tools in ArcGIS Pro consist of Python scripts, even though the casual user does not necessarily notice this fact (or need to). Esri has officially embraced Python as the preferred scripting tool for working with desktop ArcGIS, and ArcGIS Pro includes further integration of Python within the desktop software interface.
1.5 Python history and versions
Python was created by Guido van Rossum at the Centrum voor Wiskunde en Informatica (CWI) in the Netherlands and was first released in 1991. Van Rossum remained active in the ongoing development of Python but was joined by numerous contributors. The name Python was inspired by the British comedy group Monty Python—it is a common misconception that the name has anything to do with the snake species.
Python’s features include basic data types such as strings, numbers, lists, and dictionaries, as well as many advanced elements common in object-oriented programming languages. The robustness of Python reflects the need to include the basic features that all programmers need, as well as more advanced functionality that is common in other, more complex programming languages.
Compared with other languages, Python has gone through a limited number of versions, reflecting a philosophy of incremental change and backward compatibility. Python 2 was introduced in 2000, and its most recent (and final) version is 2.7. With every version, new features were added, and over time some inconsistencies and design flaws crept into the code. Python 3 was released in 2008 as a major overhaul, with the primary goal to clean up the code base and remove redundancy. The most recent version at the time of this writing is 3.12.
Some of the changes made in Python 3 are fundamental, which result in breaking with the backward compatibility philosophy of Python. As a result, not all code written in Python 3 will work in Python 2. Some of the new functionality added in Python 3 has been added to Python 2, a process known as backporting. With careful attention to detail, you can write code that works in both versions.
Both versions of Python are currently in use, which can be confusing. The two versions will continue to coexist for some time, but officially maintenance of Python 2.7 stopped in 2020. Any existing code will continue to work, but no further improvements will be made to version 2.7. Chapter 4 covers the fundamentals of the Python language. Important for now is to address how the two versions relate to ArcGIS Pro.
Note: If you are new to learning Python, you should focus on learning Python 3, which is the focus of this book.
When Python was first selected as the scripting language to work with ArcMap 9.0, the version selected was Python 2. Since then, every new version of ArcGIS Desktop software has installed with the current version of Python 2. For example, ArcGIS Desktop 10.8.2 uses Python 2.7.18. When ArcGIS Pro was developed, it was designed from the beginning to use Python 3. At the time of writing, the most recent release of ArcGIS Pro is version 3.2, and this version works with Python 3.9.18. The choice of Python 3 was logical because of the impending retirement of Python 2, but it was also facilitated by the availability of other Python packages in version 3 that were not available when ArcGIS Desktop 10.0 was introduced in 2010.
The fact that ArcGIS Desktop 10.x uses Python 2.7, whereas ArcGIS Pro uses Python 3.9, has many implications. If you are going to write scripts for both versions, you must learn the differences between the two versions of Python, as well as the functionality of ArcGIS Desktop 10.x and ArcGIS
Pro. The same applies if you are already an experienced Python programmer and are planning to migrate scripts and tools from ArcGIS Desktop 10.x to ArcGIS Pro. Many resources and utilities exist to assist with this conversion.
The purpose of this book is to focus on writing scripts for ArcGIS Pro using Python 3. Some of the most relevant differences between ArcGIS Desktop 10.x and Python 2.7 are mentioned in selected chapters, but all the code in the book is written for ArcGIS Pro and Python 3. Although Python code is not 100 percent backward compatible between versions 3 and 2, it is, in principle, possible to write Python code that works for both versions. However, because of fundamental differences between ArcGIS Desktop 10.x and ArcGIS Pro, many scripts written for one version are unlikely to work in the other. Nonetheless, sometimes the differences are small.
If you are relatively new to learning ArcGIS, the only desktop software you may be familiar with is ArcGIS Pro. Good familiarity with ArcGIS Pro is expected for users of this book, but experience with Python is not required. If you are familiar only with ArcGIS Pro, you can essentially ignore the occasional note that refers to ArcGIS Desktop 10.x or Python 2.
Note: Many GIS users will continue to use both ArcGIS Desktop 10. x and ArcGIS Pro for some time to come. At the time of this writing, the most current versions are ArcMap 10.8.2 and ArcGIS Pro 3.2. The installation of ArcMap 10.8.2 includes the installation of Python 2.7.18.4, and the installation of ArcGIS Pro 3.2 includes the installation of Python 3.9.18. These two versions can run on the same computer. When working with ArcGIS Pro 3.2, you should use only Python 3.9.18. Chapter 2 covers how to ensure you are using the correct version of Python.
1.6 The structure of this book
Python Scripting for ArcGIS Pro consists of 11 chapters that explain the structure and syntax of Python and illustrate how to write scripts for ArcGIS Pro. Sample code is provided throughout the text. Review questions are included at the end of each chapter for discussion.
Chapter 1 introduces Python, explains why Python is used for writing scripts for ArcGIS Pro, and illustrates several example scripts that were developed using Python.
Chapter 2 explains how to use Python editors to write and test your code. Several different editors are used in this book, including IDLE and PyCharm. Chapter 2 also describes the Python window in ArcGIS Pro, which allows you to run one or several lines of Python code directly from within ArcGIS Pro. Code written in the Python window can be saved to a script, and existing code from a script can be loaded into the Python window.
Chapter 3 introduces the ArcGIS geoprocessing framework, including the use of tools and the ModelBuilder™ application. Experienced ArcGIS Pro users will be familiar with most of the material, but a good review will be beneficial. Knowing what is possible with the existing set of geoprocessing tools will be helpful in writing effective scripts. Similarly, Python scripts and ModelBuilder are often used in combination, so a good knowledge of ModelBuilder is recommended to get the most from Python scripting.
Chapter 4 explains the fundamentals of the Python language, including the use of data types and data structures, variables, keywords, statements and expressions, methods, functions, and modules; controlling workflow; and best practices for writing scripts. Chapter 4 covers the basic syntax of Python for the novice Python user. Experienced Python users will be familiar with this material.
Chapter 5 describes ArcPy, which makes it possible to perform many of the tasks commonly carried out in ArcGIS Pro using Python. ArcPy includes numerous modules, classes, and functions, which provide an effective way to integrate ArcGIS Pro and Python. Chapter 5 also includes working with data paths, environment settings, and licenses.
Chapter 6 includes techniques for data exploration, including describing data, as well as working with lists to characterize data before using it in other operations.
Chapter 7 describes error-handling techniques for anticipating common errors and making your code more robust. The code debugging environment, which allows you to test your code interactively, is also covered.
Chapter 8 introduces how to manipulate spatial and tabular data, including the use of cursors, searching for data, and working with tables and fields. The lessons in this chapter make it possible to use Python to run SQL queries on spatial data and to write expressions for field calculations.
Chapter 9 describes how to work with the geometric properties of spatial objects, including how to use the properties of existing features and create new features.
Chapter 10 describes how to work with rasters in Python, including the tools of the ArcGIS Spatial Analyst™ extension. The Spatial Analyst module of ArcPy contains many specialized map algebra operators and other classes for using rasters in spatial analysis.
Chapter 11 describes the ArcPy mapping module for automating mapping tasks. This chapter includes working with projects, maps, and layers, as well as exporting and printing layouts.
If you are an experienced developer and have already written scripts and tools in Python for ArcGIS Desktop 10.x and are looking to upgrade your skills to make the move to ArcGIS Pro, many of the chapters in this book will cover material you already know. You may want to focus on chapters that cover materials that have changed the most relative to the Python Scripting for ArcGIS book that was written for ArcGIS Desktop 10.x. These chapters include chapter 2 on working with Python editors and chapter 11 on map scripting.
Several chapters are also expanded and improved substantially compared with the previous book. Chapter 4 on Python fundamentals is completely revised to update all the syntax to Python 3, and new sections are added on working with tuples, dictionaries, and sets; print formatting; Boolean logic; working with long lines of code; and file management. Chapter 8 has a new section on Python expressions for field calculations. Chapter 9 is expanded substantially with more examples on how to
work with geometry objects. Chapter 10 on rasters has new sections on the expanded functionality of the Image Analyst and Spatial Analyst extensions, as well as working with the Raster Cell Iterator and using raster functions.
Several important topics with respect to Python scripting for ArcGIS Pro are not included in this book to maintain a manageable length. These topics are covered in the book Advanced Python Scripting for ArcGIS Pro, second edition (Esri Press, 2024). Topics include creating script tools and Python toolboxes; sharing tools; managing packages using conda; using third-party packages other than ArcPy; migrating scripts and tools from ArcGIS Desktop 10.x to ArcGIS Pro; and using the ArcGIS API for Python and Jupyter Notebook. These topics follow logically on the topics covered in the current book. Therefore, if you have mastered all the topics in Python Scripting for ArcGIS Pro, the next logical step is to continue with Advanced Python Scripting for ArcGIS Pro.
1.7 Exploring how Python is used
This section uses several examples to illustrate how Python is used to create scripts for working with ArcGIS Pro. These examples resemble the scripts used in later chapters in this book. One of the reasons for presenting these examples is for you to become familiar with looking at Python code. One of the best ways to learn how to write code is to work with existing examples. You are not expected to fully understand the code at this point, but the examples will give you a flavor of what is to come.
Example 1: Copying shapefiles to a geodatabase
A common task in ArcGIS Pro is to copy datasets from one format to another. For example, consider the scenario in which you have several shapefiles in a folder, and you must copy these shapefiles to a new feature dataset inside a file geodatabase. In addition, you want to copy only the polygon shapefiles. The figure shows a typical folder with several shapefiles.
Catalog view of a folder called Data with five shapefiles, including two polygon shapefiles.Accomplishing this task in ArcGIS Pro requires running several tools. You must run Create File Geodatabase and Create Feature Dataset to establish the desired database structure. Then you have several options to copy the shapefiles, including Feature Class to Feature Class, Feature Class to Geodatabase, or Copy Features. In ArcGIS Pro, you typically must run these tools for every shapefile you want to copy. The Feature Class to Geodatabase tool allows you to use multiple shapefiles as input features, but they must be selected manually in the tool dialog box. Running these tools for a handful of shapefiles is not difficult, but what if you had 100 or more shapefiles to copy? Running the tools 100 times would be cumbersome and prone to errors. This is where a Python script is beneficial.
The following script accomplishes the task. Read through the script to see whether you can follow the general logic of the script.
import arcpy, osÍws = C:/Data
gdb = Database.gdb
prj = myprojection.prj
arcpy.env.workspace = ws
arcpy.CreateFileGDB_management(ws, gdb)
sr = arcpy.SpatialReference(os.path.join(ws, prj))
arcpy.CreateFeatureDataset_management(gdb, Polygons
, sr)
fc_list = arcpy.ListFeatureClasses()
for fc in fc_list:
fc_desc = arcpy.Describe(fc)
if fc_desc.shapeType == Polygon
:
newfc = os.path.join(gdb, Polygons
, fc_desc.basename)
arcpy.CopyFeatures_management(fc, newfc)
The same code is also shown as a completed script in a Python editor.
Code as a completed script.There is no expectation that you fully understand the code at this point. The next chapters walk you through how to write and run this type of script. Chapter 2 on Python editors explains where to write this code. Chapter 4 on Python fundamentals explains the syntax used in a Python script. Chapters 5 and 6 cover the fundamentals of ArcPy to use geoprocessing tools in a Python script.
The example script can do more than what geoprocessing tools can do, such as creating a list of all the shapefiles in a folder and filtering out only those that consist of polygons. Without scripting, these are manual tasks in ArcGIS Pro.
The result of running the script is a new geodatabase with a feature dataset containing the copied polygon feature classes.
Catalog view of Data folder. Description
Data folder contains a file geodatabase called Database.gdb, a feature dataset called Polygons, and two polygon feature classes called parks and zip.
The 14 lines of carefully written code automate the entire task. The script can process any number of inputs because the code is written to read and process the contents of a folder, regardless of how many shapefiles are included. The cumbersome and repetitive task of manually copying 100 or more shapefiles is replaced by a single run of the Python script. Of course, it takes skill, time, and effort to develop the script, but once the script is written, it can be used multiple times and revised for different scenarios.
The bottom line is that the time and effort that go into writing a script are typically offset by the time savings that result from using the script to automate repetitive tasks.
Example 2: Creating points along a line
The next example is a bit more complex in terms of the Python code. Consider the example of a feature class containing one or more polylines. The task is to create points placed along each polyline at regularly spaced intervals. It requires reading the geometry of each feature and creating points based on the properties of the geometry relative to the interval. The figure shows an example of regularly spaced points along a polyline to illustrate what the Python script accomplishes.
On the left side is a single polyline, and on the right side is the same polyline overlaid with regularly spaced points.Note: ArcGIS Pro has a tool called Generate Points Along Lines that accomplishes this task. This geoprocessing tool is a script tool written in Python. The script used here is a simplified version of this script tool for the purpose of illustration.
The following script accomplishes the task. Read through the script to see whether you can follow the general logic. Notice how comments are added to make it easier to understand how the script works. The script includes options to place points by distance or by percentage, and an option to include the endpoints or not include img src=them.
# Import modules and set workspace
import arcpy, os
home = C:/Data
arcpy.env.workspace = home
# Script parameters
in_fc = route.shp
out_fc = points9.shp
interval = 500
use_percent = False
end_points = True
# Create output feature class
desc = arcpy.Describe(in_fc)
sr = desc.spatialReference
arcpy.CreateFeatureclass_management(home, out_fc, POINT
,
, , , sr)
# Add a field to transfer FID from input
fid_name = NEW_ID
arcpy.AddField_management(out_fc, fid_name, LONG
)
# Create new points based on input lines
with arcpy.da.SearchCursor(in_fc, [SHAPE@
, OID@
]) as search_cur:
with arcpy.da.InsertCursor(out_fc, [SHAPE@
,
fid_name]) as insert_cur:
for row in search_cur:
line = row[0]
if line:
if end_points:
insert_cur.insertRow([line.firstPoint, row[1]])
cur_length = interval
max_position = 1
if not use_percent:
max_position = line.length
while cur_length < max_position:
insert_cur.insertRow(
[line.positionAlongLine(cur_length,
use_percent), row[1]])
cur_length += interval
if end_points:
insert_cur.insertRow([line.lastPoint, row[1]])
The same code is also shown as a completed script in a Python editor.
Python script shown in a Python editor with the same code as in the text.Again, there is no expectation that you fully understand the code at this point, but you can probably identify elements that look familiar, especially with the help of the comments shown in red, with number signs in front.
This example script is longer and more complex than the first example. If you have not worked with Python or other programming languages, this code can look intimidating. However, the purpose of this book is to make you familiar with Python syntax and the logic behind it for carrying out tasks in ArcGIS Pro. The specific skills necessary to write this example script are explained in chapters 8 and 9. By the end of the book, you will be able not only to understand this script, but also to write scripts of similar complexity. With each chapter, your skills will build gradually, from writing your first line of code to authoring complex scripts that automate workflows in ArcGIS Pro.
Even if you are just starting to learn how to write Python scripts, you can benefit from these examples because they provide insight into why you would develop such scripts in the first place. Perhaps you have been using ArcGIS Pro for a while and have wondered why there is not a tool for a certain task. Or you have established a workflow that requires many repetitive tasks, and you are looking for a way to automate the steps. Having a strong motivation to develop a specific script will make it easier as you embark on learning the fundamentals of writing scripts.
Once you learn the basics of how to use Python for writing scripts, you will find that one of the best ways to keep learning Python scripting is to work with existing code. Using example code can also speed up the process of writing your own scripts. Soon you will be on your way to writing scripts that automate your own workflows.
Points to remember
ArcGIS Pro supports the use of scripting to automate workflows. Python is the preferred scripting language for working with ArcGIS Pro.
Python is not created by Esri. It is an open-source programming language and therefore can be distributed for free by third parties, including Esri.
Python is easy to use. There is a large user community, and there are many resources for learning Python. A growing set of packages for use in Python provide additional functionality.
ArcGIS Pro works with Python 3, whereas ArcGIS Desktop 10.x works with Python 2. This book covers only the use of ArcGIS Pro and Python 3. The Esri Press title Python Scripting for ArcGIS covers the use of Python 2 and ArcGIS Desktop 10.x.
One of the strengths of Python is that it is both a scripting language and a programming language. You can use it for relatively simple scripts as well as for more advanced programming tasks. Using Python to develop scripts for ArcGIS Pro is the focus of this book.
Python is an interpreted language, which means it does not need to be compiled. Python scripts are run directly from the source code, making Python easier to work with and more portable than code in compiled languages such as C#.
One of the best ways to continue learning Python scripting is to examine the work published by others. Many published examples of Python scripts developed for ArcGIS Pro are available.
Key terms
backporting
backward compatibility
cross-platform
data type
free and open-source software (FOSS)
interpreted language
object oriented
object-oriented programming (OOP)
programming language
script
scripting language
system language
syntax
Review questions
What are some of the key features of Python that make it suitable as a scripting language to work with ArcGIS Pro?
What does it mean that Python is an interpreted language?
What are some of the differences between scripting and programming?
Which version of Python is used with ArcGIS Pro?
What are some of the reasons to invest time and effort into learning Python?
What is the main goal of developing scripts for ArcGIS Pro using Python?
Chapter 2
Working with Python editors
2.1 Introduction
This chapter introduces the use of Python editors, or integrated development environments (IDEs), to write and test your scripts. Many different IDEs are used by the Python community, and experienced developers often have a strong personal preference for a specific editor. Although the choice of which IDE to use is important, Python code is the same regardless of which editor is used.
This chapter explains the ways you can run Python code and introduces several IDEs, including IDLE and PyCharm. Notebooks provide an alternative to traditional IDEs to write Python code and allow you to interact with data and maps directly in ArcGIS Pro. The Python window in ArcGIS Pro presents another way to write Python code directly within ArcGIS Pro without having to work in a separate application.
2.2 Python installation
Before examining how to work with Python code, it is useful to review how Python is installed. Python is installed as part of the ArcGIS Pro installation. This includes all the packages that are necessary to support common GIS-related workflows using Python. A package refers to functionality created by a third party that can be added to your Python installation. To make it easier to manage and install Python packages, the Python community uses package managers. ArcGIS Pro integrates a widely used package manager called conda.
The easiest way to use conda is from within ArcGIS Pro. Start ArcGIS Pro with a new project. Click Project from the top menu and then click Package Manager. This brings up a user-friendly interface to conda.
Package Manager for Python, R, and system libraries.The Package Manager shows you the active environment and which packages are currently installed as part of that environment and allows you to update and add packages. Packages are managed using environments. An environment refers to a particular version of Python that is installed and the packages that come with it. They are referred to as virtual environments because they replicate what a different installation on a different computer would look like but they reside on the same computer. They are also referred to as Python environments. The default environment is called arcgispro-py3. When you first install ArcGIS Pro, this is the only environment available to use. This environment comes with more than 200 of the most widely used Python packages for GIS-related workflows.
Specific tasks may require additional specialized packages, and you can add packages to an existing environment. The Package Manager gives you access to more than 9,000 specialized packages. However, the default environment arcgispro-py3 cannot be modified and is kept in a pristine state. To add packages, therefore, you first need to clone the default environment. You can clone environments using the Package Manager. Cloning effectively creates a copy of the original Python installation that you can then modify by adding packages.
For this book, you do not need to clone environments or update or add packages. The arcgispro-py3 environment contains all the functionality necessary to run the code in all the chapters. Details on cloning environments and managing packages using conda are covered in depth in chapter 6 of the Advanced Python Scripting for ArcGIS Pro book (Esri Press, 2024).
Although it is useful to review how Python is installed using the Package Manager, when you are just learning how to code using Python you typically do not need to make changes to this installation. The default environment consists of the correct version of Python (3.9.18 for ArcGIS Pro 3.2) and includes all the necessary packages for most tasks. When you update the software—for example, to ArcGIS Pro 3.3—the version of Python and the packages are automatically updated.
Finally, it is useful to consider where Python is installed. For a typical installation of ArcGIS Pro, Python is installed in the following folder:
C:\Program Files\ArcGIS\Pro\bin\Python
The default environment is installed in the following subfolder:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
These paths may be slightly different on your computer depending on how ArcGIS Pro was installed. The following sections of this chapter assume the paths referenced above. A common alternative path is
C:\Users\
where
You can use File Explorer to confirm where ArcGIS Pro is installed on your computer and update the paths in the following sections accordingly.
2.3 Using a Python editor or IDE
A computer script is essentially a list of commands that can be run by a program or scripting engine. Scripts are usually plain text documents that have a specific file extension and contain instructions written in a scripting language. Most scripts can be opened and edited using a basic text editor. However, using a specialized script editor to work with a script provides additional functionality when you’re writing the script and allows you to run the script as well.
You can work with Python in many ways. The most basic approach is to use the so-called command line or command prompt. If you have used other programming languages, you may be familiar with this type of interface. To access the Python command line in Windows, search for the application called Python Command Prompt. This application resides in the ArcGIS program group.
Python command line in Windows.On a typical computer, this is not the only Python command prompt. For example, if you installed ArcGIS Desktop 10.x, you also have a command prompt in the ArcGIS program group called Python (command line), which works with Python 2.7. If you installed other programs that use Python, you may have additional command prompts. When working with ArcGIS Pro, you should use the specific command prompt installed with the software, which ensures that you have the correct version of Python that works with ArcGIS Pro and that you have access to the