Table Of ContentProgramming ArcGIS
with Python Cookbook
Second Edition
Over 85 hands-on recipes to teach you how to automate
your ArcGIS for Desktop geoprocessing tasks using Python
Eric Pimpler
BIRMINGHAM - MUMBAI
Programming ArcGIS with Python Cookbook
Second Edition
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
First published: February 2013
Second edition: July 2015
Production reference: 1230715
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-289-8
www.packtpub.com
Credits
Author Technical Editor
Eric Pimpler Shivani Kiran Mistry
Reviewers Copy Editor
Mohammed Alhessi Sonia Michelle Cheema
Matthew Bernardo
Project Coordinator
Rahul Bhosle
Sanchita Mandal
Kristofer Lasko
Doug McGeehan
Proofreader
Ann Stark, GISP
Safis Editing
Commissioning Editor
Indexer
Akram Hussain
Mariammal Chettiyar
Acquisition Editors
Graphics
Kevin Colaco
Disha Haria
Usha Iyer
Greg Wild Production Coordinator
Rebecca Youe Nilesh R. Mohite
Content Development Editor Cover Work
Adrian Raposo Nilesh R. Mohite
About the Author
Eric Pimpler is the founder and owner of GeoSpatial Training Services
(http://geospatialtraining.com/) and has over 20 years of experience implementing
and teaching GIS solutions using Esri, Google Earth/Maps, open source technology. Currently,
Eric is focusing on ArcGIS scripting with Python and the development of custom ArcGIS Server
web and mobile applications using JavaScript. Eric is the author of Programming ArcGIS with
Python Cookbook and Building Web and Mobile ArcGIS Server Applications with JavaScript,
both by Packt Publishing.
Eric has a bachelor's degree in geography from Texas A&M University and a master's degree in
applied geography with a focus on GIS from Texas State University.
About the Reviewers
Mohammed Alhessi is a GIS professional and instructor who is interested in geospatial
theory, algorithms, and applications. He has a good amount of experience in GIS analysis,
development, and training. He has conducted quite a few training courses for people from
different backgrounds. The courses have been diverse in terms of subjects and have included,
but are not limited to, Enterprise Geodatabase Administration in MS SQL Server, spatial data
analysis and modeling, and Python scripting for ArcGIS.
He has worked at the University of Stuttgart as a GIS developer, where he programmed
Geoprocessing tools using Java and Python. He is involved in many local GIS projects,
providing consultancy for the local community. He is currently a lecturer at the Islamic
University—Gaza, Palestine. He also holds classes at University College of Applied
Sciences, Gaza.
Mohammed has a master's degree in geomatics engineering from the University of Stuttgart.
He also has a bachelor's degree in civil engineering from the Islamic University—Gaza.
Matthew Bernardo is the senior GIS analyst at Newport Renewables, a renewable
energy firm based in Newport. An avid outdoorsman and technophile, he is drawn to the
assimilation of environment and technology that GIS offers. Over the last few years, he has
used GIS and Python programming to answer complex questions in many fields, including
renewable energy, intelligence analysis, remote sensing, marine science, environmental
science, and town planning.
He has a BS in environmental science from the University of Rhode Island and a graduate
certificate in geospatial intelligence from Penn State.
Rahul Bhosle earned his bachelor of engineering degree in information technology from
Shivaji University, India, and a master of geospatial information science and technology from
North Carolina State University. Currently, he is a Geospatial Developer at GIS Data Resources,
Inc. By profession, he is a geospatial developer. He has experience in the fields of Python,
JavaScript, ArcGIS Suite, GeoServer, PostGIS, PostgreSQL, SQL Server, Leaflet, Openlayers,
Machine Learning, and NoSQL.
Kristofer Lasko earned his bachelor's degree in geographical sciences from the University
of Maryland. He subsequently earned a master's degree in geospatial information science
from the University of Maryland. He teaches a graduate and undergraduate GIS course at the
University of Maryland. He began learning about Python several years ago, when he found it
necessary to automate mundane tasks as well as process large volumes of geospatial data.
He is currently a PhD student at the University of Maryland, where he's studying geographical
sciences. He has previously worked at NASA's Goddard Space Flight Center and NASA's Jet
Propulsion Lab. He has also worked as a GIS and remote sensing analyst at the University of
Maryland. His current research focuses on the burning of crop residue in Vietnam.
His website can be viewed at http://terpconnect.umd.edu/~klasko/cv.html.
Doug McGeehan is a third year PhD student at the Missouri University of Science
and Technology in Rolla, Missouri, USA, where he's studying computer science under
the supervision of Dr Sanjay Madria and Dr Dan Lin. In 2013, he received his bachelor's
degree in computer science from Missouri University of Science and Technology, having
already published two papers in computational geometry and working as a computational
cartographer for the United States Geological Survey (USGS).
Ann Stark, a GISP since 2005, has been active in the GIS profession for 20 years. She is
passionate about GIS and is an active and engaging member of the GIS community in the
Pacific Northwest of the United States, coordinating local user groups and serving as the
president of the region's GIS professional group. She is an enthusiastic teacher who explains
how to effectively use Python with ArcGIS and maintains a blog devoted to the topic at
https://gisstudio.wordpress.com/. She co-owns a GIS consulting business,
Salish Coast Sciences, which provides strategic planning, process automation, and GIS
development services.
To unwind from technology, Ann enjoys spending time with her husband and son at their
urban farm in the heart of the city, where they seek to live sustainably and as self-sufficiently
as an urban farm allows.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can search, access, and read Packt's entire library of books.
Why Subscribe?
f Fully searchable across every book published by Packt
f Copy and paste, print, and bookmark content
f On demand and accessible via a web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface v
Chapter 1: Fundamentals of the Python Language for ArcGIS 1
Using IDLE for Python script development 2
Using the ArcGIS Python window 6
Python language fundamentals 9
Summary 29
Chapter 2: Managing Map Documents and Layers 31
Introduction 31
Referencing the current map document 32
Referencing map documents on a disk 34
Getting a list of layers in a map document 35
Restricting the list of layers 37
Zooming in to selected features 39
Changing the map extent 42
Adding layers to a map document 44
Inserting layers into a map document 46
Updating layer symbology 49
Updating layer properties 52
Working with time-enabled layers in a data frame 57
Chapter 3: Finding and Fixing Broken Data Links 65
Introduction 65
Finding broken data sources in your map document and layer files 66
Fixing broken data sources with MapDocument.
findAndReplaceWorkspacePaths() 69
Fixing broken data sources with MapDocument.replaceWorkspaces() 71
Fixing individual layer and table objects with replaceDataSource() 75
Finding broken data sources in all map documents in a folder 79
i