ArcPy and ArcGIS – Geospatial Analysis with Python
By Silas Toms
()
About this ebook
- Perform GIS analysis faster by automating tasks, such as selecting data or buffering data, by accessing GIS tools using scripting
- Access the spatial data contained within shapefiles and geodatabases, for updates, analysis and even transformation between spatial reference systems
- Produce map books and automate the mapping of geospatial analyses, reducing the time needed to produce and display the results
If you are a GIS student or professional who needs an understanding of how to use ArcPy to reduce repetitive tasks and perform analysis faster, this book is for you. It is also a valuable book for Python programmers who want to understand how to automate geospatial analyses.
Related to ArcPy and ArcGIS – Geospatial Analysis with Python
Related ebooks
Advanced Python Scripting for ArcGIS Pro Rating: 4 out of 5 stars4/5Programming ArcGIS 10.1 with Python Cookbook Rating: 4 out of 5 stars4/5Learning Geospatial Analysis with Python Rating: 5 out of 5 stars5/5ArcPy and ArcGIS - Second Edition Rating: 5 out of 5 stars5/5Programming ArcGIS with Python Cookbook - Second Edition Rating: 4 out of 5 stars4/5Spatial Analytics with ArcGIS Rating: 0 out of 5 stars0 ratingsLearning R for Geospatial Analysis Rating: 0 out of 5 stars0 ratingsLearning Geospatial Analysis with Python - Second Edition Rating: 0 out of 5 stars0 ratingsPython Geospatial Development Rating: 4 out of 5 stars4/5Geospatial Development By Example with Python Rating: 5 out of 5 stars5/5ArcGIS By Example Rating: 1 out of 5 stars1/5Mastering QGIS Rating: 4 out of 5 stars4/5Python Geospatial Development - Third Edition Rating: 4 out of 5 stars4/5Learning QGIS - Third Edition Rating: 4 out of 5 stars4/5Mapbox Cookbook Rating: 0 out of 5 stars0 ratingsMastering PostGIS Rating: 0 out of 5 stars0 ratingsDeveloping Mobile Web ArcGIS Applications Rating: 0 out of 5 stars0 ratingsGeoServer Cookbook Rating: 0 out of 5 stars0 ratingsPython Geospatial Analysis Essentials Rating: 0 out of 5 stars0 ratingsQGIS By Example Rating: 3 out of 5 stars3/5ArcGIS Blueprints Rating: 4 out of 5 stars4/5Building Mapping Applications with QGIS Rating: 0 out of 5 stars0 ratingsQGIS Blueprints Rating: 0 out of 5 stars0 ratingsMastering OpenLayers 3 Rating: 0 out of 5 stars0 ratingsLeaflet.js Essentials Rating: 4 out of 5 stars4/5Python Data Science Essentials Rating: 0 out of 5 stars0 ratingsReal-Time Big Data Analytics Rating: 5 out of 5 stars5/5Learning Predictive Analytics with Python Rating: 0 out of 5 stars0 ratingsLarge Scale Machine Learning with Python Rating: 2 out of 5 stars2/5Learning Jupyter Rating: 5 out of 5 stars5/5
Internet & Web For You
No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsSocial Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Coding For Dummies Rating: 5 out of 5 stars5/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Get Rich or Lie Trying: Ambition and Deceit in the New Influencer Economy Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 0 out of 5 stars0 ratingsPodcasting For Dummies Rating: 4 out of 5 stars4/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsMore Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The Cyber Attack Survival Manual: Tools for Surviving Everything from Identity Theft to the Digital Apocalypse Rating: 0 out of 5 stars0 ratingsThe Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5The Internet Is Not What You Think It Is: A History, a Philosophy, a Warning Rating: 4 out of 5 stars4/5How To Start A Podcast Rating: 4 out of 5 stars4/5How to Destroy Surveillance Capitalism Rating: 4 out of 5 stars4/5Introduction to Internet Scams and Fraud: Credit Card Theft, Work-At-Home Scams and Lottery Scams Rating: 4 out of 5 stars4/5
Reviews for ArcPy and ArcGIS – Geospatial Analysis with Python
0 ratings0 reviews
Book preview
ArcPy and ArcGIS – Geospatial Analysis with Python - Silas Toms
Table of Contents
ArcPy and ArcGIS – Geospatial Analysis with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Introduction to Python for ArcGIS
Overview of Python
Python as a programming language
Interpreted language
Standard (built-in) library
The glue language
Wrapper modules
The basics of Python
Import statements
Variables
For loops
If/Elif/Else statements
While statements
Comments
Data types
Strings
Integers
Floats
Lists
Tuples
Dictionaries
Iterable data types
Other important concepts
Indentation
Functions
Keywords
Namespaces
Zero-based indexing
Important Python Modules for GIS Analysis
The ArcPy module
The Operating System (OS) module
The Python System (SYS) module
The XLRD and XLWT modules
Commonly used built-in functions
Commonly used standard library modules
Summary
2. Configuring the Python Environment
What is a Python script?
How Python executes a script
What is the Python interpreter?
Where is the Python interpreter located?
Which Python interpreter should be used?
How does the computer know where the interpreter is?
Make Python scripts executable when clicked on
Integrated Development Environments (IDEs)
IDLE
PythonWin
Aptana Studio 3
IDE summary
Python folder structure
Where modules reside
Using Python's sys module to add a module
The sys.path.append() method
Summary
3. Creating the First Python Script
Prerequisites
ModelBuilder
Creating a model and exporting to Python
Modeling the Select and Buffer tools
Adding the Intersect tool
Tallying the analysis results
Exporting the model and adjusting the script
The automatically generated script
File paths in Python
Continuing the script analysis: the ArcPy tools
The Intersect tool and string manipulation
The string manipulation method 1–string addition
The string manipulation method 2–string formatting #1
The string manipulation method 3–string formatting #2
Adjusting the Script
Adding the CSV module to the script
Accessing the data: Using a cursor
The final script
Summary
4. Complex ArcPy Scripts and Generalizing Functions
Python functions–Avoid repeating code
Technical definition of functions
A first function
Functions with parameters
Using functions to replace repetitive code
More generalization of the functions
Summary
5. ArcPy Cursors – Search, Insert, and Update
The data access module
Attribute field interactions
Update cursors
Updating the shape field
Adjusting a point location
Deleting a row using an Update Cursor
Using an Insert Cursor
Inserting a polyline geometry
Inserting a polygon geometry
Summary
6. Working with ArcPy Geometry Objects
ArcPy geometry object classes
ArcPy Point objects
ArcPy Array objects
ArcPy Polyline objects
ArcPy Polygon objects
Polygon object buffers
Other Polygon object methods
ArcPy geometry objects
ArcPy PointGeometry objects
Summary
7. Creating a Script Tool
Adding dynamic parameters to a script
Displaying script messages using arcpy.AddMessage
Adding dynamic components to the script
Creating a Script tool
Labelling and defining parameters
Adding data types
Adding the Bus Stop feature class as a parameter
Adding the Census Block feature class as a parameter
Adding the Census Block field as a parameter
Adding the output spreadsheet as a parameter
Adding the spreadsheet field names as a parameter
Adding the SQL Statement as a parameter
Adding the bus stop fields as a parameter
Inspecting the final script
Running the Script Tool
Summary
8. Introduction to ArcPy.Mapping
Using ArcPy with map documents
Inspecting and replacing layer sources
Fixing the broken links
Fixing the links of individual layers
Exporting to PDF from an MXD
Adjusting map document elements
Automated map document adjustment
The variables
The map document object and the text elements
The layer objects
Replacing the data sources
Adjusting layer visibility
Generating a buffer from the bus stops feature class
Intersecting the bus stop buffer and census blocks
Populating the selected bus stop and buffer feature classes
Updating the text elements
Exporting the adjusted map to PDF
Running the script in the Python Window
Summary
9. More ArcPy.Mapping Techniques
Using arcpy.mapping to control Layer objects
Layer object methods and properties
Definition queries
Controlling the data frame window extent and scale
Adding a Layer object
Exporting the maps
Summary
10. Advanced Geometry Object Methods
Creating a Python module
The __init__.py file
Adding advanced analysis components
Advanced Polygon object methods
Generating random points to represent population
Using the functions within a script
Creating an XLS using XLWT
Summary
11. Network Analyst and Spatial Analyst with ArcPy
The Network Analyst extension
Using Network Analyst
Creating a Feature Dataset
Importing the datasets
Creating the Network Dataset
Accessing the Network Dataset using ArcPy
Breaking down the script
The Network Analyst module
Accessing the Spatial Analyst Extension
Adding elevation to the bus stops
Using Map Algebra to generate elevation in feet
Adding in the bus stops and getting elevation values
The final result
Summary
12. The End of the Beginning
Getting field information from feature classes
Accessing the ListFields' properties
List comprehensions
Creating the field information functions
Querying feature class information
Generating File Geodatabases and feature classes
Generating a feature class
Setting up the script tool parameters
Environmental settings
Resolution and tolerance settings
Summary
Index
ArcPy and ArcGIS – Geospatial Analysis with Python
ArcPy and ArcGIS – Geospatial Analysis with Python
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 2015
Production reference: 1210215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-866-2
www.packtpub.com
Credits
Author
Silas Toms
Reviewers
Alessio Di Lorenzo
Dara O'Beirne
Mark Pazolli
Marjorie Roswell
Commissioning Editor
Ashwin Nair
Acquisition Editor
Harsha Bharwani
Content Development Editor
Akashdeep Kundu
Technical Editor
Deepti Tuscano
Copy Editors
Aarti Saldanha
Adithi Shetty
Project Coordinator
Milton Dsouza
Proofreaders
Simran Bhogal
Joanna McMahon
Bernadette Watkins
Indexer
Priya Sane
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
About the Author
Silas Toms is a geospatial programmer and analyst with a love of geography, history, food, and sports. He resides in the San Francisco Bay Area and can't decide which side of the Bay is more beautiful. He received a bachelor's degree in Geography from Humboldt State University and is currently pursuing a master's degree in GIS at San Francisco State University. With a background in GIS analysis for city governments and environmental consulting, Silas loves the combination of GIS and Python for analysis automation and data manipulation.
Working for Arini Geographics, Silas is helping governments understand how GIS can organize and simplify the management of infrastructure and the environment. This dual role as a programmer and analyst allows him to use Python and GIS to quickly produce geospatial data and tools. Combined with web mapping, these tools are transforming how governments work to serve the public. He also teaches workshops on ArcPy and web mapping at the City College of San Francisco, while hoping to one day finish his master's thesis.
Silas has worked as a reviewer on the book Python Geospatial Analysis, Packt Publishing and is working on the book Python Geospatial Development, Packt Publishing to be published in 2015.
I would like to thank my girlfriend, Christine, for her encouragement and patience. I would like to thank my boss, Gabriel Paun, for his inspiration and for pushing me to become a true GIS professional. I would like to thank the faculty at HSU and SFSU for their help along the way, and I would like to thank my family for their belief in me and for never asking me if I was going to become a teacher with my geography degree (even though I have and I love it!).
About the Reviewers
Alessio Di Lorenzo is a marine biologist and has an MSc in Geographical Information Systems (GIS) and Remote Sensing. Since 2006, he has been dealing with the analysis and development of GIS applications dedicated to the study and spread of environmental and epidemiological data. He is experienced in the use of the main proprietary and open source GIS software and programming languages.
Dara O'Beirne is a certified GIS Professional (GISP) with over eight years of GIS and Python experience. Dara earned both his Bachelors and Masters of Arts degrees in geography from San Francisco State University. Dara is currently a GIS Analyst working at Arini Geographics in Santa Clara, CA. Before joining Arini Geographics, Dara was a GIS Analyst and technical lead at Towill Inc., a GIS and Land Surveying company in Northern California. At Towill, Dara played a central role in developing and implementing procedures related to the collection and analysis of LiDAR data for environmental and engineering applications. Prior to Towill, Dara gained his professional GIS experience working for the Golden Gate National Recreation Area managed by the National Park Service, one of the largest urban park systems in the world, which includes National treasures, such as Alcatraz, Muir Woods, and the Marin Headlands. His Master's Thesis examined the errors associated with measuring tree heights in an urban environment with both traditional field methods and airborne LiDAR data.
I would like to thank my wife, Kate, and daughter, Anya O'Beirne, for their patience and assistance during the review of this book.
Marjorie Roswell is a web developer and map maker from Baltimore, MD. She purchased her first GIS in 1991, and built an application to assist citizen callers to the Baltimore Office of Recycling. Recent projects include interactive maps of legislative scores, political endorsements, committees, election data, and advocacy interests.
Her site http://committeemaps.org/ details Congressional committee membership, while the site http://farmbillprimer.org/ is devoted to mapping and charting federal food and farm policy.
Marjorie is the author of Drupal 5 Views Recipes, Packt Publishing. She was the technical reviewer of jQuery UI 1.10, The User Interface Library for jQuery, Packt Publishing.
Mark Pazolli is an engineer and data scientist who uses ArcGIS and Python to help his employers decipher the mountains of data they keep on the assets of the Western Australian electrical network. He has qualifications in Electrical Engineering, Computer Science, and Applied Mathematics. He appreciates excellent design and enjoys building interesting things.
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
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.
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?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
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.
Preface
ArcGIS, the GIS software from industry leader ESRI, allows for the analysis and presentation of geospatial data.
The integration of Python into ArcGIS has made the ArcPy module an important tool for GIS students and professionals. The ArcPy module provides a powerful way to improve productivity when performing geospatial analysis. From basic Python scripting through advanced ArcPy methods and properties, ArcPy and other Python modules will improve the speed and repeatability of any GIS work flow.
This book will guide you from basic Python scripting to advanced scripting tools. It focuses on geospatial analysis scripting and touches on automating cartographic output. By the end of this book, you will be able to create reusable modules, add repeatable analyses as script tools in ArcToolbox, and export maps automatically. By reducing the time-consuming nature of GIS from days to hours, one GIS professional can become as powerful as a whole team.
What this book covers
Chapter 1, Introduction to Python for ArcGIS, offers a quick introduction to the basics of Python, including other uses for the programming language. It covers Python data types and important modules used throughout the book.
Chapter 2, Configuring the Python Environment, is an introduction to how Python works: its folder structure, executables, and modules. It also explains importing modules into scripts, the built-in modules, and covers Integrated Development Environments (IDEs), which are powerful programming aids.
Chapter 3, Creating the First Python Script, demonstrates how to use ArcGIS ModelBuilder to model the first analysis and then export it as a Python script. String manipulations and how to use file paths in Python are also introduced.
Chapter 4, Complex ArcPy Scripts and Generalizing Functions, examines how to perform analyses and produce outputs that are not possible using ModelBuilder. By using functions, or reusable code blocks, repeating code is avoided.
Chapter 5, ArcPy Cursors – Search, Insert, and Update, covers ArcPy data access cursors and how they are used to search, update, or insert records in feature classes and tables. It explains the quirks of iterating using cursors, and how to only select or update the records of interest.
Chapter 6, Working with ArcPy Geometry Objects, explores ArcPy Geometry objects and how they are combined with cursors to perform spatial analysis. It demonstrates how to buffer, clip, reproject, and more using the data cursors and the Arcpy geometry types without using ArcToolbox.
Chapter 7, Creating a Script Tool, explains how to make scripts into tools that appear in ArcToolbox and are dynamic in nature. It explains how the tools and scripts communicate and how to set up the ArcTool dialog to correctly pass parameters to the script.
Chapter 8, Introduction to ArcPy.Mapping, explores the powerful Arcpy.Mapping module and how to fix broken layer links, turn layers on and off, and dynamically adjust titles and text. It shows how to create dynamic map output based on a geospatial analysis.
Chapter 9, More ArcPy.Mapping Techniques, introduces Layer objects, and their methods and properties. It demonstrates how to control map scales and extents for data frames, and covers automated map export.
Chapter 10, Advanced Geometry Object Methods, expands on the ArcPy Geometry object methods and properties. It also explains how to create a module to save code for reuse in subsequent scripts, and demonstrates how to create Excel spreadsheets containing results from a geospatial analysis.
Chapter 11, Network Analyst and Spatial Analyst with ArcPy, introduces the basics of using ArcPy for advanced geospatial analysis using the ArcGIS for Desktop Network Analyst and Spatial Analyst Extensions.
Chapter 12, The End of the Beginning, covers other important topics that need to be understood to have a full grasp of ArcPy. These topics include the Environment Settings, XY values and Z and M resolutions, Spatial Reference Systems (Projections), the Describe functions, and more.
What you need for this book
You will need the proprietary or free version of ArcGIS 10.1/10.2/10.3. To support your environment, you will need 2GB RAM, 32-bit or 64 bit machine hardware configuration, and Windows 7/8. Python 2.7 is required to do the programming and is installed along with ArcGIS.
Who this book is for
This book is intended for GIS students and professionals who need an understanding of how to use ArcPy to