logo资料库

Python.Scripting.for.ArcGIS.pdf

第1页 / 共360页
第2页 / 共360页
第3页 / 共360页
第4页 / 共360页
第5页 / 共360页
第6页 / 共360页
第7页 / 共360页
第8页 / 共360页
资料共360页,剩余部分请下载后查看
Cover
Contents
Preface
Acknowledgments
Part 1: Learning the fundamentals of Python and geoprocessing
Chapter 1: Introducing Python 3
1.1 Introduction
1.2 Exploring the features of Python
1.3 Comparing scripting vs. programming
1.4 Using scripting in ArcGIS
1.5 Python history and versions
1.6 About this book
1.7 Exploring how Python is used
1.8 Choosing a Python script editor
Points to remember
Chapter 2: Geoprocessing in ArcGIS
2.1 Introduction
2.2 What is geoprocessing?
2.3 A note on ArcObjects
2.4 Using toolboxes and tools
2.5 Learning types and categories of tools
2.6 Running tools using tool dialog boxes
2.7 Specifying environment settings
2.8 Using batch processing
2.9 Using models and ModelBuilder
2.10 Using scripting
2.11 Running scripts as tools
2.12 Converting a model to a script
2.13 Scheduling a Python script to run at prescribed times
Points to remember
Chapter 3: Using the Python window
3.1 Introduction
3.2 Opening the Python window
3.3 Writing and running code
3.4 Getting assistance
3.5 Exploring Python window options
3.6 Saving your work
3.7 Loading code into the Python window
Points to remember
Chapter 4: Learning Python language fundamentals
4.1 Introduction
4.2 Locating Python documentation and resources
4.3 Working with data types and structures
4.4 Working with numbers
4.5 Working with variables and naming
4.6 Writing statements and expressions
4.7 Using strings
4.8 Using lists
4.9 Working with Python objects
4.10 Using functions
4.11 Using methods
4.12 Working with strings
4.13 Working with lists
4.14 Working with paths
4.15 Working with modules
4.16 Controlling workflow using conditional statements
4.17 Controlling workflow using loop structures
4.18 Getting user input
4.19 Commenting scripts
4.20 Working with code in the PythonWin editor
4.21 Following coding guidelines
Points to remember
Part 2: Writing scripts
Chapter 5: Geoprocessing using Python
5.1 Introduction
5.2 Using the ArcPy site package
5.3 Importing ArcPy
5.4 Working with earlier versions of ArcGIS
5.5 Using tools
5.6 Working with toolboxes
5.7 Using functions
5.8 Using classes
5.9 Using environment settings
5.10 Working with tool messages
5.11 Working with licenses
5.12 Accessing ArcGIS Desktop Help
Points to remember
Chapter 6: Exploring spatial data
6.1 Introduction
6.2 Checking for the existence of data
6.3 Describing data
6.4 Listing data
6.5 Using lists in for loops
6.6 Working with lists
6.7 Working with tuples
6.8 Working with dictionaries
Points to remember
Chapter 7: Manipulating spatial data
7.1 Introduction
7.2 Using cursors to access data
7.3 Using SQL in Python
7.4 Working with table and field names
7.5 Parsing table and field names
7.6 Working with text files
Points to remember
Chapter 8: Working with geometries
8.1 Introduction
8.2 Working with geometry objects
8.3 Reading geometries
8.4 Working with multipart features
8.5 Working with polygons with holes
8.6 Writing geometries
8.7 Using cursors to set the spatial reference
8.8 Using geometry objects to work with geoprocessing tools
Points to remember
Chapter 9: Working with rasters
9.1 Introduction
9.2 Listing rasters
9.3 Describing raster properties
9.4 Working with raster objects
9.5 Working with the ArcPy Spatial Analyst module
9.6 Using map algebra operators
9.7 Using the ApplyEnvironment function
9.8 Using classes of the arcpy.sa module
9.9 Using raster functions to work with NumPy arrays
Points to remember
Part 3: Carrying out specialized tasks
Chapter 10: Map scripting
10.1 Introduction
10.2 Working with the ArcPy mapping module
10.3 Opening map documents
10.4 Accessing map document properties and methods
10.5 Working with data frames
10.6 Working with layers
10.7 Fixing broken data sources
10.8 Working with page layout elements
10.9 Exporting maps
10.10 Printing maps
10.11 Working with PDFs
10.12 Creating map books
10.13 Using sample mapping scripts
Points to remember
Chapter 11: Debugging and error handling
11.1 Introduction
11.2 Recognizing syntax errors
11.3 Recognizing exceptions
11.4 Using debugging
11.5 Using debugging tips and tricks
11.6 Error handling for exceptions
11.7 Raising exceptions
11.8 Handling exceptions
11.9 Handling geoprocessing exceptions
11.10 Using other error-handling methods
11.11 Watching for common errors
Points to remember
Chapter 12: Creating Python functions and classes
12.1 Introduction
12.2 Creating functions
12.3 Calling functions from other scripts
12.4 Organizing code into modules
12.5 Using classes
12.6 Working with packages
Points to remember
Part 4: Creating and using script tools
Chapter 13: Creating custom tools
13.1 Introduction
13.2 Why create your own tools?
13.3 Steps to creating a tool
13.4 Editing tool code
13.5 Exploring tool parameters
13.6 Setting tool parameters
13.7 Examining an example script tool
13.8 Customizing tool behavior
13.9 Working with messages
13.10 Handling messages for stand-alone scripts and tools
13.11 Customizing tool progress information
13.12 Running a script in process
Points to remember
Chapter 14: Sharing tools
14.1 Introduction
14.2 Choosing a method for distributing tools
14.3 Handling licensing issues
14.4 Using a standard folder structure for sharing tools
14.5 Working with paths
14.6 Finding data and workspaces
14.7 Creating a geoprocessing package
14.8 Embedding scripts and password-protecting tools
14.9 Documenting tools
14.10 Example tool: Market analysis
Points to remember
Appendix A Data source credits
Index
Back cover
This page intentionally left blank.
This page intentionally left blank.
>>> PYTHON 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. Includes index. ISBN 978-1-58948-371-2 (pbk.) pages cm 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: customerservice@ingrampublisherservices.com In North America: Ingram Publisher Services Toll-free telephone: 800-648-3104 Toll-free fax: 800-838-1149 In the United Kingdom, Europe, Middle East and Africa, Asia, and Australia: Eurospan Group 3 Henrietta Street London WC2E 8LU United Kingdom Telephone: 44(0) 1767 604972 Fax: 44(0) 1767 601640 E-mail: eurospan@turpin-distribution.com
Contents Preface ________________________________________________xi Acknowledgments ______________________________________xiv Part 1: Learning the fundamentals of Python and geoprocessing __ 1 Chapter 1: Introducing Python ___________________________________ 3 Introduction ___________________________________________ 3 1.1 Exploring the features of Python _________________________ 3 1.2 Comparing scripting vs. programming ____________________ 4 1.3 1.4 Using scripting in ArcGIS _______________________________ 5 Python history and versions _____________________________ 5 1.5 About this book _______________________________________ 6 1.6 Exploring how Python is used ___________________________ 8 1.7 Choosing a Python script editor ________________________ 13 1.8 Points to remember ___________________________________ 19 Chapter 2: Geoprocessing in ArcGIS _____________________________ 21 Introduction __________________________________________ 21 2.1 2.2 What is geoprocessing? ________________________________ 21 A note on ArcObjects__________________________________ 23 2.3 2.4 Using toolboxes and tools ______________________________ 24 Learning types and categories of tools ___________________ 25 2.5 Running tools using tool dialog boxes ___________________ 26 2.6 Specifying environment settings ________________________ 30 2.7 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 Introduction __________________________________________ 49 3.1 3.2 Opening the Python window ___________________________ 49 3.3 Writing and running code ______________________________ 50 3.4 Getting assistance _____________________________________ 52 Exploring Python window options ______________________ 54 3.5 Saving your work _____________________________________ 55 3.6 Loading code into the Python window __________________ 56 3.7 Points to remember ___________________________________ 57 Chapter 4: Learning Python language fundamentals _______________ 59 Introduction __________________________________________ 59 4.1 Locating Python documentation and resources ___________ 59 4.2 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 Introduction __________________________________________ 95 5.1 5.2 Using the ArcPy site package ___________________________ 95 Importing ArcPy ______________________________________ 96 5.3 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 Introduction _________________________________________ 123 6.1 Checking for the existence of data _____________________ 123 6.2 6.3 Describing data ______________________________________ 125 Listing data _________________________________________ 127 6.4 6.5 Using lists in for loops _______________________________ 131 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 Introduction _________________________________________ 139 7.1 7.2 Using cursors to access data ___________________________ 139 7.3 Using SQL in Python _________________________________ 144 7.4 Working with table and field names ____________________ 146 Parsing table and field names __________________________ 148 7.5 7.6 Working with text files _______________________________ 149 Points to remember __________________________________ 158 Chapter 8: Working with geometries ____________________________ 159 Introduction _________________________________________ 159 8.1 8.2 Working with geometry objects ________________________ 159 Reading geometries __________________________________ 160 8.3 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
分享到:
收藏