Introduction to Geoprocessing Scripts
Using Python®
                         Student Edition
Copyright © 2016 Esri
All rights reserved.
Course version 8.0. Version release date April 2016.
Printed in the United States of America.
The information contained in this document is the exclusive property of Esri. This work is protected under United States
copyright law and other international copyright treaties and conventions. No part of this work may be reproduced or
transmitted in any form or by any means, electronic or mechanical, including photocopying and recording, or by any
information storage or retrieval system, except as expressly permitted in writing by Esri. All requests should be sent to
Attention: Contracts and Legal Services Manager, Esri, 380 New York Street, Redlands, CA 92373-8100 USA.
EXPORT NOTICE: Use of these Materials is subject to U.S. export control laws and regulations including the U.S. Department
of Commerce Export Administration Regulations (EAR). Diversion of these Materials contrary to U.S. law is prohibited.
The information contained in this document is subject to change without notice.
   US Government Restricted/Limited Rights
   Any software, documentation, and/or data delivered hereunder is subject to the terms of the License Agreement.
   The commercial license rights in the License Agreement strictly govern Licensee's use, reproduction, or disclosure of
   the software, data, and documentation. In no event shall the US Government acquire greater than RESTRICTED/
   LIMITED RIGHTS. At a minimum, use, duplication, or disclosure by the US Government is subject to restrictions as
   set forth in FAR §52.227-14 Alternates I, II, and III (DEC 2007); FAR §52.227-19(b) (DEC 2007) and/or FAR §12.211/
   12.212 (Commercial Technical Data/Computer Software); and DFARS §252.227-7015 (DEC 2011) (Technical Data -
   Commercial Items) and/or DFARS §227.7202 (Commercial Computer Software and Commercial Computer Software
   Documentation), as applicable. Contractor/Manufacturer is Esri, 380 New York Street, Redlands, CA 92373-8100,
   USA.
@esri.com, 3D Analyst, ACORN, Address Coder, ADF, AML, ArcAtlas, ArcCAD, ArcCatalog, ArcCOGO, ArcData, ArcDoc,
ArcEdit, ArcEditor, ArcEurope, ArcExplorer, ArcExpress, ArcGIS, ArcGlobe, ArcGrid, ArcIMS, ARC/INFO, ArcInfo, ArcInfo
Librarian, ArcLessons, ArcLocation, ArcLogistics, ArcMap, ArcNetwork, ArcNews, ArcObjects, ArcOpen, ArcPad, ArcPlot,
ArcPress, ArcPy, ArcReader, ArcScan, ArcScene, ArcSchool, ArcScripts, ArcSDE, ArcSdl, ArcSketch, ArcStorm, ArcSurvey,
ArcTIN, ArcToolbox, ArcTools, ArcUSA, ArcUser, ArcView, ArcVoyager, ArcWatch, ArcWeb, ArcWorld, ArcXML, Atlas GIS,
AtlasWare, Avenue, BAO, Business Analyst, Business Analyst Online, BusinessMAP, CityEngine, CommunityInfo, Database
Integrator, DBI Kit, EDN, Esri, Esri—Team GIS, Esri—The GIS Company, Esri—The GIS People, Esri—The GIS Software
Leader, FormEdit, GeoCollector, Geographic Design System, Geography Matters, Geography Network, GIS by Esri, GIS Day,
GIS for Everyone, GISData Server, JTX, MapIt, Maplex, MapObjects, MapStudio, ModelBuilder, MOLE, MPS—Atlas, PLTS,
Rent-a-Tech, SDE, SML, Sourcebook·America, SpatiaLABS, Spatial Database Engine, StreetMap, Tapestry, the ARC/INFO
logo, the ArcGIS logo, the ArcGIS Explorer logo, the ArcPad logo, the Esri globe logo, the Esri Press logo, the GIS Day logo,
the MapIt logo, The Geographic Advantage, The Geographic Approach, The World's Leading Desktop GIS, Water Writes,
www.arcgis.com, www.esri.com, www.geographynetwork.com, www.gis.com, www.gisday.com, and Your Personal
Geographic Information System are trademarks, service marks, or registered marks in the United States, the European
Community, or certain other jurisdictions. CityEngine is a registered trademark of Procedural AG and is distributed under
license by Esri.
Other companies and products or services mentioned herein may be trademarks, service marks or registered marks of their
respective mark owners.
    Course introduction
    Introduction
    Course goals
    Additional resources
    Installing the course data
    Icons used in this workbook
    Understanding the ArcGIS Platform
1   Running scripts in Python
    Lesson introduction
    Understanding Python fundamentals
    Integrated Development Environment (IDE)
    Running scripts in Python window
    Exercise 1A: Use the PyScripter IDE
        Open and configure PyScripter
        Run the script
    The ArcPy site package
    ArcPy functions and classes
    The ArcPy modules
    Choosing a scripting environment
    Tips and best practices
    Exercise 1B: Run scripts in Python
        Buffer schools in Python window
        Update script in PyScripter
        Verify results in ArcMap
    Lesson review
2   Describing data
    Lesson introduction
    The Describe function
    Generic Describe object properties
    Feature class Describe properties
    Raster Describe properties
    Describing a feature class and raster
    Fill-in-the-blank
    Exercise 2: Work with the Describe object
          Describe a feature class and a geodatabase
          Describe a list of feature classes
          Clip raster datasets with Describe object properties
          Challenge: Describe dataset and coordinate system properties
    Lesson review
                                                                         i
3   Automating scripts with lists
    Lesson introduction
    The ArcPy List functions
    Explore the ArcPy List functions
    Working with List functions
    Working with da.Walk
    List data
    Exercise 3: Automate scripts with the ArcPy List functions
         List field properties
         Buffer feature classes
         Verify script results
    Lesson review
4   Working with selections
    Lesson introduction
    Selection tools in ArcMap
    Terms commonly used when working with selections
    Tools that accept feature layers
    Working with a selection
    The MakeFeatureLayer tool
    The FieldInfo object
    The AddFieldDelimiters function
    Determining a workflow
    Create a feature layer and get feature count
    Exercise 4: Work with feature layers and selections
        Create new script
        Create feature layers
        Perform spatial selection
        Create feature class from selection
    Lesson review
5   Working with cursors
    Lesson introduction
    The arcpy.da cursors
    Exploring the data access cursors
    Tips and best practices for arcpy.da Cursors
    Using the SearchCursor
    Exercise 5A: Evaluate data with the SearchCursor
        Work with the da.SearchCursor
    Using the UpdateCursor
    Exercise 5B: Alter existing data with the UpdateCursor
        Work with the da.UpdateCursor
    Using the InsertCursor
                                                                 ii
    Exercise 5C: Work with the Insert cursor
        Work with the da.InsertCursor
    Error handling with Python
    Error handling with the try-except statement
    (Optional) Using the arcpy.da.Editor
    Lesson review
6   Working with geometry objects
    Lesson introduction
    Creating geometry objects
    Creating Point geometry objects
    Creating Polyline geometry objects
    Creating Polygon geometry objects
    The geometry object
    Constructing multipart geometry
    Constructing multipart polygons
    Creating and updating feature geometry
    Using a geometry object with geoprocessing tools
    Accessing geometry objects
    Exercise 6: Work with geometry objects and cursors
        Create geometry objects
        Access Shape geometry
        Update existing features
        (Optional) Create new features
        (Optional) Use a geometry object with a geoprocessing tool
    Lesson review
7   Sharing scripts
    Lesson introduction
    Terms commonly used when sharing scripts
    Scripting advantages in ArcMap
    Making scripts dynamic
    Debugging scripts in PyScripter
    Creating and sharing a script tool
    Advantages of attaching a script to a custom tool
    Using the Add Script wizard
    Running a script
    Sharing a script
    Plan out a project
    Exercise 7: Share scripts through geoprocessing packages
        Add pseudocode to the script
        Write script code
        Attach script to custom tool
        Run script in ArcMap
                                                                     iii
        Share results as a geoprocessing package
        Verify the geoprocessing package
    Lesson review
8   Automating map production
    Lesson introduction
    The arcpy.mapping module
    Terms commonly used when working with the arcpy.mapping module
    Using the arcpy.mapping module to modify map document contents
    Referencing the map document
    Listing map document contents
    Managing layers
    Managing layout elements
    Publishing maps
    Exporting maps and reports
    Migrating scripts to ArcGIS Pro
    Customizing symbology for map layers
    Exercise 8: Work with map document contents
         Access map document in ArcGIS for Desktop
         Access map document in script
         Update layer symbology
         Update layout text elements
         Verify changes in ArcGIS for Desktop
         Challenge: Automate script for multiple map documents
    Lesson review
    Appendixes
    Appendix A: Esri data license agreement
    Appendix B: Answers to lesson review questions
       Lesson 1: Running scripts in Python
       Lesson 2: Describing data
       Lesson 3: Automating scripts with lists
       Lesson 4: Working with selections
       Lesson 5: Working with cursors
       Lesson 6: Working with geometry objects
       Lesson 7: Sharing scripts
       Lesson 8: Automating map production
                                                                     iv