ebook img

Python Scripting for ArcGIS PDF

360 Pages·2014·11.13 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python Scripting for ArcGIS

This page intentionally left blank. This page intentionally left blank. >>> PY THON ® Scripting for ArcGIS ® Paul A. Zandbergen Esri Press, 380 New York Street, Redlands, California 92373-8100 Copyright © 2013 Esri All rights reserved. First edition Printed in the United States of America 17 16 15 14 13 1 2 3 4 5 6 7 8 9 10 Library of Congress has cataloged the print edition as follows: Zandbergen, Paul A., 1968- Python scripting for ArcGIS / Paul A. Zandbergen. — First edition. pages cm Includes index. ISBN 978-1-58948-371-2 (pbk.) eISBN 978-1-58948-362-0 (electronic) 1. ArcGIS. 2. Geographic information systems. 3. Graphical user interfaces (Computer systems) 4. Python (Computer program language) I. Title. G70.212.Z36 2013 910.285’5133--dc23 2012020676 The information contained in this document is the exclusive property of Esri unless otherwise noted. This work is protected under United States copyright law and the copyright laws of the given countries of origin and applicable international laws, treaties, and/or conventions. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying or 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, California 92373-8100 USA. The information contained in this document is subject to change without notice. U.S. 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, arcgis.com, 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<,ital>, 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.com, 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, geographynetwork.com, Geoloqi, Geotrigger, GIS by Esri, gis.com, GISData Server, GIS Day, gisday.com, GIS for Everyone, 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 Explorer logo, the ArcGIS logo, the ArcPad logo, the Esri globe logo, the Esri Press logo, The Geographic Advantage, The Geographic Approach, the GIS Day logo, the MapIt logo, The World’s Leading Desktop GIS, Water Writes, and Your Personal Geographic Information System are trademarks, service marks, or registered marks of Esri 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. Ask for Esri Press titles at your local bookstore or order by calling 800-447-9778, or shop online at esri.com/esripress. Outside the United States, contact your local Esri distributor or shop online at eurospanbookstore.com/esri. Esri Press titles are distributed to the trade by the following: E-mail: [email protected] In North America: In the United Kingdom, Europe, Middle East and Africa, Asia, and Australia: Ingram Publisher Services Eurospan Group Telephone: 44(0) 1767 604972 Toll-free telephone: 800-648-3104 3 Henrietta Street Fax: 44(0) 1767 601640 Toll-free fax: 800-838-1149 London WC2E 8LU E-mail: [email protected] United Kingdom Contents Preface ________________________________________________xi Acknowledgments ______________________________________xiv Part 1: Learning the fundamentals of Python and geoprocessing __1 Chapter 1: Introducing Python ___________________________________ 3 1.1 Introduction ___________________________________________3 1.2 Exploring the features of Python _________________________3 1.3 Comparing scripting vs. programming ____________________4 1.4 Using scripting in ArcGIS _______________________________5 1.5 Python history and versions _____________________________5 1.6 About this book _______________________________________6 1.7 Exploring how Python is used ___________________________8 1.8 Choosing a Python script editor ________________________13 Points to remember ___________________________________19 Chapter 2: Geoprocessing in ArcGIS _____________________________ 21 2.1 Introduction __________________________________________21 2.2 What is geoprocessing? ________________________________21 2.3 A note on ArcObjects__________________________________23 2.4 Using toolboxes and tools ______________________________24 2.5 Learning types and categories of tools ___________________25 2.6 Running tools using tool dialog boxes ___________________26 2.7 Specifying environment settings ________________________30 2.8 Using batch processing ________________________________32 2.9 Using models and ModelBuilder ________________________34 2.10 Using scripting _______________________________________38 2.11 Running scripts as tools _______________________________41 2.12 Converting a model to a script __________________________44 2.13 Scheduling a Python script to run at prescribed times _____45 Points to remember ___________________________________47 vi Contents Chapter 3: Using the Python window_____________________________49 3.1 Introduction __________________________________________49 3.2 Opening the Python window ___________________________49 3.3 Writing and running code ______________________________50 3.4 Getting assistance _____________________________________52 3.5 Exploring Python window options ______________________54 3.6 Saving your work _____________________________________55 3.7 Loading code into the Python window __________________56 Points to remember ___________________________________57 Chapter 4: Learning Python language fundamentals _______________59 4.1 Introduction __________________________________________59 4.2 Locating Python documentation and resources ___________59 4.3 Working with data types and structures _________________60 4.4 Working with numbers ________________________________60 4.5 Working with variables and naming _____________________62 4.6 Writing statements and expressions _____________________63 4.7 Using strings _________________________________________64 4.8 Using lists____________________________________________66 4.9 Working with Python objects ___________________________66 4.10 Using functions _______________________________________68 4.11 Using methods _______________________________________69 4.12 Working with strings __________________________________70 4.13 Working with lists ____________________________________74 4.14 Working with paths ___________________________________78 4.15 Working with modules ________________________________79 4.16 Controlling workflow using conditional statements _______81 4.17 Controlling workflow using loop structures ______________83 4.18 Getting user input ____________________________________85 4.19 Commenting scripts ___________________________________86 4.20 Working with code in the PythonWin editor _____________88 4.21 Following coding guidelines ____________________________89 Points to remember ___________________________________90 Contents vii Part 2: Writing scripts ___________________________________________93 Chapter 5: Geoprocessing using Python _________________________95 5.1 Introduction __________________________________________95 5.2 Using the ArcPy site package ___________________________95 5.3 Importing ArcPy ______________________________________96 5.4 Working with earlier versions of ArcGIS _________________97 5.5 Using tools ___________________________________________98 5.6 Working with toolboxes _______________________________104 5.7 Using functions ______________________________________106 5.8 Using classes ________________________________________107 5.9 Using environment settings ___________________________110 5.10 Working with tool messages ___________________________112 5.11 Working with licenses ________________________________115 5.12 Accessing ArcGIS Desktop Help _______________________119 Points to remember __________________________________122 Chapter 6: Exploring spatial data _______________________________123 6.1 Introduction _________________________________________123 6.2 Checking for the existence of data _____________________123 6.3 Describing data ______________________________________125 6.4 Listing data _________________________________________127 6.5 Using lists in loops _______________________________131 for 6.6 Working with lists ___________________________________132 6.7 Working with tuples__________________________________133 6.8 Working with dictionaries ____________________________134 Points to remember __________________________________137 Chapter 7: Manipulating spatial data ____________________________139 7.1 Introduction _________________________________________139 7.2 Using cursors to access data ___________________________139 7.3 Using SQL in Python _________________________________144 7.4 Working with table and field names ____________________146 7.5 Parsing table and field names __________________________148 7.6 Working with text files _______________________________149 Points to remember __________________________________158 Chapter 8: Working with geometries ____________________________159 8.1 Introduction _________________________________________159 8.2 Working with geometry objects ________________________159 8.3 Reading geometries __________________________________160 8.4 Working with multipart features _______________________164 8.5 Working with polygons with holes _____________________167 8.6 Writing geometries ___________________________________169 8.7 Using cursors to set the spatial reference _______________172 8.8 Using geometry objects to work with geoprocessing tools__174 Points to remember __________________________________175 viii Contents Chapter 9: Working with rasters ________________________________177 9.1 Introduction _________________________________________177 9.2 Listing rasters _______________________________________177 9.3 Describing raster properties ___________________________178 9.4 Working with raster objects ___________________________182 9.5 Working with the ArcPy Spatial Analyst module _________183 9.6 Using map algebra operators __________________________184 9.7 Using the function _______________187 ApplyEnvironment 9.8 Using classes of the module _________________188 arcpy.sa 9.9 Using raster functions to work with NumPy arrays ______192 Points to remember __________________________________193 Part 3: Carrying out specialized tasks __________________________ 195 Chapter 10: Map scripting _____________________________________197 10.1 Introduction _________________________________________197 10.2 Working with the ArcPy mapping module ______________197 10.3 Opening map documents _____________________________198 10.4 Accessing map document properties and methods _______200 10.5 Working with data frames ____________________________201 10.6 Working with layers __________________________________203 10.7 Fixing broken data sources ____________________________208 10.8 Working with page layout elements ____________________213 10.9 Exporting maps ______________________________________216 10.10 Printing maps _______________________________________218 10.11 Working with PDFs __________________________________219 10.12 Creating map books __________________________________220 10.13 Using sample mapping scripts _________________________222 Points to remember __________________________________225 Chapter 11: Debugging and error handling ______________________227 11.1 Introduction _________________________________________227 11.2 Recognizing syntax errors _____________________________227 11.3 Recognizing exceptions _______________________________230 11.4 Using debugging _____________________________________230 11.5 Using debugging tips and tricks _______________________237 11.6 Error handling for exceptions _________________________238 11.7 Raising exceptions ___________________________________239 11.8 Handling exceptions __________________________________241 11.9 Handling geoprocessing exceptions ____________________244 11.10 Using other error-handling methods ___________________247 11.11 Watching for common errors __________________________247 Points to remember __________________________________249 Contents ix Chapter 12: Creating Python functions and classes ______________ 251 12.1 Introduction _________________________________________251 12.2 Creating functions ___________________________________251 12.3 Calling functions from other scripts ____________________255 12.4 Organizing code into modules _________________________258 12.5 Using classes ________________________________________260 12.6 Working with packages _______________________________264 Points to remember __________________________________267 Part 4: Creating and using script tools _________________________ 269 Chapter 13: Creating custom tools ______________________________ 271 13.1 Introduction _________________________________________271 13.2 Why create your own tools? ___________________________271 13.3 Steps to creating a tool _______________________________273 13.4 Editing tool code _____________________________________279 13.5 Exploring tool parameters _____________________________280 13.6 Setting tool parameters _______________________________285 13.7 Examining an example script tool ______________________290 13.8 Customizing tool behavior ____________________________293 13.9 Working with messages _______________________________294 13.10 Handling messages for stand-alone scripts and tools _____297 13.11 Customizing tool progress information _________________297 13.12 Running a script in process ___________________________301 Points to remember __________________________________302 Chapter 14: Sharing tools ______________________________________303 14.1 Introduction _________________________________________303 14.2 Choosing a method for distributing tools _______________303 14.3 Handling licensing issues _____________________________304 14.4 Using a standard folder structure for sharing tools _______305 14.5 Working with paths __________________________________306 14.6 Finding data and workspaces __________________________308 14.7 Creating a geoprocessing package ______________________310 14.8 Embedding scripts and password-protecting tools ________311 14.9 Documenting tools ___________________________________312 14.10 Example tool: Market analysis _________________________315 Points to remember __________________________________321 Appendix A Data source credits _________________________323 Index _______________________________________________327

Description:
Python scripting for ArcGIS / Paul A. Zandbergen. Part 1: Learning the fundamentals of Python and geoprocessing __ 1. Chapter 1.1. Introduction
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.