GIS Tutorial for Python ScriptingeISBN: XXXXXXXXXXGIS Tutorial for Python Scripting uses practical examples, exercises, and assignments to help students develop proficiency using Python® in...

https://bayanbox.ir/view/5487064266723849363/GIS-Tutorial-for-Python-Scripting-2014.pdf


GIS Tutorial for Python Scripting eISBN: 9781589483972 GIS Tutorial for Python Scripting uses practical examples, exercises, and assignments to help students develop proficiency using Python® in ArcGIS®. This workbook for Python scripting builds upon previously acquired GIS skills and takes them to the next level with the use of Python. Knowledge of Python—a key tool for scripting geoprocessing functions and tasks in ArcGIS for Desktop—is expanded through hands-on programming geared to automating GIS applications. Exercise data is provided, and instructor resources are available. David W. Allen is the GIS manager for the City of Euless, Texas. He has taught at Tarrant County College since 1999, where he helped found one of the first GIS degree programs in Texas and establish a state standard for GIS degree programs. He is the author of GIS Tutorial 2: Spatial Analysis Workbook (Esri Press, 2013) and Getting to Know ArcGIS ModelBuilder (Esri Press, 2011) and the co-author of GIS Tutorial 3: Advanced Workbook (Esri Press, 2011). G IS T u t o r ia l f o r P y t h o n S c r ip t in g David W. Allen for ArcGIS® 10.2ArcGIS® 10.2 Allen This page intentionally left blank. This page intentionally left blank. Esri Press, 380 New York Street, Redlands, California 92373-8100 Copyright © 2014 Esri All rights reserved. Printed in the United States of America 19 18 17 16 14 1 2 3 4 5 6 7 8 9 10 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, 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: In North America: Ingram Publisher Services Toll-free telephone: 800-648-3104 Toll-free fax: 800-838-1149 E-mail: [email protected] 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: [email protected] CONTENTS Preface VII Introduction IX Chapter 1 Using Python in labeling and field calculations 1 Tutorial 1-1 Python introduction and formatting labels 2 Tutorial 1-2 Decision making in the Label Expression dialog box 10 Tutorial 1-3 Using Python in the Field Calculator 17 Tutorial 1-4 Decision making in the Field Calculator 24 Tutorial 1-5 Working with Python date formats 31 Chapter 2 Writing stand-alone Python scripts 39 Special introduction: Working with Python 40 Tutorial 2-1 Creating describe objects 46 Tutorial 2-2 Scripting geoprocessing tasks 53 Tutorial 2-3 Coding for multiple geoprocessing tasks 62 Tutorial 2-4 Using while statements 69 Tutorial 2-5 Using lists and for statements 78 Tutorial 2-6 Building script tools 86 Tutorial 2-7 Using cursors 101 Tutorial 2-8 Combining loops 113 Tutorial 2-9 Creating custom toolbars 120 Chapter 3 The ArcPy mapping module 129 Tutorial 3-1 Accessing map document elements 129 Tutorial 3-2 Controlling the map extent 144 Tutorial 3-3 Creating a map series 158 Chapter 4 Python toolboxes 171 Tutorial 4-1 Creating a Python toolbox 171 Tutorial 4-2 Setting up value validation 184 Tutorial 4-3 Setting up dependencies 193 VI Content s Chapter 5 Python add-ins 203 Special introduction: Python add-ins 204 Tutorial 5-1 Creating a Python add-in application 222 Tutorial 5-2 Using buttons and combo boxes 229 Tutorial 5-3 Using tools to interact with the map 242 Appendix A: Using an IDE for Python scripting 257 Appendix B: Tool index 263 Appendix C: Data source credits 269 PREFACE Do you want to be an expert Python programmer? Well, so do I! That desire put me on a path of investigation and learning to find out everything I could about using Python with ArcGIS, from A to Z, and eventually led me to writing this book. As a bit of a confession, I’m not the world’s greatest Python programmer, but I overcome that by being the best GIS analyst I can be and knowing how to research the tools that I’m already familiar with. My strategy is to write detailed pseudo code, research the tools I will need in ArcGIS for Desktop Help, test those tools, and then try out what I have learned. I often write code in small steps, debugging each part of the overall program before moving on to the next. As an example, I may write and debug a file collection process, add and debug a cursor to go through the files, and then add and debug the code to modify the files. I find this process much easier than trying to write massive amounts of code and then having to debug several processes and techniques that may be used all at once. This strategy makes me an excellent Python programmer in the realm of ArcGIS, which is what I want you to be! Your work in the tutorials will reflect that process. After all, the end user will not see your code but will be amazed at the job it does. You can get your Python basics from the other books, and in this book, I will show you the tools and functions you’ll want to use most in ArcGIS. Then you can work through a variety of hands-on tutorials and exercises that will have you writing pseudo code, creating scripts from scratch, and solving problems like a pro. The other part of being a good programmer is learning to think like a computer: your steps should be clear and concise and follow a logical path. That clarity is sometimes contrary to how we function in the real world and to how we use language every day. As an example, let’s say you are on a train in Germany, traveling to Wolfsburg, and a passenger makes an upward gesture and says to you in English, “Please, sir, to help me on the shelf on top of my head to put my bag.” You would be able to interpret this sentence and understand that he wants help putting his bag in the overhead compartment. A computer, however, would not understand this sentence at all nor would it be influenced by any voice inflection or gestures. Its interpretation might be that you want to put the man on the shelf and then put the bag on top of his head. The sequence and understanding of the process is not right. The construction of detailed pseudo code will help you think through the process, ensuring that the instructions are clearly stated and that they follow a logical sequence. The more detailed the project, the more important the pseudo code. In fact, it is nearly impossible to write a Python add-in without pseudo code. VIII Pre f ac e I regret that I cannot include every possible tool and technique that exists for Python programming— there are just too many. However, this book covers many of the more commonly used processes that are afforded to programmers in the ArcGIS world. When you master these processes, you will be able to look out into the Python world and begin to learn and understand the more advanced programming techniques that are available to enhance your code. INTRODUCTION A few Python books are now available that are specific to using Python in ArcGIS—with one problem. Most of them are not hands-on workbooks. These books describe the tools individually but do not go as far as showing you how to weave the tools together into scripts to accomplish a goal. It is the equivalent of showing you a picture of a hammer, describing how nails work (note which end is pointed), and then expecting you to build a house. The practical knowledge you need to become a programmer is included in this workbook. With each of the tutorials, you should read the introduction, scenario, and description of the data. Following these sections is a section called “Scripting techniques,” which outlines some techniques that you might want to use in your script. This section may also describe aspects of tools that you may not necessarily use but are useful to know. You should try writing your own pseudo code without looking ahead at the suggested solution, and then check and modify your completed pseudo code against it. If you have a substantially different process from what the book presents, work the tutorial as written so that you will learn the techniques included, and then feel free to go back and work the tutorial a second time using your own pseudo code. It will be great practice. Data for the book is available to download on the Esri Press “Book Resources” webpage, esripress.esri.com/bookresources. Click the appropriate book title, and then click the data link under “Resources” to download the exercise data. A 60-day trial of ArcGIS for Desktop software and extensions is available for readers at esri.com/trydesktop. You may also find that some of the code is not as concise as hard-core programmers might write. This level of code is used, in part, to keep different tools and techniques separated and to make it easier for you to understand. Students sometimes have a problem looking at sample code that has several advanced techniques going on and trying to pick out and learn each technique separately. The simplicity also helps to make the code easier to debug. As you are learning, you may not always know where to start with the debugging process, and complex code will only make debugging more confusing. As you become more advanced, work on making your code more concise. This book’s tutorials are divided into five chapters.
Dec 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here