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 – Geospatial Analysis with Python

Book Description

Use the ArcPy module to automate the analysis and mapping of geospatial data in ArcGIS

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 to automate map production.

This book will guide you from basic Python scripting to advanced ArcPy script tools. This book starts off with setting up your Python environment, demonstrates a complex ArcPy script tool with multiple iterations, illustrates data access module cursors, and explains how to use ArcPy Geometry classes. Then, you will learn how to output maps using ArcPy.Mapping, and how to create ArcGIS script tools.

With the help of this book, you will be able to create repeatable analyses reducing the time-consuming nature of GIS, making you into a GIS professional as powerful as a whole team.

What You Will Learn

  • Understand how to integrate Python into ArcGIS and make GIS analysis faster and easier
  • Model an analysis and export it to Python for further improvement
  • Create Python functions from exported scripts using ArcToolbox tools to avoid repetitive code
  • Update the records of interest in your existing geospatial data automatically using data cursors
  • Add new geospatial data to existing datasets automatically from field-collected data or data produced during analysis
  • Export formatted analysis results to spreadsheets automatically
  • Update map documents with analysis-generated data and export maps to PDF or image formats
  • Create geometric networks and analyze routes using scripts

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. ArcPy and ArcGIS – Geospatial Analysis with Python
    1. Table of Contents
    2. ArcPy and ArcGIS – Geospatial Analysis with Python
    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. 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
    8. 1. Introduction to Python for ArcGIS
      1. Overview of Python
      2. Python as a programming language
        1. Interpreted language
        2. Standard (built-in) library
        3. The glue language
        4. Wrapper modules
        5. The basics of Python
          1. Import statements
          2. Variables
          3. For loops
          4. If/Elif/Else statements
          5. While statements
        6. Data types
          1. Strings
          2. Integers
          3. Floats
          4. Lists
          5. Tuples
          6. Dictionaries
          7. Iterable data types
        7. Other important concepts
          1. Indentation
          2. Functions
          3. Keywords
          4. Namespaces
          5. Zero-based indexing
      3. Important Python Modules for GIS Analysis
        1. The ArcPy module
        2. The Operating System (OS) module
        3. The Python System (SYS) module
        4. The XLRD and XLWT modules
        5. Commonly used built-in functions
        6. Commonly used standard library modules
      4. Summary
    9. 2. Configuring the Python Environment
      1. What is a Python script?
        1. How Python executes a script
      2. What is the Python interpreter?
        1. Where is the Python interpreter located?
        2. Which Python interpreter should be used?
        3. How does the computer know where the interpreter is?
      3. Make Python scripts executable when clicked on
      4. Integrated Development Environments (IDEs)
        1. IDLE
        2. PythonWin
        3. Aptana Studio 3
        4. IDE summary
      5. Python folder structure
        1. Where modules reside
        2. Using Python's sys module to add a module
      6. The sys.path.append() method
      7. Summary
    10. 3. Creating the First Python Script
      1. Prerequisites
        1. ModelBuilder
      2. Creating a model and exporting to Python
        1. Modeling the Select and Buffer tools
        2. Adding the Intersect tool
        3. Tallying the analysis results
      3. Exporting the model and adjusting the script
        1. The automatically generated script
      4. File paths in Python
      5. Continuing the script analysis: the ArcPy tools
        1. The Intersect tool and string manipulation
          1. The string manipulation method 1–string addition
          2. The string manipulation method 2–string formatting #1
          3. The string manipulation method 3–string formatting #2
      6. Adjusting the Script
      7. Adding the CSV module to the script
        1. Accessing the data: Using a cursor
      8. The final script
      9. Summary
    11. 4. Complex ArcPy Scripts and Generalizing Functions
      1. Python functions–Avoid repeating code
        1. Technical definition of functions
        2. A first function
        3. Functions with parameters
        4. Using functions to replace repetitive code
        5. More generalization of the functions
      2. Summary
    12. 5. ArcPy Cursors – Search, Insert, and Update
      1. The data access module
        1. Attribute field interactions
        2. Update cursors
        3. Updating the shape field
        4. Adjusting a point location
        5. Deleting a row using an Update Cursor
        6. Using an Insert Cursor
      2. Inserting a polyline geometry
      3. Inserting a polygon geometry
      4. Summary
    13. 6. Working with ArcPy Geometry Objects
      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
        5. ArcPy geometry objects
        6. ArcPy PointGeometry objects
      2. Summary
    14. 7. Creating a Script Tool
      1. Adding dynamic parameters to a script
        1. Displaying script messages using arcpy.AddMessage
        2. Adding dynamic components to the script
      2. Creating a Script tool
        1. Labelling and defining parameters
          1. Adding data types
          2. Adding the Bus Stop feature class as a parameter
          3. Adding the Census Block feature class as a parameter
          4. Adding the Census Block field as a parameter
          5. Adding the output spreadsheet as a parameter
          6. Adding the spreadsheet field names as a parameter
          7. Adding the SQL Statement as a parameter
          8. Adding the bus stop fields as a parameter
      3. Inspecting the final script
        1. Running the Script Tool
      4. Summary
    15. 8. Introduction to ArcPy.Mapping
      1. Using ArcPy with map documents
      2. Inspecting and replacing layer sources
        1. Fixing the broken links
        2. Fixing the links of individual layers
        3. Exporting to PDF from an MXD
        4. Adjusting map document elements
      3. Automated map document adjustment
        1. The variables
        2. The map document object and the text elements
          1. The layer objects
          2. Replacing the data sources
        3. Adjusting layer visibility
        4. Generating a buffer from the bus stops feature class
        5. Intersecting the bus stop buffer and census blocks
          1. Populating the selected bus stop and buffer feature classes
        6. Updating the text elements
          1. Exporting the adjusted map to PDF
      4. Running the script in the Python Window
      5. Summary
    16. 9. More ArcPy.Mapping Techniques
      1. Using arcpy.mapping to control Layer objects
        1. Layer object methods and properties
      2. Definition queries
      3. Controlling the data frame window extent and scale
        1. Adding a Layer object
        2. Exporting the maps
      4. Summary
    17. 10. Advanced Geometry Object Methods
      1. Creating a Python module
        1. The __init__.py file
      2. Adding advanced analysis components
        1. Advanced Polygon object methods
        2. Generating random points to represent population
        3. Using the functions within a script
        4. Creating an XLS using XLWT
      3. Summary
    18. 11. Network Analyst and Spatial Analyst with ArcPy
      1. The Network Analyst extension
      2. Using Network Analyst
        1. Creating a Feature Dataset
        2. Importing the datasets
        3. Creating the Network Dataset
        4. Accessing the Network Dataset using ArcPy
          1. Breaking down the script
      3. The Network Analyst module
      4. Accessing the Spatial Analyst Extension
        1. Adding elevation to the bus stops
        2. Using Map Algebra to generate elevation in feet
        3. Adding in the bus stops and getting elevation values
      5. The final result
      6. Summary
    19. 12. The End of the Beginning
      1. Getting field information from feature classes
        1. Accessing the ListFields' properties
          1. List comprehensions
        2. Creating the field information functions
        3. Querying feature class information
        4. Generating File Geodatabases and feature classes
        5. Generating a feature class
        6. Setting up the script tool parameters
        7. Environmental settings
          1. Resolution and tolerance settings
      2. Summary
    20. Index