You are previewing Programming ArcGIS with Python Cookbook, 2nd Edition.
O'Reilly logo
Programming ArcGIS with Python Cookbook, 2nd Edition

Book Description

Over 85 hands-on recipes to teach you how to automate your ArcGIS for Desktop geoprocessing tasks using Python

In Detail

The book kicks off with the fundamentals of starting to use Python with ArcGIS, followed by recipes on managing map documents and layers, including how to find and fix broken data links in these files.

In the second part of the book, you will learn to create custom geoprocessing tools and how to use the Attribute and Location tools to select specific features.

The third part of the book covers topics for advanced users including the REST API, and also teaches you how to use Python with ArcGIS Pro. The book finishes with appendices covering how to automate Python scripts, and the five things that should be at the back of every GIS programmer's mind.

What You Will Learn

  • Manage your map document files, layer files, feature classes, and tables

  • Programmatically update layer properties and symbology

  • Find and fix broken data links in your map document files

  • Automate map production, printing, and the creation of PDF map books

  • Develop custom geoprocessing tools that can be shared with others

  • Query and select data from feature classes and tables

  • Create new feature classes or tables and add, update, and delete records

  • Customize the ArcGIS for Desktop interface with Python add-ins

  • 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 files e-mailed directly to you.

    Table of Contents

    1. Programming ArcGIS with Python Cookbook Second Edition
      1. Table of Contents
      2. Programming ArcGIS with Python Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Fundamentals of the Python Language for ArcGIS
        1. Using IDLE for Python script development
          1. The Python shell window
          2. The Python script window
          3. Editing existing Python scripts
          4. Executing scripts from IDLE
        2. Using the ArcGIS Python window
          1. The ArcGIS Python window
          2. Displaying the ArcGIS Python window
        3. Python language fundamentals
          1. Commenting code
          2. Importing modules
          3. Variables
          4. Built-in data types
            1. Strings
              1. String manipulation
            2. Numbers
            3. Lists
            4. Tuples
            5. Dictionaries
          5. Classes and objects
          6. Statements
            1. Decision support statements
            2. Looping statements
            3. Try statements
            4. With statements
            5. Statement indentation
          7. File I/O
        4. Summary
      9. 2. Managing Map Documents and Layers
        1. Introduction
        2. Referencing the current map document
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Referencing map documents on a disk
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Getting a list of layers in a map document
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Restricting the list of layers
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Zooming in to selected features
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Changing the map extent
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Adding layers to a map document
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Inserting layers into a map document
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Updating layer symbology
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        11. Updating layer properties
          1. Getting ready
          2. How to do it…
        12. Working with time-enabled layers in a data frame
          1. Getting ready
          2. How to do it...
          3. How it works…
      10. 3. Finding and Fixing Broken Data Links
        1. Introduction
        2. Finding broken data sources in your map document and layer files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Fixing broken data sources with MapDocument.findAndReplaceWorkspacePaths()
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Fixing broken data sources with MapDocument.replaceWorkspaces()
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Fixing individual layer and table objects with replaceDataSource()
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Finding broken data sources in all map documents in a folder
          1. Getting ready
          2. How to do it...
          3. How it works...
      11. 4. Automating Map Production and Printing
        1. Introduction
        2. Creating a list of layout elements
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Assigning a unique name to layout elements
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Restricting the layout elements returned by ListLayoutElements()
          1. Getting ready
          2. How to do it…
          3. How it works...
        5. Updating the properties of layout elements
          1. Getting ready
          2. How to do it…
          3. How it works...
        6. Getting a list of available printers
          1. Getting ready
          2. How to do it…
          3. How it works...
        7. Printing maps with PrintMap()
          1. Getting ready
          2. How to do it…
          3. How it works...
        8. Exporting a map to a PDF file
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Exporting a map to an image file
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. Exporting a report
          1. Getting ready
          2. How to do it…
          3. How it works…
        11. Building a map book with Data Driven Pages and ArcPy mapping
          1. Getting ready
          2. How to do it…
          3. How it works…
        12. Publishing a map document to an ArcGIS Server service
          1. Getting ready
          2. How to do it…
          3. How it works…
      12. 5. Executing Geoprocessing Tools from Scripts
        1. Introduction
        2. Finding geoprocessing tools
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Retrieving a toolbox alias
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Executing geoprocessing tools from a script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        5. Using the output of a tool as an input to another tool
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 6. Creating Custom Geoprocessing Tools
        1. Introduction
        2. Creating a custom geoprocessing tool
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        3. Creating a Python toolbox
          1. Getting ready
          2. How to do it…
          3. How it works…
      14. 7. Querying and Selecting Data
        1. Introduction
        2. Constructing a proper attribute query syntax
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Creating feature layers and table views
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more...
        4. Selecting features and rows with the Select Layer by Attribute tool
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Selecting features with the Select by Location tool
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Combining a spatial and attribute query with the Select by Location tool
          1. Getting ready
          2. How to do it...
          3. How it works...
      15. 8. Using the ArcPy Data Access Module with Feature Classes and Tables
        1. Introduction
        2. Retrieving features from a feature class with SearchCursor
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Filtering records with a where clause
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Improving cursor performance with geometry tokens
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Inserting rows with InsertCursor
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Updating rows with UpdateCursor
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Deleting rows with UpdateCursor
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Inserting and updating rows inside an edit session
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Reading geometry from a feature class
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. Using Walk() to navigate directories
          1. Getting ready
          2. How to do it…
          3. How it works…
      16. 9. Listing and Describing GIS Data
        1. Introduction
        2. Working with the ArcPy list functions
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Getting a list of fields in a feature class or table
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Using the Describe() function to return descriptive information about a feature class
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Using the Describe() function to return descriptive information about a raster image
          1. Getting ready
          2. How to do it…
          3. How it works…
      17. 10. Customizing the ArcGIS Interface with Add-ins
        1. Introduction
        2. Downloading and installing the Python Add-in Wizard
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Creating a button add-in and using the Python add-ins module
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Installing and testing an add-in
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Creating a tool add-in
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      18. 11. Error Handling and Troubleshooting
        1. Introduction
        2. Exploring the default Python error message
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Adding Python exception handling structures (try/except/else)
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Retrieving tool messages with GetMessages()
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Filtering tool messages by the level of severity
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Testing for and responding to specific error messages
          1. Getting ready
          2. How to do it…
          3. How it works…
      19. 12. Using Python for Advanced ArcGIS
        1. Introduction
        2. Getting started with the ArcGIS REST API
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Making HTTP requests and parsing the response with Python
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Getting layer information with the ArcGIS REST API and Python
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Exporting a map with the ArcGIS REST API and Python
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Querying a map service with the ArcGIS REST API and Python
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Geocoding with the Esri World Geocoding Service
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Using FieldMap and FieldMappings
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Using a ValueTable to provide multivalue input to a tool
          1. Getting ready
          2. How to do it…
          3. How it works…
      20. 13. Using Python with ArcGIS Pro
        1. Introduction
        2. Using the new Python window in ArcGIS Pro
        3. Coding differences between ArcGIS for Desktop and ArcGIS Pro
        4. Installing Python for ArcGIS Pro
        5. Converting ArcGIS for Desktop Python code to ArcGIS Pro
      21. A. Automating Python Scripts
        1. Introduction
        2. Running Python scripts from the command line
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Using sys.argv[ ] to capture command-line input
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Adding Python scripts to batch files
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Scheduling batch files to run at prescribed times
          1. Getting ready
          2. How to do it...
          3. How it works...
      22. B. Five Python Recipes Every GIS Programmer Should Know
        1. Introduction
        2. Reading data from a delimited text file
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more...
        3. Sending e-mails
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Retrieving files from an FTP server
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Creating ZIP files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Reading XML files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      23. Index