Preface
The impetus for writing this book came from the lessons I’ve learned from using and teaching geographic information systems (GIS) for over 10 years at several different universities. One of these lessons is that “a little bit of code can go a long way.” Those of us who learned our first computer skills back in the days of MS-DOS became familiar with using a command prompt to carry out basic tasks.
Early versions of ArcGIS for Desktop Advanced (ArcInfo) software also relied heavily on a command line interface, in addition to the use of the ARC Macro Language (AML).
More recently, however, most software has come to rely on a graphic user interface (GUI) with very limited need to use a command line interface or to write any code. As a result, the majority of college students taking their first GIS course today have never seen any form of code.
Although the menu-driven user interface of ArcGIS for Desktop allows for very complicated operations and sophisticated spatial analysis, at some point users will run into tasks that require something more.
That’s where Python scripting comes in. In a nutshell, Python scripting allows you to automate tasks in ArcGIS that would be quite 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, but surely there must be a more efficient and robust way to do this. That’s what Python scripting will do, and you need only a handful of lines of code to carry out this task. About halfway through this book, you will write a script that does exactly that.
This book is designed to make the power of Python scripting available to those who have no 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 for Desktop applications. A good familiarity with ArcGIS for Desktop is assumed, including managing data in the ArcCatalog application and carrying out basic tasks in the ArcMap application, such as manipulating 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, Esri has adopted Python as the preferred language for working with ArcGIS, which is strongly reflected in the functionality introduced in version 10.
Although Python Scripting for ArcGIS was written for ArcGIS 10.1 for Desktop, most if not all of the Python scripting in the book will also work in ArcGIS 10.2.
So although the look of the interface might differ slightly, the functionality should remain largely the same. Although Python is included in a typical installation of ArcGIS for Desktop, it is important to remember that Python was not developed by Esri.
There is a large and active Python community that 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.
Many introductory computer science courses now use Python as a way to explain fundamental concepts in computer programming. This book will introduce you to some of these fundamentals, but the emphasis is clearly on writing code that is specifically designed to work with spatial data in ArcGIS.
There are numerous introductory textbooks on GIS and tutorials for learning ArcGIS. Most of them include sections on spatial analysis methods and procedures. However, coverage of Python scripts is not at all complete.
Although there is no lack of good introductory books on Python, most of them cover Python without targeting a specific application. Python’s role as a “glue” language is explained and demonstrated, but most books remain very general when it comes to how Python works with other programs.
There is no ArcGIS-specific version of the Python language, so you can use any of the general Python books to start learning Python syntax. However, the objects you work with in ArcGIS, such as feature classes, polygons, and geoprocessing tools, are very different from the more generic objects used as examples in most existing titles.
This makes it difficult for an experienced ArcGIS user to just pick up a general Python book and start writing scripts for ArcGIS. For an experienced programmer who has previously programmed with ArcGIS in either VBA or C++, a generic Python reference might suffice, but other professionals should see benefit from a book that covers Python with a very specific focus on writing scripts for ArcGIS.
The primary audience for this book consists of experienced ArcGIS users who want to learn Python but have limited programming or scripting experience. Prior experience with other scripting or programming languages, such as Perl, VBA, VBScript, Java, or C++, is helpful but not required.
More experienced programmers will also benefit, but the emphasis is on making Python scripting available to the large number of ArcGIS for Desktop users who want to get more out of the functions of ArcGIS without having to become full-time programmers and learn C++.
Readers will be expected, however, to have good overall ArcGIS skills and a basic understanding of geoprocessing procedures. This book is also intended for upper-division undergraduate and graduate courses in GIS.
A handful of colleges and universities teach upper-division courses in GIS programming and in scripting, and this number is expected to increase.
This book contains four major parts.
Part 1 covers the fundamentals of geoprocessing in ArcGIS for Desktop as well as the fundamentals of the Python language. Depending on your background and experience, you may already be familiar with some or all of this material.
Part 2 covers how to write scripts that work with spatial data. This is really the core of the book and includes chapters on running tools in Python, describing data, and manipulating and creating data.
Part 3 explores a number of more specialized tasks, such as map scripting, debugging and error handling, and creating Python functions and classes.
Part 4 explains how to create a tool out of your script and how to share it with others. By the end of the book, you will be able to create custom tools that use Python scripting to automate basic tasks in ArcGIS for Desktop.
Each of the 14 chapters in this book is accompanied by an exercise chapter that reinforces the concepts covered in the chapter.
All 14 exercise chapters are available online on the Esri Press book resources website at http://esripress.esri.com/bookresources.
Find the listing for Python Scripting for ArcGIS, click the book title, and then click the link to the book’s exercise chapters. You should first read the 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 can also 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. Most of the exercises include challenges at the end, which allow you to practice your skills. Solutions for these challenges are also included online on the companion book resources page.
To do the exercises in this book, you need to have ArcGIS 10.1 for Desktop or a later version of the software installed on your computer, or else download a trial version of the software by clicking the software link on the companion book resources page. This book will teach you the techniques needed to automate tasks in ArcGIS.
Perhaps by the end of the book you will become a Python aficionado, or perhaps you will simply 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, you will also learn the basic logic of writing code.
This will be helpful beyond the specific task at hand. My hope is that the book will contribute to demystifying what “writing code” is really all about for those who may be a little intimidated by it. And that it will show that writing code is not difficult to learn.
Coding used as an approach to solving problems is not only a very powerful tool, but is also applicable to many endeavors—I sincerely hope this book will allow you to experience the versatility of Python coding.
More recently, however, most software has come to rely on a graphic user interface (GUI) with very limited need to use a command line interface or to write any code. As a result, the majority of college students taking their first GIS course today have never seen any form of code.
Although the menu-driven user interface of ArcGIS for Desktop allows for very complicated operations and sophisticated spatial analysis, at some point users will run into tasks that require something more.
That’s where Python scripting comes in. In a nutshell, Python scripting allows you to automate tasks in ArcGIS that would be quite 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, but surely there must be a more efficient and robust way to do this. That’s what Python scripting will do, and you need only a handful of lines of code to carry out this task. About halfway through this book, you will write a script that does exactly that.
This book is designed to make the power of Python scripting available to those who have no 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 for Desktop applications. A good familiarity with ArcGIS for Desktop is assumed, including managing data in the ArcCatalog application and carrying out basic tasks in the ArcMap application, such as manipulating 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, Esri has adopted Python as the preferred language for working with ArcGIS, which is strongly reflected in the functionality introduced in version 10.
Although Python Scripting for ArcGIS was written for ArcGIS 10.1 for Desktop, most if not all of the Python scripting in the book will also work in ArcGIS 10.2.
So although the look of the interface might differ slightly, the functionality should remain largely the same. Although Python is included in a typical installation of ArcGIS for Desktop, it is important to remember that Python was not developed by Esri.
There is a large and active Python community that 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.
Many introductory computer science courses now use Python as a way to explain fundamental concepts in computer programming. This book will introduce you to some of these fundamentals, but the emphasis is clearly on writing code that is specifically designed to work with spatial data in ArcGIS.
There are numerous introductory textbooks on GIS and tutorials for learning ArcGIS. Most of them include sections on spatial analysis methods and procedures. However, coverage of Python scripts is not at all complete.
Although there is no lack of good introductory books on Python, most of them cover Python without targeting a specific application. Python’s role as a “glue” language is explained and demonstrated, but most books remain very general when it comes to how Python works with other programs.
There is no ArcGIS-specific version of the Python language, so you can use any of the general Python books to start learning Python syntax. However, the objects you work with in ArcGIS, such as feature classes, polygons, and geoprocessing tools, are very different from the more generic objects used as examples in most existing titles.
This makes it difficult for an experienced ArcGIS user to just pick up a general Python book and start writing scripts for ArcGIS. For an experienced programmer who has previously programmed with ArcGIS in either VBA or C++, a generic Python reference might suffice, but other professionals should see benefit from a book that covers Python with a very specific focus on writing scripts for ArcGIS.
The primary audience for this book consists of experienced ArcGIS users who want to learn Python but have limited programming or scripting experience. Prior experience with other scripting or programming languages, such as Perl, VBA, VBScript, Java, or C++, is helpful but not required.
More experienced programmers will also benefit, but the emphasis is on making Python scripting available to the large number of ArcGIS for Desktop users who want to get more out of the functions of ArcGIS without having to become full-time programmers and learn C++.
Readers will be expected, however, to have good overall ArcGIS skills and a basic understanding of geoprocessing procedures. This book is also intended for upper-division undergraduate and graduate courses in GIS.
A handful of colleges and universities teach upper-division courses in GIS programming and in scripting, and this number is expected to increase.
This book contains four major parts.
Part 1 covers the fundamentals of geoprocessing in ArcGIS for Desktop as well as the fundamentals of the Python language. Depending on your background and experience, you may already be familiar with some or all of this material.
Part 2 covers how to write scripts that work with spatial data. This is really the core of the book and includes chapters on running tools in Python, describing data, and manipulating and creating data.
Part 3 explores a number of more specialized tasks, such as map scripting, debugging and error handling, and creating Python functions and classes.
Part 4 explains how to create a tool out of your script and how to share it with others. By the end of the book, you will be able to create custom tools that use Python scripting to automate basic tasks in ArcGIS for Desktop.
Each of the 14 chapters in this book is accompanied by an exercise chapter that reinforces the concepts covered in the chapter.
All 14 exercise chapters are available online on the Esri Press book resources website at http://esripress.esri.com/bookresources.
Find the listing for Python Scripting for ArcGIS, click the book title, and then click the link to the book’s exercise chapters. You should first read the 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 can also 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. Most of the exercises include challenges at the end, which allow you to practice your skills. Solutions for these challenges are also included online on the companion book resources page.
To do the exercises in this book, you need to have ArcGIS 10.1 for Desktop or a later version of the software installed on your computer, or else download a trial version of the software by clicking the software link on the companion book resources page. This book will teach you the techniques needed to automate tasks in ArcGIS.
Perhaps by the end of the book you will become a Python aficionado, or perhaps you will simply 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, you will also learn the basic logic of writing code.
This will be helpful beyond the specific task at hand. My hope is that the book will contribute to demystifying what “writing code” is really all about for those who may be a little intimidated by it. And that it will show that writing code is not difficult to learn.
Coding used as an approach to solving problems is not only a very powerful tool, but is also applicable to many endeavors—I sincerely hope this book will allow you to experience the versatility of Python coding.
Download Link
2 comments:
thank you a lot
Excellent work, thanks for sharing information
Post a Comment