Preface

For those new to Python scripting, welcome to a whole new world! Programming has become an increasingly important aspect of the skillset of GIS professionals in many fields. Most GIS jobs require at least some experience in programming, and Python is often at the top of the list. If you are looking to kickstart your GIS programming skills, this book is for you.

Python scripting allows you to automate tasks in ArcGIS® Pro that would be cumbersome using the regular menu-driven interface. For example, consider having to convert 1,000 shapefiles into feature classes in a geodatabase. You could run the appropriate tool 1,000 times to complete the task, but surely there must be a more efficient and robust way to do it. That’s where Python scripting comes in, and you need only a handful of lines of code to carry out this task. About halfway through the book, you will write a script that does exactly that.

Before getting further into the contents of this book, a bit of history is in order. In 2013, Esri® Press published Python Scripting for ArcGIS. I wrote the book to serve as an easy-to-understand introduction to Python in order to create scripts for ArcGIS Desktop. At the time, ArcGIS Desktop was version 10.1 and used Python version 2. The book quickly became popular among students and professionals, but several years later the book was no longer up to date.

ArcGIS Pro was released in 2015 and further established Python as the preferred scripting language within the ArcGIS platform. ArcGIS Pro uses Python version 3, which is significantly different from version 2. As the industry started to shift from ArcGIS Desktop 10.x to ArcGIS Pro, interest grew in an updated version of the book. Both the changes in ArcGIS and the differences in Python versions necessitated a completely new book—not just a second edition of the existing book with minor code updates.

The current book is written for ArcGIS Pro 2.5, which uses Python 3.6.9. Most of the code will work in earlier versions of ArcGIS Pro, except for the most recently added functionality. As new functionality is added to future releases of ArcGIS Pro, the code in this book will continue to work. However, much of the code will not work in ArcGIS Desktop 10.x.

This book is designed to make the power of Python scripting available to those who have limited experience writing code. The book starts with the basics, such as what scripting is and how to write and run simple lines of code. Following this, the book covers how to write scripts that work with spatial data in ArcGIS Pro. A good familiarity with ArcGIS Pro is assumed, including managing data, creating cartographic output, and running tools. You should also be familiar with the basic concepts of GIS, including coordinate systems, data formats, table operations, and basic spatial analysis methods. If you have some experience in writing code in any programming language, it will be helpful but is not required.

Why Python? For a couple of reasons. First, Python is free and open source, meaning it can be freely distributed and shared. Second, it is a powerful and versatile programming language although still relatively easy to learn. Third, Python is the preferred language for working with ArcGIS Pro, which is strongly reflected in the functionality of the software.

Although Python is part of the installation of ArcGIS Pro, it is important to remember that Python was not developed by Esri. A large and active Python community maintains and continues to improve Python. As you learn the fundamentals of Python in this book, the additional benefit is that you may find other uses for Python beyond working with ArcGIS Pro. Many introductory computer science courses use Python to explain fundamental concepts in computer programming. This book introduces you to some of these fundamentals, but the emphasis is on writing code specifically designed to work with spatial data in ArcGIS Pro.

There is no ArcGIS-specific version of the Python language, so you can also use any of the general Python books to start learning Python syntax. However, the tasks you work with in ArcGIS Pro, such as managing feature classes, creating geometry objects, and running geoprocessing tools, are different from the more generic tasks used as examples in other Python books. This lack of geospatial content makes it difficult for an experienced ArcGIS Pro user to pick up a general Python book and start writing scripts for ArcGIS Pro. Students and professionals alike will benefit greatly from this book, which covers Python with a specific emphasis on writing scripts for ArcGIS Pro.

The primary audience for this book is experienced ArcGIS Pro users who want to learn Python but who have limited programming or scripting experience. Those already familiar with writing scripts in Python for ArcGIS Desktop 10.x may want to focus on the content that is significantly different relative to the Python Scripting for ArcGIS book, including chapter 2 on working with Python editors and chapter 11 on map scripting. Several chapters have also been expanded compared with the previous book, including chapter 4 on Python fundamentals, chapter 8 on manipulating spatial and tabular data, chapter 9 on working with geometries, and chapter 10 on rasters.

This book also is intended for upper-division undergraduate and graduate courses in GIS. Many colleges and universities teach courses in GIS programming, which has become one of the core skills in GIS degrees and specializations.

This book contains 11 chapters. The first few chapters cover setting up a Python editor to work with ArcGIS Pro and the fundamentals of geoprocessing in ArcGIS Pro, as well as the fundamentals of the Python language. Depending on your background and experience, you already may be familiar with some of this material. The remaining chapters cover writing Python scripts that work with spatial data. This includes chapters on running tools in Python, describing data, manipulating and creating data, working with raster data, and map scripting. By the end of the book, you will be able to write Python scripts to automate tasks in ArcGIS Pro.

Although this book covers the fundamentals of writing Python scripts to work with ArcGIS Pro, there are more skills to learn to take full advantage of the benefits of using Python. Once you complete this book, the next logical step is to continue with the book Advanced Python Scripting for ArcGIS Pro, also published by Esri Press (2020). That book covers more advanced topics, including creating script tools and Python toolboxes; sharing tools with others; 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 Python Scripting for ArcGIS Pro.

The chapters in this book are accompanied by exercises that reinforce the concepts covered in the chapters. These exercises and the data are located in the Learn organization's ArcGIS Online group, Python Scripting for ArcGIS Pro (Esri Press), at https://go.esri.com/PythonData. For general book information, go to https://go.esri.com/PythonPro. You should first read each chapter and then complete the accompanying exercise before moving on to the next chapter. Depending on your learning style and familiarity with coding, you can try out some of the code in the chapters as you read them, but you also can read the entire chapter first, and then start the exercise. You should complete the chapters and exercises in order because the concepts introduced in each new chapter build on the preceding ones. To complete the exercises, you must have ArcGIS Pro 2.5 or later installed on your computer.

This book will teach you the techniques to automate tasks in ArcGIS Pro. Perhaps by the end of the book you will become a Python aficionado, or perhaps you simply will be able to save yourself hours of work by using one of the scripts from the book. Whatever the case, beyond the specific skills of writing Python scripts for ArcGIS Pro, you will learn the basic logic of writing code. This knowledge will be helpful beyond the specific task at hand. My hope is that the book will contribute to demystifying what “writing code” really is for those who may be a little intimidated. And that it will show that writing code is not difficult to learn. Coding as an approach to solving problems is not only a powerful tool but is also applicable to many endeavors. I sincerely hope this book will allow you to experience the versatility and power of Python coding.

Paul A. Zandbergen

Vancouver, BC, Canada