O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ArcPy and ArcGIS - Second Edition

Book Description

Use Python modules such as ArcPy, ArcREST and the ArcGIS API for Python to automate the analysis and mapping of geospatial data.

About This Book

  • Perform GIS analysis faster by automating tasks.
  • Access the spatial data contained within shapefiles and geodatabases and transform between spatial reference systems.
  • Automate the mapping of geospatial analyses and production of map books.

Who This Book Is For

If you are a GIS student or professional who needs an understanding of how to use ArcPy to reduce repetitive tasks and perform analysis faster, this book is for you. It is also a valuable book for Python programmers who want to understand how to automate geospatial analyses and implement ArcGIS Online data management.

What You Will Learn

  • Understand how to integrate Python into ArcGIS and make GIS analysis faster and easier.
  • Create Python script using ArcGIS ModelBuilder.
  • Learn to use ArcGIS online feature services and the basics of the ArcGIS REST API
  • Understand the unique Python environment that is new with ArcGIS Pro
  • Learn about the new ArcGIS Python API and how to use Anaconda and Jupyter with it
  • Learn to control ArcGIS Enterprise using ArcPy

In Detail

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.

Style and approach

The book takes a pragmatic approach, showing ways to automate repetitive tasks and utilizing features of ArcPy with ArcGIS Pro and ArcGIS online.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Introduction to Python for ArcGIS
    1. Python as a programming language
      1. Interpreted language
      2. Standard (built-in) library
      3. Glue language
      4. Wrapper modules
    2. The basics of Python programming
      1. Import statements
      2. Variables
      3. For loops
      4. If/Elif/Else statements
      5. While statements
      6. Comments
    3. Data types
      1. Strings
      2. Integers
      3. Floats
      4. Data containers
      5. Zero-based indexing
      6. Lists
      7. Tuples
      8. Dictionaries
    4. Other important concepts
      1. Indentation
      2. Functions
      3. Keywords
      4. Namespaces
    5. Important Python modules
      1. The OS (operating system) module
      2. The sys (Python system) module
      3. The CSV, XLRD, and XLWT modules
      4. Commonly used built-in functions
      5. Standard library modules
    6. How Python executes a script
      1. What is a Python script?
      2. Python interpreter
      3. Where is the Python interpreter located?
      4. Which Python interpreter should be used?
      5. How does the machine know where the interpreter is?
      6. Make Python scripts executable when clicked
    7. Integrated Development Environments (IDEs)
      1. IDLE
      2. PythonWin
      3. Atom, Notepad++, and Sublime Text
      4. IDE summary
    8. Python folder structure
      1. Where modules reside
      2. Installing a third-party module
      3. Using Python's sys module to add a module
      4. The sys.path.append method
    9. Summary
  3. Creating the First Python Script
    1. Prerequisites
    2. ModelBuilder
      1. Creating a model and exporting to Python
        1. Modeling the Select and Buffer tools
        2. Adding in the Intersect tool
        3. Tallying the analysis results
    3. Exporting the model and adjusting the script
      1. The Automatically generated script
      2. File paths in Python
    4. String manipulation
      1. String manipulation method 1: string addition
      2. String manipulation method 2: string formatting #1
      3. String manipulation method 3: string formatting #2
    5. The ArcPy tools
      1. The Intersect tool
        1. Adjusting the script
        2. Adding the CSV module to the script
      2. Accessing the data: using a cursor
        1. Exceptions and  tracebacks
        2. Overwriting files
    6. The final script
    7. Summary
  4. ArcPy Cursors - Search, Insert, and Update
    1. Python functions – avoid repeating code
      1. Technical definition of functions
      2. The first function
      3. Functions with parameters
      4. Using functions to replace repetitive code
        1. The createCSV function
        2. Creating an XLS using XLWT
    2. The data access module
      1. Search cursors
      2. Attribute field interactions
      3. Update cursors
      4. Updating the shape field
      5. Adjusting a point location
      6. Deleting a row using an Update cursor
      7. Using an Insert cursor
      8. Inserting a polyline geometry
      9. Inserting a polygon geometry
    3. Summary
  5. ArcPy Geometry Objects and Cursors
    1. ArcPy geometry object classes
      1. ArcPy Point objects
      2. ArcPy Array objects
      3. ArcPy Polyline objects
      4. ArcPy Polygon objects
        1. Polygon object buffers
        2. Other Polygon object methods
        3. The AsShape method
        4. Generic geometry object
      5. ArcPy PointGeometry objects 
      6. Rewriting the bus stop analysis
        1. Adding to the analysis
    2. Summary
  6. Creating a Script Tool
    1. Adding dynamic parameters to a script
      1. Accessing the passed parameters
      2. Displaying script messages using arcpy.AddMessage
      3. Adding dynamic components to the script
    2. Creating a script tool
      1. Labeling and defining parameters
      2. Adding data types
      3. Adding the Bus Stop feature class 
      4. Adding the Census Block feature class
      5. Adding the Census Block field 
      6. Adding the output spreadsheet
        1. Adding the spreadsheet field names
        2. Adding the SQL Statement
        3. Adding the bus stop fields
    3. Inspecting the final script
      1. Running the script tool
    4. Summary
  7. The arcpy.mapping Module
    1. Using ArcPy with map documents  
      1. Interacting with map document elements
        1. Data frames
        2. Pan and zoom methods
        3. Using the arcpy.mapping  module to control layer objects
          1. Layer object methods and properties
          2. Data source
          3. Name or description
          4. Visibility
          5. Definition queries
      2. Inspecting and replacing layer sources
        1. The ListBrokenDataSources method
          1. Fixing the broken links  
          2. Fixing the links of individual layers
        2. Exporting to PDF from an MXD
      3. Automated map document production
        1. The variables
          1. Connection to the map document
          2. Data frames
          3. Access the layers
          4. The layout elements
          5. Generating a buffer from the bus stops feature class
          6. Intersecting the bus stop buffer and census blocks
          7. Format a dynamic definition query
          8. Updating the layout elements
          9. Exporting the adjusted map to PDF
        2. The Results - Dynamic maps
    2. Summary
  8. Advanced Analysis Topics
    1. Using Network Analyst
      1. Creating a network dataset
        1. Importing the datasets
        2. Creating the network dataset
      2. Accessing the network dataset using ArcPy
        1. Breaking down the script
    2. The Network Analyst module
    3. Accessing the Spatial Analyst extension
      1. Adding elevation to the bus stops
        1. Using Map algebra to generate elevation in feet
        2. Adding in the bus stops and getting elevation values
      2. The final result
    4. Summary
  9. Introduction to ArcGIS Online
    1. ArcGIS Online
      1. Signing up for an account
      2. Exploring the interface
        1. The My Organization tab
        2. The My Content tab 
          1. The Add Item option
          2. Features from services
          3. Features from files
          4. The Create tab
        3. The Groups tab
        4. The Map and Scene tabs
      3. Publishing from an MXD
        1. Styling the layers
        2. Publishing the layers
        3. The Share As menu
        4. Service Editor
          1. The Item Description option
          2. Analyze
        5. Updates
      4. Developer account
    2. Summary
  10. ArcPy and ArcGIS Online
    1. ArcGIS Online REST services
      1. Exploring ArcGIS REST services
    2. URL parameters
    3. Feature sets
      1. Feature set methods
    4. ArcGIS Online tokens
    5. Putting it all together
    6. Summary
  11. ArcREST Python Package
    1. Introducing the ArcREST module
      1. Installing ArcREST
      2. Introduction to the ArcREST package structure
      3. ArcREST security handler
    2. ArcGIS Online administration
    3. Querying hosted feature services
      1. Querying all features and saving as a feature class
      2. Adding a field to a feature service
      3. Adding domains to fields in a hosted feature service
      4. Appending a feature class to a feature service
      5. Updating records in a feature service
    4. Summary
  12. ArcPy and ArcGIS Pro
    1. Introducing ArcGIS Pro
    2. Installing and configuring ArcGIS Pro
    3. The ArcGIS Pro Python window
    4. Python 2.7 and Python 3.5 with ArcPro
    5. Conda and ArcGIS Pro
      1. Running standalone scripts with Conda
    6. Reviewing Conda basics
    7. Summary
  13. ArcGIS API for Python
    1. Introduction to the ArcGIS API for Python
      1. Installing and configuring Anaconda with Jupyter
        1. Install the ArcGIS Python API
    2. Creating a Jupyter Notebook
    3. Starting the ArcGIS API for Python
    4. Adding an item to a web map
    5. Importing a CSV with pandas
    6. Summary