Book Image

ArcPy and ArcGIS - Second Edition

By : Silas Toms, Dara OBeirne
Book Image

ArcPy and ArcGIS - Second Edition

By: Silas Toms, Dara OBeirne

Overview of this book

ArcGIS allows for complex analyses of geographic information. The ArcPy module is used to script these ArcGIS analyses, providing a productive way to perform geo-analyses and automate map production. The second edition of the book focuses on new Python tools, such as the ArcGIS API for Python. Using Python, this book will guide you from basic Python scripting to advanced ArcPy script tools. This book starts off with setting up your Python environment for ArcGIS automation. Then you will learn how to output maps using ArcPy in MXD and update feature class in a geodatabase using arcpy and ArcGIS Online. Next, you will be introduced to ArcREST library followed by examples on querying, updating and manipulating ArcGIS Online feature services. Further, you will be enabling your scripts in the browser and directly interacting with ArcGIS Online using Jupyter notebook. Finally, you can learn ways to use of ArcPy to control ArcGIS Enterprise and explore topics on deployments, data quality assurances, data updates, version control, and editing safeguards. By the end of the book, you will be equipped with the knowledge required to create automated analysis with administration reducing the time-consuming nature of GIS.
Table of Contents (13 chapters)
8
Introduction to ArcGIS Online

Python as a programming language

Over the last 40+ years of computer science, programming languages have developed from assembly and machine code towards high-level abstracted languages, which are much closer to English. The Python programming language, begun by Guido van Rossum in 1989, was designed to overcome issues that programmers were dealing with in the 1980s: slow development time, overly complicated syntax, and horrible readability. He developed Python as a language that would enable rapid code development and testing, have beautiful (or at least readable) syntax, and produce results with fewer lines of code, in less time. The first version of Python (0.9.0) was released in 1991, and it has always been free to download and use.

Go to https://www.python.org/ to explore Python documentation, try tutorials, get help, find useful Python code libraries, and download Python. Python has multiple major and minor versions. For much of the book, we are using Python 2.7, which is installed automatically along with ArcGIS for Desktop. For chapters on ArcGIS Pro, we will use Python 3.5.

Interpreted language

Python is an interpreted language. It is written in C, a compiled language, and the code is "interpreted" from Python into C before it is executed. Practically, this means that the code is executed as soon as it is converted and compiled. While the code interpretation can have speed implications for the execution of Python-based programs, this has very little real-world implications for its use with ArcGIS. Testing of code snippets is much faster in an interpretive environment, and it is perfect for creating scripts to automate basic, repeatable computing tasks.

Standard (built-in) library

Python, when installed, has a basic set of functions that are referred to as the built-in library. These built-in tools allow Python to perform string manipulations, math computations, HTTP calls, and URL parsing along with many other functions. Some of the tool libraries, or modules, are available as soon as Python is started, while others must be explicitly called using the "import" keyword to make their functions and classes available. Other modules have been developed by third parties, and can be downloaded and installed onto the Python installation as needed.

Glue language

Python is often called a "glue" language. This term describes the use of Python code to control other software programs by sending inputs to their Application Programming Interface (API) and collecting outputs, which are then sent to another program to repeat the process. A GIS example would be to use Python's urllib2 to download zipped shapefiles from a website, unzipping the files, processing the files using ArcToolbox, and compiling the results into an Excel spreadsheet. All of this is accomplished using freely available modules that are either included in Python's built-in library, or added on when ArcGIS is installed.

Wrapper modules

The ArcPy module is a "wrapper" module. It "wraps" a Python code interface over the existing ArcGIS tools and source code, allowing us to access these tools within our scripts. Wrapper modules are common in Python, and are so named because they "wrap" Python onto the tools we will need. They allow us to use Python to interface with programs written in C or other programming languages using the API of those programs. The wrapper module os allows for operating system operations.

For example, wrapper modules make it possible to extract data from an Excel spreadsheet, transform the data into another format such as a shapefile, and load it into an MXD as a layer. Not all modules are wrappers; some modules are written in "pure Python", and perform their analysis and computations using Python syntax. Either way, the end result is that a computer and its programs are available to be manipulated and controlled using Python.