Introduction To GIS Programming and Fundamentals With Python and ArcGIS
Introduction To GIS Programming and Fundamentals With Python and ArcGIS
to GIS Programming
and Fundamentals with Python and
ArcGIS
Introduction to GIS Programming
and Fundamentals with Python and
ArcGIS
Chaowei Yang
With the collaboration of
Manzhu Yu
Qunying Huang
Zhenlong Li
Min Sun
Kai Liu
Yongyao Jiang
Jizhe Xia
Fei Hu
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
2017 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
International Standard Book Number-13: 978-1-4665-1008-1 (Hardback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have
been made to publish reliable data and information, but the author and publisher cannot assume responsibility
for the validity of all materials or the consequences of their use. The authors and publishers have attempted to
trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if
permission to publish in this form has not been obtained. If any copyright material has not been acknowledged
please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted,
or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system, without
written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com
(http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive,
Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration
for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate
system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
only for identification and explanation without intent to infringe.
Preface
Acknowledgments
Editor
Contributors
Section I Overview
1. Introduction
1.1 Computer Hardware and Software
1.2 GIS and Programming
1.3 Python
1.4 Class and Object
1.5 GIS Data Models
1.6 UML
1.7 Hands-On Experience with Python
1.8 Chapter Summary
Problems
2. Object-Oriented Programming
2.1 Programming Language and Python
2.2 Class and Object
2.2.1 Defining Classes
2.2.2 Object Generation
2.2.3 Attributes
2.2.4 Inheritance
2.2.5 Composition
2.3 Point, Polyline, and Polygon
2.4 Hands-On Experience with Python
2.5 Chapter Summary
Problems
Section II Python Programming
3. Introduction to Python
3.1 Object-Oriented Support
3.2 Syntax
3.2.1 Case Sensitivity
3.2.2 Special Characters
3.2.3 Indentation
3.2.4 Keywords
3.2.5 Multiple Assignments
3.2.6 Namespace
3.2.7 Scope
3.3 Data Types
3.3.1 Basic Data Types
3.3.2 Composite Data Types
3.4 Miscellaneous
3.4.1 Variables
3.4.2 Code Style
3.5 Operators
3.6 Statements
3.7 Functions
3.8 Hands-On Experience with Python
3.9 Chapter Summary
Problems
6. Shapefile Handling
6.1 Binary Data Manipulation
6.2 Shapefile Introduction
6.3 Shapefile Structure and Interpretation
6.3.1 Main File Structure of a Shapefile
6.3.1.1 Main File Header
6.3.1.2 Feature Record
6.3.2 Index File Structure (.shx)
6.3.3 The .dbf File
6.4 General Programming Sequence for Handling Shapefiles
6.5 Hands-On Experience with Mini-GIS
6.5.1 Visualize Polylines and Polygons
6.5.2 Interpret Polyline Shapefiles
6.6 Chapter Summary
Problems
7. Python Programming Environment
7.1 General Python IDE
7.1.1 Python Programming Windows
7.1.1.1 Command-Line GUI
7.1.1.2 Interactive GUI
7.1.1.3 File-Based Programming
7.1.2 Python IDE Settings
7.1.2.1 Highlighting
7.1.2.2 General Setting of the Programming Window
7.1.2.3 Fonts Setup for the Coding
7.1.3 Debugging
7.1.3.1 SyntaxError
7.1.3.2 Run-Time Exceptions
7.1.3.3 Handling Exceptions
7.1.3.4 Add Exception Handles and Clean-Up Actions to
File Read/Write
7.2 Python Modules
7.2.1 Module Introduction
7.2.2 Set Up Modules
7.2.3 System Built-In Modules
7.3 Package Management and Mini-GIS
7.3.1 Regular GIS Data Organization
7.3.2 Mini-GIS Package
7.4 Hands-On Experience with Mini-GIS
7.4.1 Package Management and Mini-GIS
7.4.2 Run and Practice the Mini-GIS Package
7.5 Chapter Summary
Problems
9. ArcGIS Programming
9.1 ArcGIS Programming
9.2 Introduction to ArcPy Package
9.2.1 ArcPy Functions, Classes, and Modules
9.2.2 Programming with ArcPy in ArcMap
9.2.3 Programming with ArcPy in Python Window outside
ArcMap
9.2.4 Using Help Documents
9.3 Automating ArcTools with Python
9.4 Accessing and Editing Data with Cursors
9.4.1 SearchCursor
9.4.2 UpdateCursor
9.4.3 InsertCursor
9.4.4 NumPy
9.5 Describing and Listing Objects
9.5.1 Describe
9.5.2 List
9.6 Manipulating Complex Objects
9.7 Automating Map Production
9.8 Creating ArcTools from Scripts
9.9 Handling Errors and Messages
9.10 External Document and Video Resources
9.11 Implementing Spatial Relationship Calculations Using ArcGIS
9.12 Summary
9.13 Assignment
References
Index
Preface
Hands-On Experience
As a practical text for developing programming skills, this book makes every
effort to ensure the content is as functional as possible. For every introduced GIS
fundamental principle, algorithm and element, an example is explored as a hands-
on experience using Mini-GIS and/or ArcGIS with Python. This learning
workflow helps build a thorough understanding of the fundamentals and naturally
maps to the fundamentals and programming skills.
For system and open-source development, a step-by-step development of a
python-based Mini-GIS is presented. For application development, ArcGIS is
adopted for illustration.
The Mini-GIS is an open-source software developed for this text and can be
adopted for building other GIS applications. ArcGIS, a commercial product from
ESRI, is used to experience state-of-the-art commercial software. For learning
purpose, ArcGIS is available for free from ESRI.
Online Materials
This book comes with the following online materials:
Instructional slides for instructors using this text for classroom education
and professionals to assist in learning GIS programming.
Python codes for class exercises and hands-on experiences and structured
and labeled by chapter to code the chapters sequence.
Mini-GIS as an open-source package for learning the GIS fundamentals and
for exemplifying GIS principles and algorithms.
Answers to problems for instructors to check their solutions.
Using his course materials, Professor Yang structured this book with Irma
Shaglas help, and the texts structure was contracted to be published as a
book. Assistant Professor Qunying Huang, University of Wisconsin,
Madison, explored using the earlier versions of the texts materials.
Assistant Professors Huang and Zhenlong Li, University of South Carolina,
developed Section II of the text in collaboration with Professor Yang.
Dr. Min Sun, Ms. Manzhu Yu, Mr. Yongyao Jiang, and Mr. Jizhe Xia
developed Section III in collaboration with Professor Yang.
Professor Yang edited and revised all chapters to assure a common structure
and composition.
Ms. Manzhu Yu and Professor Yang edited the course slides.
Assistant Professor Li, Mr. Kai Liu, Mrs. Joseph George, and Ms. Zifu
Wang edited Mini-GIS as the software for the text.
After the above text and course materials were completed, four professors
and two developers were invited to review the texts content.
The assembled materials for the text were finally reviewed by several
professionals, including Ms. Alena Deveau, Mr. Rob Culbertson, and
Professor George Taylor.
The text was formatted by Ms. Minni Song.
Ms. Manzhu Yu and Professor Yang completed a final review of the
chapters, slides, codes, data, and all relevant materials.
Acknowledgments
This text is a long-term project evolving from the course Introduction to GIS
Programming developed and refined over the past decade at George Mason
University. Many students and professors provided constructive suggestions about
what to include, how best to communicate and challenge the students, and who
should be considered as audience of the text.
The outcome reflects Professor Yangs programming career since his
undergraduate theses at Chinas Northeastern University under the mentoring of
Professor Jinxing Wang. Professor Yang was further mentored in programming in
the GIS domain by Professors Qi Li and Jicheng Chen. His academic mentors in
the United States, Professors David Wong and Menas Kafatos, provided support
over many decades, giving him the chance to teach the course that eventually led
to this text.
Professor Yang thanks the brilliant and enthusiastic students in his classes at
George Mason University. Their questions and critiques honed his teaching skills,
improved the content, and prompted this effort of developing a text.
Professor Yang thanks his beloved wife, Yan Xiang, and childrenAndrew,
Christopher, and Hannahfor accommodating him when stealing valuable family
time to complete the text.
Ms. Manzhu Yu extends her gratitude to the many colleagues who provided
support, and read, wrote, commented, and assisted in the editing, proofreading,
and formatting of the text.
Assistant Professor Huang thanks her wonderful husband, Yunfeng Jiang, and
lovely daughter, Alica Jiang.
Dr. Min Sun thanks her PhD supervisor, Professor David Wong, for educating
her. She also thanks David Wynne, her supervisor in ESRI where she worked as
an intern, and her other coworkers who collectively helped her gain a more
complete understanding of programming with ESRI products. Last but not least,
she thanks her parents and lovely dog who accompanied her when she was writing
the text.
Yongyao Jiang thank his wife Rui Dong, his daughter Laura, and his parents Lixia
Yao and Yanqing Jiang.
Editor
Overview
1
Introduction
This chapter introduces the basic concepts of computer, hardware, software, and
programming, and sets up the context for GIS programming.
FIGURE 1.1
(a) NASA supercomputer. (From NASA supercomputer at
http://www.nas.nasa.gov/hecc/resources/pleiades.html.) (b) Other computers: personal computer (PC), laptop,
pad. (From different computers at http://www.computerdoc.com.au/what-are-the-different-types-of-
computers.)
There are two important components of a computer (Hwang and Faye 1984): (1)
the physical device that can conduct automated processing, and (2) instruction
packages that can be configured to provide specific functionality, such as word
processing or geographic information processing. The first component of a
computer, the hardware, is touchable as physical machines. The second
component, the software, may be purchased with the hardware in the form of an
operating system, or installed by downloading online. Computer hardware can be
configured or programmed to perform different tasks; thus, a computer may also
be called a general-purpose device. The software varies greatly, whether it is
providing document-processing capability, financial management, tax return
processing, or scientific simulations such as climate change or the spread of
disease. Depending on the type of software, it is either procured publicly
(freeware) or proprietary (requiring purchase and licensing). Depending on the
usage, software can be categorized as system software, application software, or
embedded software (Figure 1.2). System software refers to the basic software that
must be installed for a computer to operate. Windows and Linux are examples of
operating system (OS) software, an essential component of a computer.
Application software supports specific groups of tasks, such as Microsoft Word
for document processing and Microsoft Outlook for emails. Embedded software is
a type of firmware that is burned onto hardware and becomes part of that
hardware. Embedded software exists longer on a computer than any other
software. The firmware will always come with the hardware when you purchase a
computer, so the firmware will not have to be changed as frequently, especially
when updating a web browser or Turbo Tax Return routinely.
FIGURE 1.2
Different types of software.
1.3 Python
Python was originally developed by a Dutch programmer, Guido van Rossum, in
1990. Van Rossum was reportedly a fan of the British comedy series, Monty
Pythons Flying Circus, and upon developing the open-source programming
language, he borrowed to the name Python for the language and his nonprofit
institution, the Python Software Foundation.
Similar to programming languages C++ and Java, Python is an object-oriented
and interactive language. Python is dynamic in that it uses an automatic memory
management mechanism to allocate and release memory for data (variables).
Python and ArcGIS regularly release new versions of their programs; this book is
based on Python release 2.7 and ArcGIS 10.1.
There are many reasons for choosing Python, including the following:*
Get familiar with the concept of class and object (Chapters 1 and 2).
Learn the syntax of Python, including variables, data types, structures,
controls, statements, and other programming structures (Chapters 1 through
4).
Build Python programs from scratch and integrate open-source libraries to
facilitate programming (Chapter 5).
Become comfortable with the Python programming environment (Python
interpreter or Python Text editor, Chapter 6).
Solve GIS problems by writing code for GIS algorithms (Chapters 7
through 13).
These components are introduced in the above order throughout this book. This
chapter introduces important concepts such as object-oriented programming,
UML, and GIS models.
FIGURE 1.3
An example of representing students with the Student class.
Another example is online shopping on Amazon or eBay. Both the customers and
online products must be abstracted into classes:
Polylines are a class of vector data represented by a list of points. For instance,
a river can be represented as a polyline on a map, which then can be categorized
as a type of polyline class. A polyline class may include point coordinates,
relevant attributes, and a set of methods. Another polyline dataset example can be
roads, highways, and interstates. Both examples are categories of polylines.
Rivers can be represented using UML (Figure 1.5). The first row of the UML is
the subject of the class: River; the second row includes the rivers attributes:
name and coordinates; and the third row refers to the methods the programmer
will use: getName, setCoordinates, and setName.
FIGURE 1.5
The River class includes three parts.
Polygons are another class of vector data that are also represented by a list of
points; however, with polygons, the first and last points are the same. For
example, on the map of the state of Virginia, a specific county, like Fairfax County,
can be represented as a polygon. The county is a type of polygon class, which
includes a list of points, relevant attributes, and a set of methods. Countries on a
world map may also be represented as polygons. In either case, both the county
and country are types of polygons. As shown in Figure 1.6, the first row is the
subject name: County; the second row is the subjects attributes: name and
population; and the third row refers to the methods: getName, setPopulation, and
setName.
FIGURE 1.6
The County class includes three parts.
Developing more methods will require adding more methods and attributes to
each class to capture the evolution of the data models and the functionality of
software; UML diagrams are used to standardize their representation. This section
uses class diagrams and relevant UML standards for the point, polyline, and
polygon classes.
1.6 UML
In 1997, the Object Management Group (OMG)* created the UML to record the
software design for programming. Software designers and programmers use UML
to communicate and share the design. Similar to the English language in which we
communicate through sharing our ideas via talking or writing, UML is used for
modeling an application or problem in an object-oriented fashion. UML modeling
can be used to facilitate the entire design and development of software.
The UML diagram is used to capture the programming logic. There are two types
of diagrams that we will specifically discuss: class diagrams and object diagrams
(Figure 1.7).
FIGURE 1.7
The class diagram and object diagram used in this book.
The UML class diagram can represent a class using three parts: name, attributes,
and methods. The attributes and methods have three different accessibilities:
public (+), private (-), and protected (#). Attributes and methods are normally
represented in the following format:
FIGURE 1.8
Inheritance and dependency.
FIGURE 1.9
(a) Aggregation and (b) composition are two polar relationships among classes.
FIGURE 1.11
worldMap is an object of the Map class and the state is changing with different operations.
FIGURE 1.12
Launch the Python programming window (GUI).
Programming tips:
PROBLEMS
Define computer, programming, software, and GIS.
What are the different methods to categorize software?
What are the three GIS data models found on the UML diagram?
Explain why we need to learn GIS programming.
Use the UML diagram to model the relationship between polylines.
Use the UML diagram to model the relationship between polygons.
Practice Pythons Chapter 3 tutorial:
https://docs.python.org/3/tutorial/introduction.html.
Use Python to calculate the distance between Point (1, 2) and Point (2, 2).
Discuss how to identify classes used on a world map and how to use UML
to capture those classes.
* http://pythoncard.sourceforge.net/what_is_python.html.
* See OMG at http://www.omg.org/.
2
Object-Oriented Programming
FIGURE 2.1
Print A 1000 times using different types of languages.
FIGURE 2.2
An example of defining a Point class with Python.
CODE 2.1
Creating a point may pass in value to the object through parameters.
objectName = className(value1,value2,)
In Code 2.1, we generated two objects, point0 and point1. While declaring
object point0, no parameter is passed while three values (1, 1, first point) are
used to generate point1.
To refer to an objects attribute or method, we start with the objectName,
followed by a period and then end with the attribute name or method name.
objectName.attributeName
objectName.methodName()
Code 2.1 uses .x, .y, and .name following the objects point0 and point1 to refer
to the attributes x, y, and name. The instruction point1.setName() is called to
change the name of point1 to second point.
2.2.3 Attributes
Each class may have one or more attributes. Section 1.4 explains how attributes
can be public, private, or protected to indicate different accessibility by other
objects. How do you explicitly specify the public and private attributes while
declaring a class?
CODE 2.2
Declare public, private, and protect attributes.
Another important attribute in Python is the static attribute, which is used to hold
data that is persistent and independent of any object of the class (Code 2.3). For
example, we can create a map including different layers, and the layer scale can
be static and the same to all layer objects.
CODE 2.3
Declare static attributes.
A class (and instantiated object) can have special built-in attributes. The special
class attributes include a class name and description of the class (Code 2.4).
CODE 2.4
Special class attributes.
The special object attributes include a class name and an objects attributes
(Code 2.5).
CODE 2.5
Special object attributes.
2.2.4 Inheritance
Chapter 1 introduces three important relationships among objects in object-
oriented programming: inheritance, encapsulation, and polymorphism. Inheritance
is an efficient way to help reuse a developed class. While private attributes and
methods cannot be inherited, all other public and protected attributes and methods
can be automatically inherited by subclasses.
To inherit a super class in Python, include the super class name in a pair of
parentheses after the class name.
class DerivedClassName(SuperClass1)
We can also inherit multiple classes in Python by entering more than one class
name in the parentheses.
2.2.5 Composition
Composition is an efficient way to help us reuse created objects, and to maintain
the part-to-whole relationship between objects. To maintain the composition
relationship, you must define a class with an attribute that can include a number of
other class objects.
Figure 2.4 shows an example of composition. The class Point and the class
Polygon inherit from the class Feature. The class Polygon border is defined by a
sequence of points formed in a ring and is captured by point attributes. The points
coordinates are kept in the point objects. Not only does this show how a Polygon
object requires a number of Point objects, but also the composition relationship
between Point and Polygon.
FIGURE 2.4
Composition example (a Polygon class includes attribute points as objects generated from class Point).
Feature: Normally, a Feature class (Figure 2.5) has a name to keep the
feature name and a method draw() to draw the feature on a map. The draw
method should include at least two parameters, self and map. Self refers to
the object accessing feature object data while drawing, whereas a map
refers to the background that we will draw the feature on.
FIGURE 2.5
UML design for Feature class to define common attributes and methods of Point, Polyline, and Polygon.
For example, Code 2.6 is an example of defining Feature as a class in
Python:
Point: A Point class (Figure 2.6) should have at least two spatial attributes,
x and y, to represent the coordinates of a point. Another nonspatial attribute
could include the name. A Point class could also use calDis() to calculate
the distance between two points. The argument for the calDis method is
current point object self, and another point object point. The return
value for the distance between two points is designated as float.
CODE 2.6
Define a Feature class as a super class for Point, Polyline, and Polygon.
FIGURE 2.6
UML design for Point class to keep point vector data.
CODE 2.8
Calculate the distance between (1, 2) and (2, 2).
FIGURE 2.7
(a) UML Polyline class uses point object list to keep coordinates for polylines. (b) UML Polylines class uses x
and y lists to keep coordinates data for Polylines.
Polygon: A Polygon class (Figure 2.8) could have one attribute, points,
to represent the list of all points used to define the border or two liststhe
x and y coordinates for all such points. A Polygon class may also have a
method getLength() to calculate the border length of the Polygon without
arguments. The return value for the border length of polygon is designated
as float.
FIGURE 2.8
UML design for Polygon class to keep polygon vector data.
CODE 2.9
A Polyline class has one attribute (points), two methods (setPoints(), and getLength()).
PROBLEMS
1. Pick three points, for example, (1, 100), (25, 60), and (1, 1). Could you
form a polyline or polygon using these three points?
2. Create an algorithm to calculate the distance between two points, for
example, (x1, y1), (x2, y2).
3. Read Python Tutorial 6.2 and 6.3. (Use Python command line window for
6.2).
4. Define and program three classes for Point, Polyline, and Polygon.
5. Add distance calculation in-between every two points, and program to
calculate the distance among the three points given.
6. Add the getLength() method in Polyline and Polygon; create a polyline
and polygon using the three points given; calculate the length of the
polyline and perimeter of the polygon.
* http://grass.osgeo.org/.
* http://www.esri.com/software/arcgis.
Section II
Python Programming
3
Introduction to Python
3.2 Syntax
3.2.1 Case Sensitivity
Python is case sensitive, meaning capital and lowercase letters represent different
identifiers. You can define a variable myList with an uppercase L, and store the
list of items 1, 2, 3, and 4. If you get the first value of the list using mylist[0]
with a lowercase l, you will see a NameError, which shows that mylist is not
defined because you defined myList using a capital L (Code 3.1).
CODE 3.1
Case sensitive.
3.2.3 Indentation
In Python, indentation is important for grouping code. Indented lines start at
different positions or column; numbers are not allowed, they will trigger an
IndentationError. You may use a different number of spaces or columns to indent
different levels of statements; however, 4 or 8 spaces are recommended.
Therefore, space and tabs play significant roles in organizing codes. Different
program editors (e.g., command line and Python GUI) use tab in different
manners. Depending on your text editor, it may represent different numbers of
spaces.
3.2.4 Keywords
Keywords, such as def and del, are reserved words and cannot be used for any
other purpose (e.g., as the names of variables, classes, and objects); otherwise, a
SyntaxError will occur (Code 3.2). Table 3.2 lists the keywords in Python.
CODE 3.2
Keywords SyntaxError example.
TABLE 3.2
Python Keywords
and elif global or
assert else if pass
break except import print
class exec in raise
continue finally is return
Def for lambda try
Del from not while
CODE 3.3
Multiple assignments.
The first line of code assigns the same value to multiple variables by using
a = b = c = value.
The second and third lines of code assign different values to different
variables by using a1, b1, c1 = v1, v2, v3, or (a2, b2, c2) = (v1, v2,
v3).
3.2.6 Namespace
A namespace is a place in which a name resides. Variables within a namespace
are distinct from variables having the same names but located outside of the
namespace. It is very easy to confuse names in different namespaces. Namespace
layering is called scope. A name is placed within a namespace when that name is
given a value. Use dir to show the available names within an indicated
namespace. For example, dir() can find current namespace names, dir(sys) will
find all names available from sys, and dir(math) will find all names available
from math. A program typically includes three layers of scope (Figure 3.1):
The top layer is a system built-in namespace, which includes names defined
within Python itself; these are always available in your programming
environment.
The middle layer is global namespace defined within a file or module.
The bottom layer is local namespace, which includes names defined within
a function or a class method.
FIGURE 3.1
Hierarchy of namespaces.
3.2.7 Scope
Scope refers to a portion of code and is used to identify the effectiveness of
variables. Scope is important in functions, modules, classes, objects, and returned
data. Modules with function and data act similar to objects. For example, when
defining Point class, use p1 as a variable within the calDis() function of the class;
or use p1 to refer to an object later when creating a point object. The first p1 is
only effective in the scope of the Point class calDis() function. The second p1 is
only effective at the same level as the overall program without indentation.
Variables can be local or global:
Local variables are declared within a function, and are only accessible
within the function. Once the function is executed, the variable will go out
of scope.
Global variables are nonlocal and can be accessible inside or outside of
functions.
In the example of Figure 3.2, global_x is a global variable and local_y is a local
variable. If you use local_y variable outside the function, you will get an error.
For more information about namespaces and scope, please refer to Raschka
(2014).
FIGURE 3.2
Local and global variables.
Number Integer 231 ~ 232 decimal, octal, and 20, 20, 010, int(), e.g., int(2.0),
hexadecimal format ox80 int(2), int(2L)
Long Limited only by memory Denoted by (L) or 20L, 20L, long(), e.g.,
integer (l). 010L, ox80L long(2), long(2)
float Depends on machine Denoted by a 0.0, 77.0, 1.6, float(), e.g.,
architecture and python decimal point (.) 2.3e25, 4.3e-2 float(2),
interpreter
String N/A Denoted by (), test, test str(), e.g., str(2.0)
()
Integers
Integers are equivalent to integers in C programming language. The range
of an integer is limited as follows:
231 ~ 232 (2147483648~4294967296)
The integer value can be represented by a decimal, octal, and
hexadecimal format. For example, 010 is an octal representation of 8 and
ox80 is a hexadecimal of 8.
Long integers of nonlimited length
The range of long integer is only limited by computer memory. A long
integer is denoted by appending an upper- or lowercase L. It can also be
represented in decimal octal and hexadecimal formats.
Float
Floating numbers are equivalent to doubles in C language. A float value is
denoted by a decimal point (.) in the appropriate place and an optional e
suffix (either lowercase or uppercase) representing scientific notation. The
precision, or range of the float, depends on the architecture of a machine as
well as the Python interpreter used.
Conversion of numbers
The int(), long(), float() built-in functions are used to convert from any
numeric type to another (Code 3.4).
CODE 3.4
Data type conversion.
Tips
Typecast: Convert data from one type to another, for example, float (3.14),
which casts a string data type to float.
Type conversed assignment may result in lost precision, for example
y = 3.14
x = int(y)
where x will lose the precision values and has a value of 3 as a result.
Strings
String data are denoted by single quotes or double quotes .
Other built-in types
There are several other built-in data types, such as type, None, function,
and file. Code 3.5 illustrates the following types:
Function type () takes an object as an argument and returns the data
type of the object.
None is the null object and has no attribute.
bool object has two potential values: True and False. Conditional
expressions will result in Boolean value as either True or False.
CODE 3.5
Function type, None type, and bool type.
Tips
Different from C or Java language, Python does not support Byte, Boolean, Char,
Pointer data types.
List
The most commonly used and important composite data type is list, which
can be used to group different values together. Use list to keep a series of
points, polylines, and polygons in GIS programs.
Define: A list object can be created from: [v1, v2, v3, .], where
elements are surrounded by a square bracket (e.g., as in Code 3.6).
CODE 3.6
List example.
seq[index]: gets a value for a specific element. The starting index of all
sequence data is 0, and the end index is one fewer than the number of
elements n in the sequence (i.e., n-1) (Code 3.6a).
CODE 3.6a
A List operation.
If you try to access an element with an index that is larger than the number
of the total elements, then you will get an IndexError, indicating the index is
out of range (Code 3.6b).
CODE 3.6b
List operation out of range.
len[list]: gets the length of the elements (calculates the total number of the
element) (Code 3.6c).
CODE 3.6c
List length.
CODE 3.6d
Subset a List.
CODE 3.6e
Delete an element from a List.
CODE 3.6f
List multiplies an integer.
CODE 3.6g
Union two sequence objects.
obj in seq (obj not in seq): loops through each object in the complex data
and performs an operation with each element. The example goes through
each object in the list a, and adds the value of each object to the sum obj
(Code 3.6h).
CODE 3.6h
Loop each object in the complex data.
String data type also belongs to the sequence data type, and those operators can
be applied to a string object.
Methods: As seen in the classes created in the previous chapters, a list is a
system built-in class. The objects created from list have many methods. The most
important methods include append(), insert(), remove(), pop(), sort(), and
reverse() (Code 3.7).
CODE 3.7
List methods.
Built-in functions: There are three common built-in functions for handling a list,
which are handy in GIS programming (Khalid 2016):
filter(func, list): Filter is used to extract a target list from the original list.
For example, you can use the filter function to select all cities within
Virginia, or select the restaurants and hotels within Fairfax city.
map(func, list): Map is used to convert the original list to a new list using
the function. For example, you can convert it from degrees to meters.
reduce(func, list): Reduce is another method that is useful for real-world
GIS problems. To calculate the total street or road length of Fairfax County,
reduce can invoke a function func iteratively over each element of the list,
returning a single cumulative value.
Tuple
Similar to lists, tuple is another complex data type. One obvious difference
between tuple and list is that it is denoted by the use of parentheses.
Another difference is that tuple data type is immutable (Table 3.4),
meaning that the element cannot be altered once it is defined. An error will
occur if the value of a tuple element is altered (Code 3.8).
CODE 3.8
Tuple operation.
Dictionary
A dictionary is mutable and a container data type that can store any Python
objects, including other container types. A dictionary differs from sequence
type containers (lists and tuples) in how the data are stored and accessed.
Define: The syntax used to define a dictionary entry is {key:value, key:value,
..}, where all elements are enclosed in braces. It is convenient for storing
spatial feature objects so each object will include a unique object ID, spatial
attributes (coordinates), and nonspatial attributes (e.g., Name). Where unique IDs
can be used as key, all attributes can be used as a value. The keys are integers or
strings while values can be any data type, such as list or dictionary (Code 3.9).
CODE 3.9
Dictionary operation.
In Code 3.9, parkingLots is declared as a dictionary data type that includes two
elements. The unique ID and parking lot sequence A and B are used as the keys,
and the attribute information of each key is held in a distinct list.
Set
Set is used to construct and manipulate unsorted collections of unique
elements. A set object can either be created from {v1, v2, v3,.} where
elements are surrounded by braces, or from a set(list) where the argument
is a list (Code 3.10).
CODE 3.10
Set operation.
Operations: Set supports several operations (Table 3.5), including union (|),
intersection (&), difference (-), and symmetric difference (^) (Linuxtopia 2016).
TABLE 3.5
Operations between Two Set Objects, s and t
Operation Operator Function Usage
Methods: Set is a system built-in class. Several important methods are supported
by a set object, including add(), remove(), and pop(). It also supports four
methods: difference(), intersection(), symmetric_difference(), and union() (Code
3.11).
CODE 3.11
Set operations.
3.4 Miscellaneous
3.4.1 Variables
A variable is a memory space reserved for storing data and referred to by its
name. Before use, a variable should be assigned a value. Variables have different
types of values. The basic variable value types include byte, short, int, long, text,
float, and double, as introduced in Section 3.4.1. Some types of data can be
converted using typecast. For example, float(3.14) will convert texts into a
floating number 3.14.
In Python, variable types are dynamic, with the type only defined when its value
is assigned. This means a variable can change to a different data type. For
example (Figure 3.3), x = float(1) will assign x as float, but x = x has a dynamic
type will change the variable x from a float type to a string type. Here, we use
p1 as an object variable name.
FIGURE 3.3
Dynamic data type.
A name is required for each variable. The variables name must be a legal
identifier, which is a limited combination series of alphabet letters, digits, and
underscores. The name must begin with a character or underscore, but it may not
start with a digit. Therefore, point1 is a legal name, but 1point is illegal. In
addition, blanks are not allowed in variable name. Python reserved words (Table
3.2) cannot be used as variable name.
FIGURE 3.4
Coding style.
3.5 Operators
Operators include basic characters, division and type conversion, modulo,
negation, augmented assignment, and Boolean operations. These operators are
categorized into several types:
TABLE 3.6
Arithmetic Operators (Assume Variable a Holds 5 and Variable b Holds 2)
Arithmetic
Description Example
Operators
+ Addition >>> a + b
7
Subtraction >>> a b
3
* Multiplication >>> a * b
10
/ Division >>> a/b
2.5
** Exponentiation: Performs exponential (power) calculation on operators >>> a **
b
25
% Modulus: Divides left-hand operand by right-hand operand and returns >>> a %
remainder b
1
// Floor Division: The division of operands where the result is the quotient in which >>> a //
the digits after the decimal point are removed b
2
>>> 5.0
// 2.0
2.0
>> Binary Right Shift Operator. The left operands value is moved right by the a >> b will give 1,
number of bits specified by the right operand. which is 0000 0001
<< Binary Left Shift Operator. The left operands value is moved left by the a << b will give 20,
number of bits specified by the right operand. which is 0001 0100
& Binary AND Operator copies a bit to the result if it exists in both operands. a & b will give 0, which
is 0000 0000
| Binary OR Operator copies a bit if it exists in either operand. a | b will give 7, which
is 0000 0111
^ Binary XOR Operator copies the bit if it is set in one operand but not both. a ^ b will give 7, which
is 0000 0111
TABLE 3.8
Assignment Operators (Assume Variable a Holds 5 and Variable b Holds 2)
Assignment Operators Name How to Use Equivalent Result
Logic Operators (Table 3.10): Logic operators are used together with if,
else, while keywords to create logic control statements (Section 3.4).
Statements with logic operators are either True or False. Code 3.13 shows
an example.
CODE 3.13
Logic operations.
TABLE 3.10
Logic Operators (Assume Variable a Holds True and Variable b Holds True)
Logic
How to Use Results
Operators
And Logical AND operator. If both the operands are true, then condition becomes (a and b) is
true. true.
Or Logical OR operator. If any of the two operands are nonzero, then condition (a or b) is
becomes true. true.
Not Logical NOT operator. Use to reverse the logical state of its operand. If a Not (a and
condition is true, then Logical NOT operator will make false. b) is false.
3.6 Statements
A statement is a combination of variables and operators. The statement should
comply with the operators usage. If you assign a value, you should use assignment
operator. If you accidentally use comparison operators, you should expect an
error. Pay attention to the statements precision. For example (Code 3.14), the first
and second i seem to be assigned with similar values using identical division and
addition operations. However, they generate different results.
CODE 3.14
Statement examples.
3.7 Functions
Functions are defined by the keyword def followed by the function name with
various input arguments. Similar to the methods defined within a class, the number
of arguments can be zero to many. Class methods are special functions with the
first argument as self.
def funcName(arg1, arg2, ):
Statement blocks
A function is defined by a function name and other required input arguments, like
funcName(arg1, arg2, ). The arguments passed to a function while calling a
function should match what is declared. A function can return none, one, or more
arguments (Code 3.15).
CODE 3.15
Return value from a function.
When you declare default arguments for class methods (Section 2.1), you can
also set up default values for function arguments, and make those arguments
optional for the caller. The example (Code 3.16) shows how to calculate cost
function (calCost) and taxRate as the default argument. Therefore, when the
calCost function is set with the parameter as 100, the variable taxRate uses 0.05
as its value.
CODE 3.16
Default arguments.
Tips
Use the keyword lambda to declare one line version of a function. Usually such
functions are anonymous because they are not defined in a standard manner. The
body of the lambda function statement should be given on the same line, like in the
add() function (Code 3.17).
CODE 3.17
Lambda example.
The Python interpreter has built-in functions that are always available. They are
listed in alphabetical order in Figure 3.5. The functions written in red have
already been introduced. The functions written in blue are important, and will be
introduced in later chapters.
FIGURE 3.5
System built-in functions. (From Python. 2001a. Built-In Functions. https://docs.python.org/3/library/index.html
(accessed September 3, 2016).)
Python syntax
Python data types
Operators
What a function is and how to declare a function
PROBLEMS
1. Keywords: Check the following keywords (Table 3.2.) and briefly explain
them (concepts, when/how to use keywords [use function help() to get help
about each keyword, e.g., help(if)], and design/program an example of
how to use the keywords). For example, if, elif, else keywords.
a. Explain
if, elif, else are the keywords used to make decisions
b. Examples (Code 3.19)
CODE 3.19
Ifelifelse example.
Description: A Point class has two attributes, x and y, to represent the coordinate
of a point. A Point class also has a method calDis () to calculate the distance
between two points. The arguments for the calDis method is point object self,
and the another point object is point. The return value for the distance between
two points is designed as float.
The UML design for Point class to keep point vector data is as follows:
Point(Feature)
+x: float
+y: float
+ calDis(p: Point): float
The following Code 3.20 exemplifies how to implement the Point class:
Description: A Polygon class has one attribute points to represent the list of
coordinates. A Polygon class also has a method getLength () to calculate the
perimeter of the Polygon. The arguments for the getLength method is current
Polygon object self. The return value for the border length of a polygon is
designed as float.
CODE 3.20
Point class definition.
The UML design for a Polygon class keeping polygon vector data is as follows:
Polygon(Feature)
+points: list<Points>
+ getLength(): float
Assign a polygon with the following data: [(1.0, 2.0), (3.0, 5.0), (5.0, 6.0), (1.0,
2.0)] and calculate the border length of the polygon.
4
Any number with a value of zero (e.g., 0, 0.0, 0L, 0j, Code 4.1 right)
CODE 4.1
False conditional expressions: empty list (left) and zero (right).
An empty string ( or )
An empty container, such as list (Code 4.1 left), tuple, set, and dictionary
False and None
Comparison Operators: >, >=, <, <=, = =, !=, is, is not, in, not in
Logic Operators: and, or, not
else:
Statement block n
If the conditional expression 1 (or 2, 3,.) is true, then the statement block 1 (or
2, 3.) will be executed and the other statement block will be skipped. However,
if all above conditions (1, 2, 3, ., n1) are not true, the blocks under else
(statement block n) will be executed.
Tips: pass statement (Code 4.2)
CODE 4.2
Pass statement in if else structure.
pass statement is unique in that it does not perform any function. It is used in the
decision-making process, telling the interpreter not to do anything under certain
conditions.
In the software development process, it can serve as a place holder, to be
replaced later with written code (Code 4.3).
CODE 4.3
Pass is used as a place-holder statement written in method.
4.2 Loops
Another type of control structure is the loop. Usually, a loop executes a block until
its condition becomes false or until it has used up all the sequence elements in a
container (e.g., list). You can either interrupt a loop to start a new iteration (using
continue) or end the loop (using break). Both while and for can be used to loop
through a block of statements.
Syntax:
for item in sequence:
Statement block
CODE 4.4
Use range function with default start and step values.
while statement: while statement is very flexible, and can repeat a block of
code while the condition is true. The while statement is usually applied
when there is an unknown number of times before executing the loop. Code
4.5 shows an example of using while loop to calculate the sum of 1 to 100.
CODE 4.5
Calculating summary of 1 to 100 using while loop.
range() function and len() function: The range (Pythoncentral 2011) and
len functions are often used in for and while loops. Using range(start, end,
step) generates a list where for any k, start <= k < end, and k iterates from
start to end with increments of step. For example, range(0,4,1) produces a
list of [0,1,2,3]; range(0,50,10) produces a list of [0,10,20,30,40]. range
function takes 0 as default starting value and 1 as default step. For example,
range(4) produces a list of [0,1,2,3]. Code 4.4 is an example using range(4)
to produce a list, and using for loop structure to print every element within
the list.
The following example illustrates how to use the range function, and how to
calculate the sum of 1 to 100 by using the total variable to hold the summarizing
result for loop (Code 4.6).
CODE 4.6
Calculate the summary of 1 to 100 using range and for loop.
The function len() returns the total number of elements in composite data. For
instance, len(polyline.points) can return the number of points within a polyline.
The following example uses while and len() to calculate the length of a polyline
(Code 4.7).
CODE 4.7
Calculate the length of a polyline using while loop and len() method.
CODE 4.8
Test if two lines intersect with each other using break and for loop.
continue: The continue statement is used less often than the break statement
to skip the rest of a loop body under a certain condition. This can eliminate
executions of specific loop values or categories of values. For example,
when a map is drawn, you may uncheck the layers with an image. The code
(Code 4.9) skips all image layers:
CODE 4.9
Draw a map without drawing the image layers.
CODE 4.11
Calculate distance between points using double loops.
Opening a file: To operate a file, use the function open with a filename and
file operation mode as arguments. In the example f = open(filename, mode),
mode could be
r: when the file will only be read, and this is the default value.
w: for only writing (an existing file with the same name will be
erased).
a: open the file for appending; any data written to the file are
automatically added to the end of file.
r+: open the file for both reading and writing.
Reading a file: The open() function will load the file from disk storage to
the memory, and return it as a file object. This file object has three file read
methods:
read(size): returns the entire file when no size parameter is passed (by
default the size is equal to 1), or content as a string in the byte size
specified.
readline(): reads and returns one line as a string (including trailing \n).
readlines(): reads and returns all lines from file as a list of strings
(including trailing \n).
Writing a file: A file object has two written methods:
write(str): writes string str to file.
writelines(list): writes a list of strings to file; each string element is one
line in the file.
The write function writes the contents of a string to a file. However, there are
other data types such as float and integer. How is data written into a file? Since a
string acts as the input argument, the str() and repr() function will convert a
nonstring object to a string object. For example, if you write a float number 3.14
into a file, you can use write(str(3.14)). Typically, a text file is organized line by
line, while the write() function writes data into a file and changes it to a new line
using the special characters \n. Thus, write (first line\nsecond line) will
output two lines in the file as shown below:
first line
second line
The return value is also a string when reading the data from a file with read()
and readline(); therefore, we need to format those string values into the data type
we prefer. We can use float(str), for example, float(3.14), int(str), for example,
int(2.0), and long(str), for example, long(2.0l) to convert strings to numbers.
Change file objects pointer position: While reading a file, we may need to
skip several lines and read out specific information in the file. Under such
circumstances, we can locate specific lines and words of a file with the
following two methods:
seek(offsize, whence): go to a position within a file, with offsize bytes
offset from whence (0==beginning of file, 1==current location, or
2==end of file).
tell(): return current cursor/pointer position within a file.
Close a file: After we finish manipulating a file, we should close the file to
release it from the memory with the method:
close(): close a file
4.5 Exceptions
Exceptions (Python 2001b) are the errors encountered when executing Python
programs, for example, the errors to open a nonexisting file, division by zero,
concatenate str and int objects. These exceptions can be handled in Python
programs using the tryexcept statement. There can be one or more except
clauses in the statement. Each except clause is used to catch one exception. If an
exception clause matches the exception, the program will execute the except
clause. If no except clause matches, the program will be passed on to outer try
statements and give the exception error. Code 4.12 handles the ZeroDivisionError
using the tryexcept statement.
CODE 4.12
Handle ZeroDivisionError using tryexcept statement.
Try and test another method where multiple loops are used (Code 4.14).
CODE 4.14
Calculate the longest distance between any two points of a 4 point set using double for loop with i and j as
incremental variables.
In this method, double loop is used to calculate the distances between each pair
of four points (p0, p1, p2, p3). As shown as Figure 4.1, during the outer loop level
1, when i=0, the inside loop will iterate j from 1 to 2 to 3. After the outer loop
advances to i=1, j will be iterated from 2 to 3, and so forth.
FIGURE 4.1
Use double loop to calculate the distance between each pair of four points p0, p1, p2, p3.
CODE 4.15
Write a text file.
Note: (1) Make sure the directory c:/code exists, or you will generate an
error such as: IOError: [Errno 2] No such file or directory:
c:/code/points.txt. makedirs() function in os module could help to create
directory; (2) Make sure you have the permission to create a file under
c:/code directory, otherwise you will generate an error such as IOError:
[Errno 13] Permission denied: c:/code/points.txt. For Mac, or Linux OS,
replace the directory c:/code/points.txt with a different one, for example,
/home/points.txt.
2. Go to the points.txt directory, and check that there is a points.txt created
(which should be true if there is no error popping up for your code). Now
open the file and examine the information in the file.
3. Code 4.16 reads out the data from the points.txt.
CODE 4.16
Read from a text file.
point:
1.0, 1.0; 2.0, 2.0
Point:
1: 1, 2
2: 100, 300
3: 4, 5
4: 0, 500
5: 10, 400
6: 600, 20
7: 500, 400
8: 500, 500
PROBLEMS
1. Review the Python tutorial Input and Output, which came with Chapter
7 of the Python software help document.
2. Analyze the patterns of the following text string and save it to a text file,
for example, polylines.txt.
Polyline:
1. 1603714.835939442,142625.48838266544;
1603749.4678153452,142620.21243656706;
1603780.3769339535,142607.37201781105;
1603801.475846678,142582.27024446055;
1603830.4767344964,142536.14692804776;
2. 1602514.2066492266,142330.66992144473;
1602521.4127475217,142414.92978276964;
1602520.1146955898,142433.93817959353;
1602501.3840010355,142439.12358761206;
1602371.6780588734,142417.84858870413;
1602351.6610373354,142408.02716448065;
1602334.5180692307,142388.58748627454;
1602331.6999511716,142376.66073128115;
1602334.8067251327,142348.965322732;
1602338.308919772,142323.6111663878;
1602349.0226452332,142314.50124930218;
1602363.9090971674,142310.79584660195;
1602514.2066492266,142330.66992144473;
3. Write a Python program to parse the text file and use list to hold the two
polylines. Please refer to Section 5.6.1 in Python Library Reference (from
Python help document) String methods for split(), strip(), and Built-in
Functions float(x).
4. Generate two polyline objects.
5. Calculate the length of the two polylines.
6. Review the class materials on handling exceptions and Python tutorial
Errors and Exceptions (Section 8.3 in Python help document).
7. While reading the file and converting the string data to another data type,
for example, float, please add tryexceptfinally to catch the
Exceptions, for example, IOError and ValueError.
5
CODE 5.1
Create a GUI using Tkinter.
In this example, a window was created with one label showing Hello World.
TKinter supports a variety of widget objects; the most common are described in
Table 5.1.
TABLE 5.1
Popular TKinter Widgets
Widgets Syntax Example
Button Using (master, options) as parameters to initialize a widget, root is the Button(root,
parent widget, options are the widget options such as command, back text="OK",
command=callback)
Canvas Canvas(root, width =
800, height = 600)
Label Label(root, text="Hello,
world!")
Listbox Listbox(root, height=8)
Menu Menu(root,
borderwidth=2)
Message Message(root,
text="this is a
message")
Radiobutton Radiobutton(root,
text="Grayscale",
value="L")
Scrollbar Scrollbar =
Scrollbar(root)
Text Text(root, font=
("Helvetica", 16))
In addition to the widgets in Table 5.1, Tkinter has other widgets, which include
Entry, Frame, LabelFrame, Menubutton, OptionMenu, panelWindow, Scale,
Spinbox, and Toplevel.
When using other widgets, replace the third line and fourth line (in Code 5.1) by
creating an object of a specific widget and passing it in relevant arguments
specified on the online reference,* for example, when using Canvas for map,
replace the third line and fourth line with
can = Canvas(root, width = 800, height = 600)
can.pack()
Among all the widgets, Canvas is the most widely used. It supports many methods,
like drawing points, lines, polylines, and polygons. A typical Canvas preparing
code is shown in Code 5.2.
CODE 5.2
Preparing a Canvas for drawing.
In Code 5.2, the third line creates a Canvas with the pixel size dimensions of
800 by 600. Although the size is based on the computer monitor size, the actual
size can be adjusted on the display settings tab. The second to last line ensures
that Canvas is visible on the window and the last line will ensure the window
shows up. The Canvas prepared can be used as the map cloth, which can be used
to draw points, polylines, and polygons. The next step is to prepare GIS data so
that it can serve as visualization on Canvas.
FIGURE 5.3
An example of geographic area and window monitor size with 800 600.
FIGURE 5.4
Calculation of ratioX and ratioY.
TIPS: Using different ratios for the x- and y-axis will cause the map to be distorted
in the visualized map.
A reference point is another important element when converting geographic
coordinates to monitor coordinates. Reference points could be centers of both
systems, which are (12.385, 1.61) and (400, 300), or upper left of both
systems: (179.00, 85.78) and (0, 0).
FIGURE 5.5
The monitor coordinates of four corner points are based on ratioX (left) and ratioY (right), using the upper left
corner as the reference point.
As shown in Figure 5.5, using ratioX will not use all 600 pixels of window
height; however, not all features will show up while using ratioY (window
coordinates are out of boundary). Typically, the larger one should be selected as
the ratio value to ensure that all features are displayed on the screen at the
initialization stage.
Finally, transform the geographic coordinates (x, y) to the screen pixel
coordinates (winx, winy) after both the ratio and reference points (X0, Y0) are
determined using the following formula:
winx = (XX0)/ratioX
winy= (X Y0)/ratioY (add a negative sign to flip the y-axis direction)
For example, if ratioX and an upper left point (179.00, 85.78) are (0, 0), any
point (x, y) from the GIS data will serve as the coordinates for the monitor
window (Figure 5.6):
winx = (x (179.00))/ratioX
winy= (y (85.78))/ratioX
FIGURE 5.6
Coordinate conversion using ratioX as ratio and the upper left corner as the reference point.
Each of these methods will take different arguments as listed in the table. For
example, create_arc will take the parameters of x0, y0, x1, y1, options. The (x0,
y0) point will define the upper left point and the (x1, y1) point will define the
lower right point of the rectangle (in which the arc will be drawn). There are
many options, such as start (the beginning angle of an arc), extent (the width of the
arc in degrees), and fill (the color used to fill in the arc). Figure 5.7 shows how to
create a Canvas object and create three arcs using the same rectangle points, but
with different colors and extents. As illustrated, the angle starts from a positive x-
axis and goes counterclockwise.
FIGURE 5.7
Create an arc with Tkinter. (a) Source code, (b) Draw arc, (c) Draw line.
As shown in Figure 5.7a, the source code creates a window, where Canvas is
drawn creating a 30 degree arc (270 degrees red, 60 degrees blue, and 30 degrees
green).
can.create_line (1,2,35,46,5,6,76,280,390,400)
Then run the code and check the GUI to see whether it is the same as Figure 5.5c.
FIGURE 5.9
Programming components/steps for the Chapter 4 problem.
The more patterns practiced, the more experienced a programmer will become
to find a solution.
"""
Chapter#4
Read the following data:
Polyline;
1: 1603714.835939442,142625.48838266544;
1603749.4678153452,142620.21243656706;
1603780.3769339535,142607.37201781105;
1603801.475846678,142582.27024446055;
1603830.4767344964,142536.14692804776;
2: 1602514.2066492266,142330.66992144473;
1602521.4127475217,142414.92978276964;
1602520.1146955898,142433.93817959353;
1602501.3840010355,142439.12358761206;
1602371.6780588734,142417.84858870413;
1602351.6610373354,142408.02716448065;
1602334.5180692307,142388.58748627454;
1602331.6999511716,142376.66073128115;
1602334.8067251327,142348.965322732;
1602338.308919772,142323.6111663878;
1602349.0226452332,142314.50124930218;
1602363.9090971674,142310.79584660195;
1602514.2066492266,142330.66992144473;
Code 5.3 defines the function readPolylineFile to read data line by line. The
readPolylineFile function will return two values: polylines and firstpolylineNum,
which refers to how many points we have for first polyline.
Problem analyses
Pattern matching
Coordinate transformation
Drawing vector data on Canvas
Two coding examples are used to demonstrate the programming thinking
process: (a) reading, parsing, and calculating length for polylines, and (b)
generating random points and rectangles; and check the contain relationship
between every point and rectangle
PROBLEMS
1. There is a module named random in Python; import it and use its method
random() to generate a random number from 0 to 1.
2. There is a popular algorithm in GIS to find whether a point is inside a
rectangle based on their respective point coordinates (x, y, and minx, miny,
maxx, maxy). Describe the algorithm in a mathematical algorithm using (x,
y, and minx, miny, maxx, maxy).
3. Write a program to (a) generate m number of points and n number of
rectangles (m and n can be changed through user input), (b) check which
points are in which rectangles.
4. Program to write the point coordinates and rectangles point coordinates to
a text file, and then write the result of (2) to the text file.
5. In a Word document, explain the point in rectangle algorithm and
program created, and code the program in a .py file to find which point
generated in (3) is within which rectangle generated in (3). Then check
the text file output.
* http://www.pythonware.com/library/tkinter/introduction/tkinter-reference.htm.
6
Shapefile Handling
One of the most important functions of GIS software is to read popular GIS data
file formats, such as shapefiles. A shapefile is a binary data file format originally
developed by ESRI, and has been widely used for exchanging vector data among
different GIS professionals and communities (ESRI 1998). This chapter
introduces how shapefiles are formatted and how to read them with Python, that is,
reading binary data, reading a shapefile header, reading a point shapefile, and
reading polyline and polygon shapefiles.
struct.unpack(fmt, binarydata)
The struct module must be imported before using (the first statement of Code
6.1). The code also demonstrates how to pack two integers (100, 200) represented
by variables (x, y) into a binary string. String ii is used to represent two integer
values with each i representing one integer. The fifth statement unpacks the
binary string into its original data value (100, 200). The string ii is important
and referred to as the string format (denoted as fmt), which is used to specify the
expected format, and is required to call both pack and unpack methods. Table 6.1
details the format characters used in the string fmt to specify format for packing
and unpacking binary data.
TABLE 6.1
Format Characters
Format Character C Type Python Type Standard Size
Code 6.2 shows how to pack different formats using the format characters. Five
variables representing five values in data types of integer, Boolean, double, float,
and double are packed. The total length of the packed string is 4(i) + 1(b) + 8(d) +
4(f) + 8(d) = 25. Because the struct package is following C standard, the C Type
is used. Python has fewer data types; however, the standard size of each data type
can be kept if the first character of the format string is indicated by the byte order,
size, and alignment of the packed data.
CODE 6.2
Packing and unpacking different data types using proper format.
By default, the @ will be used if the first character is not one of the characters
given in Table 6.2 below:
TABLE 6.2
Struct Starting Character
Character Byte Order Size Alignment
Byte order* concept is also used in the ESRI Shapefile format. Big-endian and
little-endian byte orders are two ways to organize multibyte words in the
computer memory or storage disk. When using big-endian order, the first byte is
the biggest part of the data, whereas the first byte is the smallest part of the data
when using little-endian order. For example, when storing a hexadecimal
representation of a four-byte integer 0 44532011 (Table 6.3) using the big-
endian order binary format, the byte sequence would be 44 53 20 11.
For big-endian byte order, use > while packing or unpacking the bytes, for
example, struct.unpack(>iiii, s).
For little-endian byte order, use <, for example, struct.unpack(<iiii, s).
If these two are mixed, for example, packing using > and unpacking using
<, an unexpected result will be generated, as shown in the last statement
of Code 6.3.
TABLE 6.3
Four-Byte Integer 0 44532011 in the Storage
Big-Endian 44 53 20 11
Little-Endian 11 20 53 44
CODE 6.3
Pack and unpack must use the same byte order.
While constructing the formatted string, the byte order should be specified. In the
absence of a byte order symbol, packing and unpacking will use little-endian by
default for PCs (Code 6.4). The results of two codes show no difference. The
results are hexadecimal, that is, 0x 00000064 represents 100, 0x 0000c8
represents 200, and x0000012c represents 300. More about struct and formatting
info can be found at http://docs.python.org/library/struct.html.
CODE 6.4
The default byte order is little-endian for our computers.
.shp: The .shp file contains the vertices of all entities (Figure 6.1). The
vertices are organized hierarchically in features/records, parts, and points.
The .shp file also contains information on how to read the vertices (i.e., as
points, lines, or polygons). Some important attributes can also be termed as
the third dimension (measurements), and stored in the .shp file.
.shx: An index is kept for each record, and is beneficial for finding the
records more quickly.
.dbf: Attribute information is stored in the .dbf file associated with each
.shp file. The .dbf file contains dBASE tables and stores additional
attributes that cannot be kept in a shapefiles features. It contains exactly the
same number of records as there are features in the .shp file (otherwise the
data could not be interpreted). The records belong to the shapes
sequentially, meaning that the first, second, and third records belong,
respectively, to the first, second, and third, features in the .shp file. If we
edit the .dbf using a third-party tool and alter the records, the order may be
lost. More information can be found from the ESRI shapefile format white
paper (ESRI 1998).
FIGURE 6.1
Shapefile structure. (Adapted from ESRI. 1998. ESRI Shapefile Technical Description. An ESRI White Paper,
34.)
There are 14+ types of features supported by shapefiles, such as point, polyline,
and polygon. 3D features are added to shapefile structure with a dimension
dedicated to z value.
The rest of the file header is in little-endian byte order, and < is required to
unpack them, for example, struct.unpack(<iiii, s). Omit the < since it is the
default value for pack or unpack on most PCs. Starting byte 28, a 4-byte integer
(value of 1000) refers to the version of the shapefile. Starting byte 32, a 4-byte
integer, indicates the feature shape type (e.g., 1 means the file is for Point feature,
and 3 indicates it is a Polyline file, Figure 6.3 right). Byte 36 to the 100 is the
bounding box of the entire dataset in the shapefile. The bounding box includes four
dimensions x, y, z, and m. Each dimension includes minimum and maximum values
in the sequence of minx, miny, maxx, maxy, minz, maxz, minm, and maxm. The
bounding box should be written with fmt <dddddddd for all values in double
data type.
Hands-on practice: Interpret the shapefile header (Code 6.5)
The first three statements have 28 bytes from the shapefile. The fourth statement
unpacks the data in big-endian order and has seven integers. The first integer is
9994, the file code for shapefiles. The next five are zero, and are reserved for
future use. The last one is the file length, which is the total length of the shape
main file in 16-bit or two bytes unit. Therefore, the actual total length of the file is
double the indicated value (i.e., 288 * 2 = 576) bytes. The next statement reads out
72 bytes and unpacks them using little-endian byte order to obtain two integers
and eight double values. The first one, with a value of 1000, is the version of
shapefile. The second one, with value 1, indicates that the shapefile feature type is
Point. The following four refer to minx, miny, maxx, maxy, respectively, and minz,
maxz, minm, maxm are all zeros since these two dimensions are not used in this
shapefile.
FIGURE 6.5
File structure for Point .shp file.
Code 6.6 reads the shapefile to get the file length (bytes 2427 in file header)
and uses the file length to calculate the number of points in this shapefile in the
following steps:
1. Doubling the size to convert from 16-bit (two bytes) unit to 8-bit (one
byte) unit
2. Subtracting 100 bytes for a file header
3. Dividing by 28 (each record header and record content takes 28 bytes in
point shapefile) to get the feature number
The file length and number of point features are then printed out and a text file is
opened to write the results. A for loop is used to cycle through each record/feature
to read out the x, y values and print out and write to the text file. Lastly, the two
files are closed to conclude the file read/write process. In the for loop, the first
line moves the file pointer to the position where the ith records x value starts
(100 + 12 + i*28, 12 refer to the record header [8 bytes] and the shape type
integer 1 [4 bytes]), then reads 16 bytes for x, y and unpacks them into x, y
variables.
FIGURE 6.6
File structure for .shx file.
CODE 6.7
Interpreting the shape index file to get the number of records, and the offset, content length of each record.
Code 6.7 first reads the integer of index file length from bytes 2427. This
number, in 16-bit or two-byte unit is then used to calculate the feature number by
The feature number and file length are printed and a text file is opened to keep
each feature offset and content length value in the main file. The for loop reads
each record and writes it in the text file. Again, both files are closed at the end of
the program. The for loop cycles through each record to (a) move to the start of
the ith record (100 + i*8), (b) read out 8 bytes for two integers, and (c) unpack the
two integers as offset and contentLength, which are printed and written to the text
file following the sequence of the record. The text file and the output on the
interactive window can be used to verify the content.
6.3.3 The .dbf File
The .dbf file keeps attributes for the related shapefile. It has the same name as the
main and index file. Each feature must have a record in .dbf file. Each feature must
be in the same order in the .dbf file as it appears in the main and index files.
Details of the .dbf file format can be found online* and will not be discussed in
this book.
Steps 7 and 8 are for shapefile output. Unless converting each features spatial
attributes (coordinates) or nonspatial attributes from a different format (e.g., a text
file) to generate new shapefiles, these two steps are not required. This conversion
could also be easily accomplished using ArcGIS Python scripting, which will be
introduced later in this book.
FIGURE 6.7
(a) Select the countries folder to add data, and (b) the map window displays countries as polygons and with
country borders as polylines.
The country polygons and country border polylines will be displayed (Figure
6.7b). We can operate the map by selecting View-> [Zoom In |Zoom Out|Zoom
Extent|Zoom Full]. All the source codes for the file read, map display, and map
operation are included in the package. Please try to identify from the Mini-GIS
package the files that read shapefiles and visualize polylines and polygons.
Reading a polyline file can leverage the code developed for reading the feature
number, record offset, and record content length (Code 6.8). Once the offset and
content length for each record is identified, the rest will go to the starting position
of a specific record to begin interpreting the polyline. For each record header, the
first integer is the record sequence number and the second integer is the content
length, as shown in Figure 6.8. In the record content, the first value is an integer
showing the shape type as a value 3-polyline. The following 32 bytes are the
bounding box for minx, miny, maxx, maxy for the current feature. Because a
polyline may include multiple lines, each line of a polyline is considered a part.
The number of parts of a polyline follows the bounding box as an integer,
followed by the number of points as an integer. Based on the part number, there
are 4*part-Number bytes holding the starting point of each part in the entire point
sequence in the current feature. The starting number is 0 for the first part.
Following the part index are the points x, y coordinates. If z and m dimensions
are supplied in the format, relevant fields, such as bounding box and points
coordinate values will adjust according to store z and m values. Based on these
analyses, the Python code for reading a shapefile can be structured as follows
(Code 6.8):
1. Type/copy the code into the programming window, save the python file.
2. Copy the data Partial_Streets.shp and Partial_Streets.shx to the same
folder where you save the python .py file.
3. Run the python file.
4. Explore and analyze the code to understand each section of the code.
CODE 6.8
Reading and visualize polyline shapefiles.
PROBLEMS
1. Pick a polyline shapefile with 10100 features and one polygon file with
10100 features.
2. Write a Python program to read the polylines and polygons from the
shapefiles. (We can use .shp and .shx files.)
3. Construct polyline and polygon objects in Python.
4. Draw the polylines/polygons using Tkinter package.
5. Comment and explain the program to show the logic flow of your
programming by referring to Code 6.8.
7.1.2.1 Highlighting
Coloring the code can help you better understand, capture, communicate, and
interact with peer programmers. In Python IDLE, code (including comments) can
be highlighted with different colors based on the types of the words input. For
example, keywords can be set as yellow, variables as black, and functions as blue
(Figure 7.4). These settings can be customized in the Highlighting tab.
FIGURE 7.4
Color of different parts of a program can be highlighted for better formatting, communication, interaction, and
programming.
7.1.3 Debugging
Testing the code to fix errors and improve the robustness is called debugging. It is
a must-have process in programming because it is almost impossible to write bug-
free codes. This section will go through basic skills for errors/exceptions
handling and debugging.
7.1.3.1 SyntaxError
Syntax errors occur when syntax requirements are not met, and are detected in the
interpreting process before the program is executed. Syntax errors are removed
when translating the source code into a binary code. When a syntax error is
detected, the Python interpreter outputs the message SyntaxError: invalid syntax.
Such errors occur frequently, especially when you are unfamiliar with Pythons
syntax. Code 7.1 shows a syntax error with a missing colon (:) after True.
CODE 7.1
While invalid syntax problem.
Unfortunately, error messages are often not informative. Described below are
four common mistakes that result in syntax errors; this list can be used to help you
detect problems:
Indent correctly? In Code 7.2, elif statement should be at the same
indentation level as if statement.
CODE 7.2
If invalid syntax because of indentation.
CODE 7.3
Missing parts of a statement syntax error.
Using if, elif, or while without any conditional expression.
CODE 7.4
Examples of run-time exceptions.
TABLE 7.1
Built-In Exceptions
Class Name Reasons for Having Exceptions
ValueError Pass function an argument with correct type object but with an inappropriate value
Check the exception type and review reasons causing the exceptions (Table
7.1).
Look into the code, especially the line (indicated in the exception message)
that throws errors, and analyze what resulted in the exception. Sometimes,
you may need to go up/down a few lines to identify the real problem.
If still not sure about the causes, use print to output the values for relative
variables to check if they are right.
Revise code and run again.
FIGURE 7.7
Raise and catch exceptions.
You can catch each exception using a different except block (Figure 7.7a) or
catch multiple exceptions within one except block (Figure 7.7b). You can also
detect all exceptions without any exception type after except (Figure 7.7c).
Although the first approach requires more coding, it is recommended because it
provides the most detailed exception messages for debugging. The last approach
is not recommended because it does not provide potential causes for the
exceptions.
The tryfinally statement is used to define clean-up actions that must be
executed under all circumstances (Code 7.5 and Code 7.6). This means that the
finally statements are executed regardless of whether or not the exception occurs.
CODE 7.5
Uses tryexceptfinally to capture the ZeroDivisionError exception and remove result from memory if an
exception happened.
CODE 7.6
Tryexceptfinally to clean up variables if an exception occurred.
CODE 7.8
Add the path to the module through sys.path.append() method will add the path to the end of sys.path list.
To make the path configuration persistent, change the systems Environment
Variable in the following steps:
1. Right click on My computer and select Properties.
2. Click Advanced tab on the top; Click Environment Variables on the
bottom.
3. Click New in either window.
4. Add the PYTHONPATH variable, and set the value as c:\pythoncode.
5. Save and quit.
6. Restart your Python interactive GUI.
7. Check if your path can be found with sys.path by importing sys module.
Another method of setting up the module file path is to specify it at the beginning
of the Python program as follows:
import sys, os
if os.getcwd() not in sys.path:
sys.path.append(os.getcwd)
The first statement imports sys and os modules. The second statement fetches the
current file path using os.getcwd to check if it is already in the system path, adding
to the system path if not.
TABLE 7.2
System Built-In Modules
Module Description Examples
os Interact with the operating system os.system(dir)
sys System-specific parameters and functions sys.path, sys.exit()
math Floating point math functions math.pow()
shutil High-level file operations shutil.copyfile(),
shutil.move()
glob Get file lists from directory wildcard searches glob.glob(*.shp)
re Regular expression tools for advanced string re.match(c, abcdef)
processing
datetime Manipulate dates and times date.today()
zlib, gzip, bz2, zipfile and Data archiving and compression ZipFile(spam .zip, w)
tarfile
math (Figure 7.9) is the most popular module used in this book.
FIGURE 7.9
math methods.
We can always use built-in function dir() to find what is supported in a new
module. Figure 7.10 shows that the math module includes many methods, such as
acos, sin, fabs, etc., and several private attributes, such as __doc__,
__name__, and __package__. These three attributes are typically included in
all modules. The built-in function help() can be used to check the description of
the module and functions.
FIGURE 7.10
Check a module.
FIGURE 7.11
ArcMap user interface.
FIGURE 7.12
Map data organization hierarchy.
Putting these together creates a package for reading and displaying ESRI
shapefiles, including simple functions (e.g., calculate the distance and centroid).
As shown in Figure 7.14, Tkinter is a built-in module for data visualization in a
GUI, and struct is used for reading binary data. A feature class is a simple module
including only two methods, __init__() and vis() (Figure 7.14). Polyline and Point
classes are inherited from the Feature class and include two methods, __init__()
and vis(), as well. In addition to these two methods, Polylines also include a
method length(). A polygon is inherited from the Polyline class and overrides the
vis() function.
FIGURE 7.14
Hierarchy of primary Mini-GIS modules (Tkinter and struct are Python built-in modules).
In the folder, *.py files are the Python files and *.pyc files are the compiled
python files. The sample-data folder contains several shapefile data folders; the
cities folder, which contains world city data; countries, which contains world
country data; the Fairfax folder contains schools, highways, major utility lines,
and regions in Fairfax; and the multi-parts-polyline folder contains two
polylines with multiparts.
FIGURE 7.16
Primary functions are feature modules and map modules.
FIGURE 7.17
(a) Using Mini-GIS to import Fairfax shapefile folder. (b) Zoom to extent. (c) Python Shell outputs.
4. View the map using the menu under View menu: zoom in, zoom out,
zoom to extent, zoom to full window and close layer. Figure 7.17b shows
zooming the map to the left boundary. Figure 7.17c shows the outputs in
the Python Shell, which also outputs the map ratio.
FIGURE 7.18
Draw features.
The Mini-GIS can also calculate the intersections between two polyline layers.
Figure 7.19 indicates the intersections between utility layers and highway layers
with yellow circles.
FIGURE 7.19
Intersections between HIGHWAY and UTILITY_LINES layers.
PROBLEMS
1. Take the data used in Chapter 4.
2. Based on the solution to problems in Chapter 4, how can you find the
bounding box (minx, miny, maxx, maxy) of the polylines?
3. Prepare the data to visualize (coordinates transform, etc.) based on the
window size you want to draw.
4. Program to draw the data (using Canvas and related
objects/functions/widgets). Organize your program in a file or several
files to set up a module in the system; import the module and execute the
module that was developed and configured.
5. Develop a word document explaining the program.
6. Provide comments for all lines in the code to explain the logic.
8
GIS is different from other software technologies due to its algorithms and
analyses for spatial relationships in spatial data. This chapter introduces a similar
category of algorithms for vector data processing, including the calculations of
centroid (8.1), area (8.2), length (8.3), line intersection (8.4), and point in polygon
(8.5), which are the fundamental algorithms based on geometry and the spatial
relationship.
8.1 Centroid
Centroid can be considered as the gravity center of a feature (wiki.gis 2011). One
application example of centroid is to set up the annotation location, such as
labeling a building on a map. This section discusses the centroid calculation for
three geometry types: triangle, rectangle, and polygon.
FIGURE 8.2
A rectangles centroid is the intersection of two diagonals.
8.2 Area
This section introduces how to calculate the area of polygon feature. Two types of
polygons are discussed: a simple polygon and a polygon with hole(s).
FIGURE 8.3
Area calculation of a simple polygon.
To calculate the area of this polygon, we first draw vertical lines from each
point of the polygon to the x-axis to form five trapezoids: A and B (Figure 8.3b),
and C, D, and E (Figure 8.3c). By visually examining the five trapezoids, the area
of the polygon (SP ) can be derived from Equation 8.4.
where SA, SB, SC, SD, and SE denote the corresponding areas of the five
trapezoids.
Since the coordinates for each point are known, we can calculate the area for
each trapezoid with Equation 8.5.
By plugging Equation 8.5 into Equation 8.4, we derive Equation 8.6 to calculate
the area of the polygon.
By generalizing Equation 8.6, we have Equation 8.7 to calculate the area for any
simple polygon with n points (point0 = pointn).
FIGURE 8.4
A polygon with two holes.
8.3 Length
Length calculation finds the length of a line feature and the perimeter of a polygon
feature.
If the side lengths of the two right angles a and b are known, the length of the
third side, which is the length of the line segment P1P2, can be derived. Since a
and b are parallel to the axis, we have a = x2 x1, b = y2 y1. Plugging these two
equations to c2 = a2 + b2, Equation 8.8 is derived to calculate the length of a line
segment:
FIGURE 8.7
Illustration of line segments intersection.
A common method to represent a line in the Cartesian coordinate system is to use
the linear equation: y = ax + b, where x, y are the variables, a, b are the constants
or parameters, a is the slope of the line, and b is the intercept of the line with y-
axis (Figure 8.8).
FIGURE 8.8
Mathematical representation of a line in Cartesian coordinate system.
Based on this, Line1 can be represented as y = a12x + b12; and Line2 can be
represented as y = a34x + b34, where a12, b12, a34, b34 are constant values that can
be calculated based on the four points (x1,y1), (x2,y2), (x3,y3), and (x4,y4) as
detailed below.
Calculating a12, b12, a34, b34: Since the two points (x1,y1), (x2,y2) are on Line1,
we have the following equation group (Equation 8.10):
where a34 is not equal to a12, and a12, b12, a34, b34 can be calculated based on the
given four points. It should be noted that (x0,y0) is the solution for two infinite
lines. To check whether (x0,y0) falls on both Line1 ((x1,y1), (x2,y2)) and Line2
((x3,y3), (x4,y4)), the following test is required:
If the four conditions are all true, Line1 and Line2 intersect at point (x0,y0).
Otherwise, they do not intersect.
The above method for checking the intersection does not work for two special
scenarios: parallel lines and vertical lines.
Once (x0,y0) is calculated, the same test is required to check whether the
intersection point falls on both line segments. The same method can be applied if
Line2 ((x3,y3), (x4,y4)) is vertical.
If both lines are vertical, they are parallel. This can be handled the same way as
parallel lines.
Since a polyline consists of two or more line segments (Figure 8.10), the
following procedure can be used to check whether the two polylines intersect: for
each line segment in polyline1, check whether it intersects with any of the line
segments in polyline2. Once a single intersection point is found, we can conclude
that the two polylines intersect. We may also find out all the intersection points of
two polylines by checking each of the line segment pairs between the two
polylines.
FIGURE 8.10
Intersection checking of two polylines.
TABLE 8.1
Number of Intersection Points for the Seven Rays
Point Id Number of Intersection Points Point in Polygon?
1 0 No
2 2 No
3 1 (odd) Yes
4 4 No
5 5 (odd) Yes
6 2 No
7 6 No
FIGURE 8.12
Special scenarios: ray passes the polygon vertex.
Examining carefully, the difference is that, for point1, the two edges sharing the
vertex are on different sides of the ray, while for point three, the two edges are on
the same side (left) of the ray. A general pattern is that if the edges are on different
sides, we consider it as one intersection point, but if the edges are the same side,
then we consider it as two intersection points. Based on this pattern, both edges of
point2 are on the right side of the ray as it passes through the vertex (i.e., two
intersections); adding the unambiguous intersection at the top of the polygon
results in a total of three intersection points, and we can correctly conclude that it
falls inside the polygon.
FIGURE 8.13
Centroid Practice window.
PROBLEMS
Review the class material.
Review the class practice code: chap8_intersection_pract.py
Create the following four line segments: [(50,200),(400,200)],[(60,450),
(400,450)],[(100,600),(350,250)],[(300,100),(300,400)]
Write a program to determine whether they intersect with each other.
Display the line segments on the monitor and draw the intersected point in a
different color and size.
Section III
ArcGIS Programming
Programming all GIS analysis or mapping functions from scratch is not feasible
because of their complexities; however, many commercial and open-source
packages, such as ArcGIS, support such comprehensive GIS functions. Integrating
various functions from existing packages will satisfy analytical requirements of an
application. ArcGIS supports such workflow implementation through Python
programming, which has become a sought-after capability for GIS professionals.
This chapter introduces programming in ArcGIS for Desktop (i.e., ArcMap) using
Python. The ArcGIS knowledge introduced in this chapter will set the foundation
for later chapters when programming for algorithms and analyses.
FIGURE 9.1
Geoprocessing with (a) ArcToolbox, ModelBuilder, and (b) ArcMap Python window.
NOTE: We use ArcPy in this text to be consistent with the Esri naming
convention; however, in programming, arcpy (all lowercase) will be used.
Hands-On Practice 9.1
Open Python window from ArcMap and type in (not copy) the following
command:
While typing, observe the change in the interactive help document on the
right panel of the window.
FIGURE 9.3
Sample code of using ArcPy outside ArcMap.
To improve the flexibility of the developed scripts for end users, configure the
input as dynamic value, and allow the program to automatically retrieve the
input. The sys.argv can be used for this purpose with a list of strings that hold the
script name and additional arguments that are passed to the script.
Hands-On Practice 9.2: Fetching System Parameter Input
1. Open the Python editor window and enter Code 9.1. Save the code to a .py
file, for example, samplecode.py.
CODE 9.1
Python code using ArcPy and allowing flexible parameter inputs.
2. Open the windows command console, navigate to the folder where the
*.py file was saved, and run the script to perform buffer analysis with the
following command:
samplecode.py C:\Default.gdb\out2 C:\Default.gdb\output 5000
Meters
The first parameter is the absolute path of the input data, the second parameter is
the absolute path of the output data, and the third describes the buffer size (Figure
9.4).
FIGURE 9.4
Screenshot of the windows command to execute a .py file and the output. (If there is no error reported in the
window, and the output is generated, then the execution is successful.)
FIGURE 9.5
Desktop and online help document.
FIGURE 9.6
Syntax and sample code of Python script in the online help document for the corresponding geoprocessing tool.
For novice users, one way to learn ArcPy is to go through one of the three help
documents, try the sample code, and make changes to the code using customized
data. The Python prompt window inside ArcMap embeds the interactive help on
each ArcPy function and class.
where the first three parameters are required and the last parameter with braces is
optional. Scripting, rather than operating the tool through the ArcToolBox
interface, is very useful when the process involves a loop (for/while statement)
or conditional expression (if statement) to execute the geoprocessing function
repeatedly under certain conditions. For example, to calculate the line length
within each polygon in a data layer, the process needs to include a for loop to
enable the repeated process for each polygon. Inside the for loop, there will be a
clip function that can cut off the line inside that polygon and a statistics function,
which sums up the total length of the lines within the polygon.
Hands-On Practice 9.4: Calculate the Road Length within an Area
1. Type Code 9.2 in the Python window in ArcMap. Change the paths
according to your workspace.
CODE 9.2
Automate calculating the road length within each polygon.
arcpy.MakeFeatureLayer_management("I:\\sampledata\\data.gdb\\bearMove",
"inferLy")
2. Open the output tables, and list the total lengths of roads in each polygon
(Figure 9.7)
FIGURE 9.7
Example of output summary table generated by Code 9.2.
9.4.1 SearchCursor
The SearchCursor function establishes a read-only cursor on a feature class, such
as a shapefile or a table. The SearchCursor can be used to iterate through row
objects and extract field values. The syntax of SearchCursor is as follows:
arcpy.da.searchCursor (in_table, field_names, {where_clause},
{spatial_reference}, {explode_to_points}, {sql_clause})
The first argument (input feature table) and second argument (the queried fields)
are required while others are optional (e.g., limited by a where clause or by field,
and optionally sorted).
Hands-On Practice 9.5: Get Attributes of Each Feature of a Shapefile Using the
SearchCursor
1. Open a Python window in ArcMap and type Code 9.3.
CODE 9.3
SearchCursor with for statement.
2. Check the results.txt file. What is included in the file? How many lines
you can find in the file?
3. Search cursors also support the with statement. Using a with statement
will guarantee that the iterator is closed and the database lock is released.
By applying the with statement, the above code can be changed to Code
9.4.
CODE 9.4
SearchCursor using with statement.
4. A where clause may be used to limit the records returned by the cursor.
Run Code 9.5 and check the result again. How many lines are included in
the result file?
CODE 9.5
SearchCursor with where clause ("FID < 10").
5. SearchCursor can also access the feature geometry. Run Code 9.6 and
Code 9.7, and then check the result again:
CODE 9.6
Accessing geometry using SearchCursor example 1.
CODE 9.7
Accessing geometry using SearchCursor example 2.
9.4.2 UpdateCursor
The UpdateCursor object can be used to update or delete specific rows in a
feature class, shapefile, or table. The syntax of UpdateCursor is similar to that of
SearchCursor:
Hands-On Practice 9.6: Update the Attributes of Each Feature for a Shapefile
Using the UpdateCursor
1. Check the attribute value of Shape_Leng in the data
Partial_Streets.shp.
2. Open a Python window in ArcMap and type Code 9.8 in the editor.
CODE 9.8
UpdateCursor example.
9.4.3 InsertCursor
InsertCursor is used to insert new records into a feature class, shapefile, or table.
The InsertCursor returns an enumeration object, that is, a row in a table.
Hands-On Practice 9.7: Inserts Rows into a Shapefile Using the InsertCursor
1. Open the attribute table of school.shp and count the number of records.
2. Open a Python window in ArcMap and type Code 9.10.
CODE 9.10
Insert Cursor example.
9.4.4 NumPy
As a new feature in ArcMap 10.1+, data access modules offer functions to enable
the transformation between data array and feature classes or tables. Since NumPy
library has powerful capabilities for handling arrays, the related function of
ArcPy is developed based on NumPy. With arcpy.NumPyArrayToFeatureClass,
arcpy.NumPyArrayToTable, and arcpy.TableToNumPyArray functions, users can
quickly transform values that are organized in array into a feature class or table,
and vice versa.
Without using the NumPy function in the data access module, include many more
steps to create a point feature class so the performance is much lower (Code
9.11).
CODE 9.11
Add multiple new records to feature class using InsertCursor.
In contrast, creating the feature class with NumPy requires only one step. The
arcpy.da.NumPyArrayToFeatureClass function actually creates a feature class and
inserts two records inside.
Hands-On Practice 9.8: Create a Feature Class Using
NumPyArrayToFeatureClass
1. Open the Python window in ArcMap, and run Code 9.12 to create a
feature class using the NumPy functions in the data access module.
CODE 9.12
Add multiple new records to feature class using NumPyArrayToFeatureClass.
2. Also run Code 9.11 in the Python window in ArcMap, and compare the
execution time spent of with and without using numpy: the
arcpy.da.NumPyArrayToFeatureClass has a much better performance.
1. Run Code 9.13 in ArcMap Python window and check the output. What is
the shape type of the input feature class?
CODE 9.13
Describe function example.
2. Replace the Describe parameter with another shapefile that has different
geometric types and see how the results change when running it again.
9.5.2 List
ArcPy provides functions to list all data under a particular workspace or list
corresponding information in data. ListFields are frequently used functions in
ArcPy to list all the fields and associated properties of a feature class, shapefile,
or table. Code 9.14 is an example of the ListFields function. The code controls
operations to be conducted on specific fields only: those that are in Double type
or that include the name Flag.
CODE 9.14
ListFields example.
CODE 9.15
List all polyline feature class and conduct buffer analysis on them.
3. Change the code and implement buffer analysis for every point feature
class within 10 miles.
The Walk function in the data access module can help list all data under the
workspace or under its sub-workspace in hierarchy. For example, there is a
shapefile, a .png file, a geodatabase Default.gdb, and a folder temp under a
specific workspace sampledata. This function will list all the files under
sampledata, the feature classes under Default.gdb, and the files under temp.
The Walk function is much faster than traditional List functions. Code 9.16 is a
sample code of the Walk function and Figure 9.8 shows its results.
CODE 9.16
List files using arcpy.da.Walk function.
FIGURE 9.8
Results of arcpy.da.Walk function.
Another popular complex object, field, describes the field in the attribute table
of a feature class, shapefile, and table (Code 9.17).
CODE 9.17
Accessing the properties of a field object.
Meanwhile, ArcPy also provides the classes for manipulating geometry objects.
Geometry information is usually stored in feature classes, feature layers, or
datasets. Accessing geometry directly is not common when using geoprocessing
tool for analysis. Sometimes, however, only locational information, such as
creating sample points and making a buffer distance, is needed. Using geometry
objects can save the time on creating feature classes that really do not persist.
ArcPy offers several types of geometric objects:
Point: a single point, which is the basic unit for all the other geometry types,
cannot be directly used as input in geoprocessing functions
Geometry: a general type
Multipoint: a geometry entity with multiple points, consisting of Point(s)
PointGeometry: a geometry entity with a single point, consisting of Point
Polyline: a line geometry entity, consisting of Point(s)
Polygon: a polygon geometry entity, consisting of Point(s)
CODE 9.18
Example of using geometry object to coduct buffer analysis.
CODE 9.19
Create SpatialReference object.
CODE 9.21
Create a feature class with a spatial reference.
CODE 9.22
Create a feature class with the spatial reference from another data.
Code 9.23 represents the process of making a map for a feature class using the
predefined layer style. The process includes the opening of files and layers,
changing layer style, and revising the location and content of map elements. The
code is generalized from the script of an ArcTool named CCMap Generator
(Teampython 2013). The script is open and can be downloaded from the Analysis
and Geoprocessing community through the ArcGIS resource center website
http://resources.arcgis.com/en/communities/analysis/.
CODE 9.23
Making maps with predefined symbol style automatically.
The steps to add scripts as a tool in the ArcToolBox are shown in Figure 9.11.
FIGURE 9.11
General steps to add scripts as tools.
1. Before creating the ArcTool, copy Code 9.24 and save as a *.py file.
CODE 9.24
A script with GetParameter functions to obtain input from ArcTool interface.
3. Add Toolset by right click, and select New -> Toolset (Figure 9.13).
Change the name of toolset, such as Buffer.
FIGURE 9.13
Add ArcToolset.
4. Then, right click Buffer, and select Add -> Script (Figure 9.14).
FIGURE 9.14
Add Python script as ArcTool.
Specify (1) basic info for the tool, such as name, label, descriptions,
etc., (2) the script file path, and (3) input and output parameters, including
types and names, etc. Click Finish to complete creating your customized
ArcTool (Figure 9.15).
FIGURE 9.15
Configure the property of the tool, the path of script, and the input parameter.
Hands-On Practice 9.13: Add Message into the Custom Script Tool
1. Make a Python script with Code 9.25 and then add as an ArcTool.
CODE 9.25
AddMessage examples.
2. Use the tool to test the buffer analysis and check the output.
Find the data states.shp, and run Code 9.26 in the ArcMap Python
window. Note that new fields must be added into the attribute table before
the calculation in order to record the results (Figure 9.17).
CODE 9.26
Calculate the centroid, perimeter, and area of polygons using arcpy.
FIGURE 9.17
Result of Code 9.26.
1. Find the two shapefiles interstates and railway in the disk, and select
the interstate roads that intersect with railways. Selection will be
conducted on the interstate features based on their spatial relationship
with railway layer. Run Code 9.27 in ArcMap python window and
Figure 9.18 shows the result.
CODE 9.27
Calculate line intersection.
FIGURE 9.18
Line intersection result.
2. Select the railway stations (in amtk_sta.shp) in Virginia. Run Code 9.28
in ArcMap python window and see the result (Figure 9.19).
CODE 9.28
Select all railway stations in Virginia.
FIGURE 9.19
Point in polygon result.
9.12 Summary
This chapter introduces programming within ArcGIS using Python scripts and
ArcPy package. This chapter introduces
Esri geoprocessing framework and ArcPy.
The capability and syntax of ArcPy functions, classes, and modules.
How to create simple or complex analysis workflows with ArcPy?
How to manipulate vector data or objects through ArcPy?
How to create ArcTools from the python scripts?
How to implement spatial calculations using ArcGIS?
9.13 Assignment
Read the ArcPy section in ArcGIS desktop help or the online version.
Find a road data or download data from the package of corresponding
course material.
Select highways from road data.
Generate a buffer with 300 meters as the radius for the highway.
Output the buffer as a transportation pollution zone.
Add a field with the name of buildings with Long type in the buffer zone
data.
Count the number of buildings within each buffer zone and store into the
new field.
Write a report to explain how you conducted the analysis and programming.
Compare the differences of implementing spatial calculations using ArcGIS
and pure Python.
NOTE: All codes can be successfully executed on ArcGIS for desktop versions
10.2.2 through 10.3. There may be problems on running the code on more recent
version of ArcGIS.
10
FIGURE 10.1
Raster data structure.
FIGURE 10.2
Raster image. (a) Each pixel of the raster is color coded and (b) value of each pixel and order of pixel storage.
Nowadays, data are increasingly available and has higher resolution. Computing
capabilities have also improved. Therefore, better methods of data storage and
compression are needed. Raster compression reduces the amount of disk space
consumed by the data file, while retaining the maximum data quality. There are
different methods for raster data compression, including Run Length Coding, quad
tree coding, and others.
For multilayer raster datasets, the normal practice is to store the layers
separately. It is also possible to store all information for each pixel together;
however, this requires extra space to be allocated initially within each pixels
storage location for layers, which can be created later during analysis.
FIGURE 10.3
Example of Run Length Coding.
Run Length Coding has its limitations. For example, Run Length will not save
storage in cases where pixel values do not repeat frequently. In some cases, such
as DEM data in a mountainous area, neighboring pixels always have different
values, and Run Length Coding may actually increase the length of the initial
storage. However, Run Length Coding is very successful when dealing with black
and white images, such as a fax. In this case, it is relatively efficient because most
faxed documents are predominantly white space, with only occasional
interruptions of black.
FIGURE 10.4
Quad tree process. (a) Pixel value of the rater, (b) search order of the four quadrants, (c) continuing dividing
when finding non-equal values inside each quadrant of (b), and (d) final division.
Quad tree works for images with identical-value patterns. For each quad
division used, four more storage elements are needed. Quad trees are of great
interest for indexing spatial data, whereby cells that are adjacent in space are
more likely to have similar spatial index addresses than in column or row
ordering schema. Hence, data that are close together are also close in the storage
system. This feature makes quad tree compressed data much easier and quicker to
manipulate and access.
10.3.1 TIFF
TIFF (Tagged Image File Format) is an image format recognized by many
computer systems. The TIFF imagery file format is used to store and transfer
digital satellite imagery, scanned aerial photos, elevation models, scanned maps,
or the results of many types of geographic analysis. TIFF supports various
compression and tiling options to increase the efficiency of image transfer and
utilization. The data inside TIFF files are categorized as lossless compressed or
lossy compressed.
10.3.2 GeoTIFF
GeoTIFF are TIFF files that have geographic (or cartographic) data embedded as
tags within the TIFF file (Ritter and Ruth 1997). The geographic data can then be
used to position the image in the correct location and geometry on the screen of a
geographic information display. The potential additional information includes map
projection, coordinate systems, ellipsoids, datums, and everything else necessary
to establish the exact spatial reference for the file. Any Geographic Information
System (GIS), Computer Aided Design (CAD), Image Processing, Desktop
Mapping, or other type of systems using geographic images can read GeoTIFF
files created on any system following the GeoTIFF specification.
10.3.3 IMG*
IMG files are produced using the IMAGINE image processing software created
by ERDAS. IMG files can store both continuous and discrete, single-band and
multiband data. These files use the ERDAS IMAGINE Hierarchical File Format
(HFA) structure. An IMG file stores basic information including file information,
ground control points, sensor information, and raster layers. Each raster layer in
the image file contains information in addition to its data values. Information
contained in the raster layer includes layer information, compression, attributes,
and statistics. An IMG file can be compressed when imported into ERDAS
IMAGINE, which normally uses the run length compression method (described in
Section 10.2.1).
10.3.4 NetCDF
NetCDF (Network Common Data Form) is a set of software libraries and self-
describing, machine-independent data formats that support the creation, access,
and sharing of array-oriented scientific data (Rew and Davis 1990). It is
commonly used in climatology, meteorology, and oceanography applications (e.g.,
weather forecasting, climate change) and GIS applications. It is an input/output
format for many GIS applications, as well as for general scientific data exchange.
NetCDF is stored in binary in open format with optional compression.
10.3.5 BMP
BMP (Windows Bitmap) supports graphic files inside the Microsoft Windows
Operational System. Typically, BMP files data are not compressed, which can
result in overly large files. The main advantages of this format are its simplicity
and broad acceptance.
10.3.6 SVG
Scalable Vector Graphics (SVG) are XML-based files formatted for 2D vector
graphics. It utilizes a lossless data compression algorithm, and typically reduces
data to 20%50% of the original size.
10.3.7 JPEG
JPEG (Joint Photographic Experts Group) files store data in a format with loss
compression (in major cases). Almost all digital cameras can save images in
JPEG format, which supports eight bits per color for a total of 24 bits, usually
producing small files. When the used compression is not high, the quality of the
image is not as affected, however, JPEG files can suffer from noticeable
degradations when edited and saved recurrently. For digital photos that need
repeated editing or when small artifacts are unacceptable, lossless formats other
than JPEG should be used. This format is also used as the compression algorithm
for many PDF files that include images.
10.3.8 GIF
GIF (Graphic Interchange Format) is the first image format used on the World
Wide Web. This format is limited to an 8-bit palette, or 256 colors. It utilizes
lossless LempelZivWelch (LZW) compression, which is based on patented
compression technology.
10.3.9 PNG
PNG (Portable Network Graphic) is an open-source successor to GIF. In contrast
to the 256 colors supported by GIF, this format supports true color (16 million
colors). PNG outperforms other formats when large uniformly colored areas form
an image. The lossless PNG format is more appropriate for the edition of figures
and the lossy formats, as JPEG, are better for final distribution of photos, because
JPEG files are smaller than PNG files.
FIGURE 10.5
Pixel value to grayscale mapping.
Raster data can also be transformed from RGB model to grayscale. The most
common ways to conduct the transformation are Luster (Jack 2011), Intensity
(Hoeffding 1963), and Luma (Bosch et al. 2007). The Luster method averages the
most prominent and least prominent colors. The Intensity method simply averages
the values. The Luma method is a more sophisticated version of the average
method. It also averages the values, but it forms a weighted average to account for
human perception; because human beings are more sensitive to green than other
colors, green is weighted most heavily (Equation 10.1). Figure 10.7 shows the
transformation results.
FIGURE 10.7
RGB to grayscale. (Original image from http://www.coloringpages456.com/color-pictures/.)
The color depth measures the amount of color information available to display
or print each pixel of a digital image. Owing to the finite nature of storage
capacity, a digital number is stored with a finite number of bits (binary digits).
The number of bits determines the radiometric resolution of the image. A high
color depth leads to more available colors, and consequently to a more accurate
color representation. For example, a pixel with one bit depth has only two
possible colors. A pixel with 8 bits depth has 256 possible color values, ranging
from 0 to 255 (i.e., 281), and a pixel with 24 bits depth has more than 16 million
of possible color values, ranging from 0 to 16,777,215 (i.e., 2241). Usually, the
color depths vary between 1 and 64 bits per pixel in digital images.
where v refers to the stretched pixel value and v refers to the original pixel value.
This may result in a crisper image, and some features may become easier to
distinguish (Figure 10.8c).
Different stretches will produce different results in the raster display; standard
methods include Standard Deviation, MinimumMaximum, Histogram Equalize,
and Histogram Specification.
The RGB Composite renderer uses the same methods as the Stretched renderer,
but allows combining bands as composites of red, green, and blue. Multiband
raster datasets, such as satellite or aerial imagery, are usually displayed in
different combinations of bands using RGB Composite.
The Classified renderer is used with a single-band raster layer. The classified
method displays thematic raster by grouping cell values into classes. Continuous
phenomena such as slope, distance, and suitability can be classified into a small
number of classes which can be assigned as colors. Classification is a spatial data
operation used in conjunction with previous selection operations. This can either
create a new dataset or present a different view of the same data (e.g., display
properties).
Two less common renderers are Unique Values and Discrete Color. The Unique
Values renderer is used to display each value in the raster layer individually. It is
suitable for data containing discrete categories representing particular objects,
such as a thematic map representing land use or types. The Unique Values renderer
can display each value as a random color or use a color map. Discrete Color
renderer displays the values in the raster dataset using a random color. This
renderer is similar to the Unique Values renderer, but is more efficient when there
are a large number of unique values because it does not have to calculate how
many unique values exist. The Discrete Color renderer assigns a color to each
unique value until it reaches the maximum number of colors chosen. The next
unique value starts at the beginning of the color scheme; this process continues
until each unique value has a color assigned to it.
FIGURE 10.9
Reclassification of categorical data involves replacing individual values with new values. For example, land use
values can be reclassified into preference values of low (1), medium (2), and high (3).
FIGURE 10.10
Reclassification of continuous data involves replacing a range of values with new values. For example, a raster
depicting distance from roads can be reclassified into three distance zones.
2. Change the color render mode. As shown in Figure 10.12a, export the land
cover data. In the Export Raster Data window (Figure 10.12b), select
Use Renderer, which allows the output raster using the same color
schema, and select Force RGB, which will transfer the single band
raster into the new Raster storing the pixel values in RGB mode.
FIGURE 10.12
Steps of export raster data in ArcMap. (a) Export the land cover data and (b) Export Raster Data window.
When the raster is stored in RGB mode, we will see three sublayers in the
Table of Contents (Figure 10.13) and under the Symbology tab in the Layer
Properties window, there is an RGB Composite renderer choice, but Unique
Value, Classified, and Discrete Color are no longer available.
FIGURE 10.13
Raster displayed in RGB.
Note that the Reclassify function requires that the ArcMap users have the
Spatial Analyst extension; therefore, check the license of the extension
before executing the analysis.
The Reclassify function provides two methods for defining the classes:
RemapRange redefines a range of values into a new class value;
RemapValue defines the one-to-one mapping relationship, that is, replacing
a single original value with a single new value.
2. Land cover dataset stores the land cover types in detail. For example,
Forest is divided into three subtypes: Deciduous Forest, Evergreen
Forest, and Mixed Forest. Run Code 10.2 in the ArcMap python
window to generalize the land cover dataset. The result is shown in
Figure 10.15.
CODE 10.2
Generalize the land cover dataset.
FIGURE 10.15
Result of reclassifying land cover type data in Code 10.2.
3. Run Code 10.3 in ArcMap python window to overlay the classified DEM
and land cover datasets to find the area in forest (land cover dataset pixel
value = 4) with an elevation between 60 and 100 (DEM dataset pixel
value = 100). For this specific dataset, one way to find the expected area
is to add the two layers and find the pixels with the value in 104, then
reclassify all pixels with the value in 104 into one class (1), and all other
pixels into another class (0). Figure 10.16 shows the result of
reclassification.
CODE 10.3
Reclassify and find specific areas.
FIGURE 10.16
Result of reclassifying land cover type data in Code 10.3.
CODE 10.4
Calculate area.
2. Using the classified land cover dataset that resulted from the previous
practice, run Code 10.5 in the ArcMap Python window to calculate the
area of each land cover type. The area of each land cover type can be first
calculated using the proportion of the pixel counts of this type to the total
pixel count, and then multiply the total area of the raster dataset.
Accordingly, use the SearchCursor (see Chapter 8) to capture the counts
of pixels.
CODE 10.5
Calculate total area.
NOTE: All codes can be successfully executed on ArcGIS for desktop versions
10.2.2 to 10.3. There may be problem on running the code on more recent version
of ArcGIS.
V = {v1,v2,v3,v4}
E = {(v1,v2), (v2,v4), (v1,v3), (v1,v4), (v3,v4)}
FIGURE 11.2
An example of basic network representation.
FIGURE 11.3
Example of directed and undirected network. (a) Directed network, (b) Undirected network.
FIGURE 11.4
Adjacency matrix of directed and undirected network. (a) Directed network, (b) Undirected network.
TABLE 11.1
Example of Node Table for the Network in Figure 11.2
ID X Y
v1 23.2643 75.1245
v2 23.1443 74.1242
v3 23.2823 75.1315
v4 23.1442 75.1286
TABLE 11.2
Example of Link Table for the Network in Figure 11.2
ID Origin Destination One-Way
e1 v1 v2 Not
e2 v2 v4 Not
e3 v1 v3 Not
e4 v1 v4 Not
e5 v3 v4 Not
Node table: This table contains at least three fields: one to store a unique
identifier and the others to store the nodes X and Y coordinates. Although
these coordinates can be defined by any Cartesian reference system,
longitudes and latitudes ensure an easy portability to a GIS (Rodrigue,
2016).
Links table: This table also contains at least three fields: one to store a
unique identifier, one to store the node of origin, and one to store the node
of destination. A fourth field can be used to state whether the link is
unidirectional or not.
Step 1: Find all possible paths from the start point to the end point.
Step 2: Calculate the length of each path.
Step 3: Choose the shortest path by comparing the lengths of all different
paths.
For example, given the network in Figure 11.5, we would like to find the shortest
path from A to all the other vertices. The number of each edge is the cost and
Table 11.3 shows all the possible paths from A to the other vertices. Although this
method of finding the shortest path is simple and straightforward, the complexity
of this approach increases exponentially with the number of vertices and edges.
For example, if we connect B and C, there will be at least two more routes from A
to E. In a real network application, we usually have a large number of both
vertices and edges (e.g., a transportation system), which would be very expensive
and time-consuming from a computational standpoint. Therefore, a
computationally efficient algorithm to calculate the shortest path is needed.
FIGURE 11.5
A network example used to illustrate finding shortest path problem.
TABLE 11.3
Brute Force Approach to Solving the Shortest Path Problem (Find the Shortest Path from A to E)
Destination Point Possible Paths and Length Shortest Path
B AB: 1; ACDB: 6 AB: 1
C AC: 2; ABDC: 5 AC: 2
D ABD: 3; ACD: 4 ABD: 2
E ABDE: 4; ACDE: 5 ABDE: 4
Assign to every node a tentative distance value: set it to zero for the initial
node and to infinity for all other nodes.
Set the initial node as current. Mark all other nodes unvisited. Create a set
of all the unvisited nodes called the unvisited set.
For the current node, consider all of its unvisited neighbors and calculate
their tentative distances. Compare the newly calculated tentative distance
to the current assigned value and assign the smaller one. For example, if the
current node A is marked with a distance of 6, and the edge connecting it
with a neighbor B has length 2, then the distance to B (through A) will be 6
+ 2 = 8. If B was previously marked with a distance greater than 8, then
change it to 8. Otherwise, keep the current value.
After considering all of the neighbors of the current node, mark the current
node as visited and remove it from the unvisited set. A visited node will
never be checked again.
If the destination node has been marked visited (when planning a route
between two specific nodes) or if the smallest tentative distance among the
nodes in the unvisited set is infinity (when planning a complete traversal;
occurs when there is no connection between the initial node and remaining
unvisited nodes), then stop. The algorithm has finished.
Otherwise, select the unvisited node that is marked with the smallest
tentative distance, set it as the new current node, and go back to step 3.
As an example, the problem mentioned in the last section can be solved by using
the Dijkstra algorithm (Table 11.4). The pseudo-code for the Dijkstra algorithm is
shown in Table 11.5.
TABLE 11.4
Process of Using Dijkstra Algorithm to Solve the Shortest Path from A to Other Vertices (CX Means the Cost
from A to X)
Step Selected Point CA CA CA CA CA Path M
1 A 0 A A
2 B 1 2 AB AB
3 C 2 3 AC ABC
4 D 3 ABD ABCD
5 E 1 2 3 4 ABDE ABCDE
TABLE 11.5
Pseudo-Code for the Dijkstra Algorithm
Input: Network dataset (G), starting vertex (A)
{
DK1: for each vertex v in G:
DK2: dist[v] =
DK3: dist[A] := 0
DK4: T = the set of all vertices in G
DK5: while T is not empty:
DK6: s = vertices in T with smallest dist[ ]
DK7: delete s from T
DK8: for each connected (neighbor) v of s:
DK9: temp_Distance = dist[s] + dist_between(s, v)
DK10: if temp_Distance < dist(v)
DK11: dist [v] = temp_Distance
DK12: shortest_Distance [v] = temp_Distance
DK13: return shortest_Distance []
}
11.3.1 Routing
Whether finding a simple route between two locations or one that visits several
locations, people usually try to take the best route. But the best route can mean
different things in different situations. The best route can be the quickest, shortest,
or most scenic route, depending on the impedance chosen. The best route can be
defined as the route that has the lowest impedance, where the impedance is chosen
by the user. If the impedance is time, then the best route is the quickest route. Any
valid network cost attribute can be used as the impedance when determining the
best route.
11.3.6 Location-Allocation
Location-allocation could help choose, given a set of facilities, from which
specific facilities to operate, based on their potential interaction with demand
points. The objective may be to minimize the overall distance between demand
points and facilities, maximize the number of demand points covered within a
certain distance of facilities, maximize an apportioned amount of demand that
decays with increasing distance from a facility, or maximize the amount of demand
captured in an environment of friendly and competing facilities.
Figure 11.10 shows the results of a location-allocation analysis meant to
determine which fire stations are redundant. The following information was
provided to the solver: an array of fire stations (facilities), street midpoints
(demand points), and a maximum allowable response time. The response time is
the time it takes firefighters to reach a given location. In this example, the
location-allocation solver determined that the fire department could close several
fire stations and still maintain a 3-minute response time.
FIGURE 11.10
Example of location-allocation.
CODE 11.1
Script to create a route layer from the network dataset.
The input parameter is the ND layer of the network dataset. Give a name
to your output route layer, such as myRoute. In this example, the impedance
attribute is Length. FIND_BEST_ORDER and PRESERVE_BOTH
mean that the order of the salesmans stops can be changed when analyzing
the shortest path (to approach an optimal result), but the first and end stops
are preserved as his fixed start and end locations. The total length of the
resulting path is calculated for reference (accumulate_attribute_name =
"Length"). Figure 11.11 shows the route data layer in ArcMap created by
Code 11.1.
FIGURE 11.11
Network dataset (left) and the route layer generated using the dataset (right).
2. Add the stops of the salesman in the stops.shp to the route layer. Code
11.2 is provided to obtain all the subclasses in the route layer structure.
The subclasses of a route layer include Barriers, PolygonBarriers,
PolylineBarriers, Stops, and Routes. Except the Routes class, all the
other classes are the input classes that allow users to input stops and
barriers to restrict the network analysis. The analysis output will be
automatically stored in the Routes class. Since we set a parameter
INPUT in the code below, the code will only return those input
subclasses (Figure 11.12).
CODE 11.2
Script to get all input sublayer in the route layer.
FIGURE 11.12
Returned result of Code 11.2: all input sublayer in the route layer.
Run Code 11.3 to input stops in the Stops subclass. The default value of
speed and length the stops is different from at the stops is set as 0, which
means the salesman will not have speed and have length cost at those stops.
The results of adding stops to each route layer are shown in Figure 11.13.
CODE 11.3
Script to add the salesmans stops to the Stops sublayer in the route layer.
FIGURE 11.13
Returned result of Code 11.3: the route layer with stops added in.
3. Run the Solve function (Code 11.4) to calculate the shortest path with
Length as impedance (i.e., constraining cost).
CODE 11.4
Script to execute shortest path algorithm in the route layer.
Figure 11.14 (upper) shows the resulting route. The order of the stops is
different from the ones in Figure 11.11, because the order has been changed
to optimize the analysis result. The total length of the route has been
accumulated and stored in the attribute table of the resulting route (Figure
11.14 lower).
FIGURE 11.14
Routing result (upper: the route line; lower: the attribute table of the route layer).
PROBLEMS
Review the class material and practice code, and develop a network for your
University Campus for routing:
NOTE: All codes can be successfully executed on ArcGIS for desktop versions
10.2.2 through 10.3. There may be problem on running the code on more recent
version of ArcGIS.
12
The elements needed to create a contour map include a base contour and a
contour interval from values for a specific feature. For example, we can create a
contour every 15 meters, starting at 10 meters. In this case, 10 meters would be
considered the base contour and the contour interval would be 15 meters; the
values to be contoured would be 10 m, 25 m, 40 m, 55 m, etc.
FIGURE 12.2
Grid surface model.
FIGURE 12.4
TIN triangles.
12.1.2.2.3 Comparison of Grid and TIN
TIN and Grid each has their advantages, so it would be arbitrary to credit one as
being better than the other. Some advantages and disadvantages of TINs are as
follows:
The TIN model has variable resolution. A TIN preserves the x, y location of
input points, allowing for more detail where there are extensive surface
variations and less detail where the changes are small or nonexistent.
Since TINs are vector data, they can be displayed well at all zoom levels.
Raster display degrades when you zoom in too close.
For large-scale applications (those covering a small area in detail) or
applications where display quality is very important, TINs are often a
better choice.
However, in many cases, TINs require visual inspection and manual control
of the network.
Different methods exist for calculating the grids elevation based on the weights
scheme and the number of nearest points used. Commonly used interpolation
methods include inverse distance weighting (IDW), spline, kriging, and natural
neighbors. IDW weights the points closer to the target cell more heavily than those
farther away. Spline fits a minimum curvature surface through the input points.
Kriging is a geostatistical interpolation technique in which the surrounding
measured values are weighted to derive a predicted value for an unmeasured
location. These weights are based on the distance between the measured points,
the prediction locations, and the overall spatial arrangement among the measured
points. Natural neighbors create a Delaunay triangulation of the input points,
selecting the closest nodes that form a convex hull around the interpolation point,
and then weighting their values proportional to their area. In ArcGIS, Grid surface
is phrased as Raster.
1. Pick sample points. In many cases, sample points must be selected from
control points, such as existing, dense Digital Elevation Model (DEM) or
digitized contours, to ensure accuracy of representation. There are several
existing algorithms for selecting from a DEM: the Fowler and Little
(1979) algorithm, the VIP (Very Important Points) algorithm (Chen and
Guevara 1987), and the Drop heuristic algorithm (Lee 1991). In essence,
the intent of these methods is to select points at significant breaks of the
surface.
2. Connect points into triangles. The selected TIN points will then become
the vertices of the triangle network. Triangles with angles close to 60
degrees are preferred since this ensures that any point on the surface is as
close as possible to a vertex. There are different methods of interpolation
to form the triangles, such as Delaunay triangulation or distance ordering.
Delaunay triangulation, the method most commonly used in practice,
ensures that no vertex lies within the interior of any of the circumcircles
of the triangles in the network (Figure 12.5). Delaunay triangulation is
accomplished either by starting from the edges of the convex hull and
working inward until the network is complete, or by connecting the
closest pair that must be a Delaunay edge, searching for a third point such
that no other point falls in the circle through them, and then working
outward from these edges for the next closest point.
FIGURE 12.5
Delaunay triangulation.
3. Model the surface within each triangle. Normally, the surface within
each triangle is modeled as a plane.
12.3.1 Elevation
Elevation of a certain point can be calculated based on the interpolation methods
introduced in creating the surface. In a Grid model, elevation of a certain point
can be calculated using the Grid cells close to the point. Taking IDW as an
example, a general form of finding an interpolated elevation z at a given point x
based on samples zi = z (xi) for i = 1, 2, , N using IDW is an interpolating
function (Equation 12.3).
where wi(x) = 1/d(x, xi)p is a simple IDW weighting function (Shepard 1968), x
denotes an interpolated (arbitrary) point, xi is an interpolating (known) point, d is
a given distance (metric operator) from the known point xi to the unknown point x,
N is the total number of known points used in interpolation, and p is a positive
real number, called the power parameter.
In the TIN model, the three points associated with the triangle inside which the
point falls, as well as other nearest points, can be used for calculation.
12.3.2 Slope
Slope identifies the steepest downhill slope for a location on a surface. Slope is
calculated for each triangle in TINs and for each cell in raster. TIN is the
maximum rate of change in elevation across each triangle, and the output polygon
feature class contains polygons that classify an input TIN by slope. For raster,
slope is determined as the greatest of the differences in elevation between a cell
and each of its eight neighbors, and the output is a raster.
The slope is the angle of inclination between the surface and a horizontal plane,
and may be expressed in degrees or percent. Slope in degrees is given by
calculating the arctangent of the ratio of the change in height (dZ) to the change in
horizontal distance (dS) (Equation 12.4).
Percent slope is equal to the change in height divided by the change in horizontal
distance multiplied by 100 (Equation 12.5).
For a raster slope, the values of the center cell and its eight neighbors determine
the horizontal and vertical deltas. As shown in Figure 12.6, the neighbors are
identified as letters from a to i, with e representing the cell for which the aspect is
being calculated.
FIGURE 12.6
Surface scanning window.
The rate of change in the x direction for cell e is calculated with Equation 12.6.
The rate of change in the y direction for cell e is calculated with Equation 12.7.
Based on the above Equations 12.4 through 12.7, the summarized algorithm used
to calculate the slope is demonstrated in Equation 12.8.
Slope can also be measured in units of degrees, which uses Equation 12.9.
12.3.3 Aspect
Aspect is the direction that a slope faces. It identifies the steepest downslope
direction at a location on a surface. It can be thought of as slope direction or the
compass direction a hill faces. Aspect is calculated for each triangle in TINs and
for each cell in raster. Figure 12.7 shows an example of the aspect results of a
surface using ArcMap 3D Analytics.
FIGURE 12.7
Clockwise in calculation aspect.
Aspect is measured clockwise in degrees from 0 (due north) to 360 (again due
north, coming full circle). The value of each cell in an aspect grid indicates the
direction in which the cells slope faces (Figure 12.7).
Aspect is calculated using a moving 3 3 window visiting each cell in the input
raster. For each cell in the center of the window (Figure 12.6), an aspect value is
calculated using an algorithm that incorporates the values of the cells eight
neighbors. The cells are identified as letters a through i, with e representing the
cell for which the aspect is being calculated.
The rates of change in the x and y directions for cell e are calculated with
Equation 12.10.
Taking the rate of change in both the x and y directions for cell e, aspect is
calculated using Equation 12.11.
The aspect value is then converted to compass direction values (0360 degrees),
according to the following rule:
if aspect < 0
cell = 90.0 aspect
else if aspect > 90.0
cell = 360.0 aspect + 90.0
else
cell = 90.0 aspect
For raster surface dataset, the distance is calculated between cell centers.
Therefore, if the cell size is 1, the distance between two orthogonal cells is 1, and
the distance between two diagonal cells is 1.414 (the square root of 2). If the
maximum descent to several cells is the same, the neighborhood is enlarged until
the steepest descent is found. When a direction of steepest descent is found, the
output cell is coded with the value representing that direction (Figures 12.9 and
12.10). Taking the 3 by 3 square (in red rectangle) as an example, the center cell
(row 3, column 3) has a value of 44, surrounded by 8 neighboring cells. The
steepest descent can be found at southeastern cell, which has the largest change
from the center cell. Since the steepest descent direction is found to be the
southeast, based on the direction coding (Figure 12.9), the flow direction of the
center cell is 2.
FIGURE 12.9
Direction coding.
FIGURE 12.10
Flow direction example.
If all neighboring cells are higher than the processing cell or when two cells
flow into each other, creating a two-cell loop, then the processing cell is a sink,
whose flow direction cannot be assigned one of the eight valid values. Sinks in
elevation data are most commonly due to errors in the data. These errors are often
caused by sampling effects and the rounding of elevations to integer numbers.
To create an accurate representation of flow direction, it is best to use a dataset
that is free of sinks. Sinks should be filled to ensure proper delineation of basins
and streams; otherwise, a derived drainage network may be discontinuous. The
profile view of filling a sink is illustrated in Figure 12.11. For example, if the
center pixel in Figure 12.9 is a sink, then the values of the nine pixels are sorted in
a specified order (Figure 12.11) according to one of the several existing
algorithms.
FIGURE 12.11
Profile view of a sink before and after fill.
After filling sinks, flow direction can be conducted a second time to ensure
accuracy. Flow accumulation can be determined based on the flow direction
results. Accumulation is calculated according to the total number of cells that
drain into a given cell and the value of each cell. First, the incremental flow
(Figure 12.12b) of a given cell can be calculated by adding up the number of cells
flowing into it. The total flow (Figure 12.12c) of a cell can then be calculated by
summing the incremental flow value from the given cell and incremental flow
values from all incoming cells.
FIGURE 12.12
Flow accumulation calculations. (a) Flow directions. (b) Incremental flow. (c) Total flow.
Taking, as an example, the cell located at row 3, column 3 in Figure 12.12 (red
rectangles), there are three arrows pointing to this cell, from northwest, west, and
southwest, so that in the incremental flow, the value of this cell is 3. The
accumulation value of this cell is obtained by adding this incremental value and
the values from all the cells that flow into the cell. In this case, the calculation of
accumulation flow is to add the incremental value 3 and 1 from northwest cell, 3
from west cell, and 0 from southwest. Therefore, the accumulation flow of this
cell is 3 + 1 + 3 + 0 = 7.
CODE 12.1
Conversion between DEM and TIN.
FIGURE 12.13
Result of Code 12.1. (a) TIN created from DEM. (b) DEM created from the TIN.
Step 2. Compare the original DEM and the new DEM that was regenerated
from the TIN. Observe the areas with dramatic difference and consider the
reasons (Figure 12.14).
FIGURE 12.14
DEM comparison. (a) Original DEM. (b) DEM created from TIN, which is generated from the original DEM.
Step 3. Run Code 12.2 in ArcMap Python window to create contours through a
DEM surface raster dataset (Figure 12.15).
CODE 12.2
Create contour from DEM.
FIGURE 12.15
Result of Code 12.2the contour created from DEM.
Step 4. Run Code 12.2 again, using the new DEM generated from the TIN as
input, to create another contour layer. Compare the two contour layers
(Figure 12.16).
FIGURE 12.16
Contour comparison. (a) Contour generated from the new DEM. (b) Pink line is the contour generated from
original DEM and green line is the one from the new DEM.
CODE 12.3
Create slope from DEM.
FIGURE 12.17
Result of Code 12.3.
2. Run Code 12.4 in ArcMap Python window to create aspect from DEM
(Figure 12.18).
CODE 12.4
Create aspect from DEM.
FIGURE 12.18
Result of Code 12.4.
CODE 12.5
Create flow direction from DEM.
FIGURE 12.19
Result of Code 12.5.
CODE 12.6
Check and fill sink.
FIGURE 12.20
Flow accumulation layer generated by running Codes 12.3 through 12.7.
1. Surface data structure and essential representations of surface data, that is,
discrete data or continuous surface data.
2. The creation of two types of continuous surface data: Grid and TIN, and
the conversion between them.
3. Surface data analysis, including the calculation of elevation, slope, aspect,
and flow direction.
4. Hands-on experience with arcpy, conducting conversion among DEM,
TIN, and contours, and calculating slope, aspect, and flow direction.
PROBLEMS
1. Review the chapter and 3D Analyst extension of ArcGIS.
2. Pick a problem related to 3D surface analysis.
3. Design a solution for the problem, which should include the transformation
to TIN and Grid, slope analysis, and aspect or flow direction analysis.
4. Select related 3D datasets to evaluate.
5. Code in Python to program a tool added to the ArcToolBox for operating
ArcGIS and 3D Analyst and use it to obtain the results data/information
needed in your solution.
NOTE: All codes can be successfully executed on ArcGIS for desktop versions
10.2.2 through 10.3. There may be a problem on running the code on the more
recent version of ArcGIS.
Section IV
Advanced Topics
13
Performance-Improving Techniques
13.1 Problems
If the waiting time for processing cannot be tolerated by the end user, then the
performance of a tool or computer software is critical to its success. Many GIS
algorithms are time-consuming. For example, given the 10,000 GIS river features
of the United States, finding the features within Washington, D.C., can be
accomplished by evaluating each river to see whether or not it intersects with the
D.C. boundary. Supposing that one such evaluation takes 0.1 second, the entire
process could take 0.1 10,000 = 1000 seconds, or approximately 18 minutes.
Such a long time is not tolerable to end users and is not typical to commercial
software or optimized open-source software. As another example, if we have
10,000 roads and 10,000 rivers within the United States, and we want to find all
intersecting points, we need to check for possible intersections between each road
and each river. If such an average intersecting check takes 0.01 second, the entire
process would take 10,000 10,000 0.01 seconds = 1M seconds or
approximately 300 hours, which is not tolerable. Many techniques can be utilized
to improve processing. We will take a closer look at three different techniques to
examine how and to what extent they can improve performance, as well as their
limitations and potential problems.
Returning to the second example, calculate the line intersection by
By switching the FileBuffer between true and false for reading one layer of
Amtrack station data (amtk_sta.shp) on a laptop, we obtained a value of ~0.060
seconds for true and a value of ~0.069 seconds for false. These values may
change according to different datasets, computers, and working loads of the
computers.
13.3.2 Multithreading
The performance package integrates a sample multithreading framework. The
logic workflow repeats a 0.01-second process 1000 times. Processed
sequentially, this will require roughly 10 seconds to finish all processes.
However, you can break the required processes into 10 groups, with each group
processed by a thread, so all 10 groups can be executed concurrently. Ideally, this
approach could reduce the processing time from 10 seconds to 1 second. Code
13.2a creates a 10-thread list and Code 13.2b creates one thread and executes the
same number of processes. The SummingThread is inherited from the Python
module threading class Thread.
CODE 13.2
Execute the same process (takes 0.01 second) 10,000 times in 10 multithreads (a) versus in one single thread
(b).
The code is included in the multithreading.py file and the execution will output
the time spent by each method. It is observed that the 10-multithread approach is
about 10 times faster than the single-thread approach by running the Python file.
The code can be experimented on by running on one computer or multiple
computers to compare the time outputs to observe the improvements.
CODE 13.3
Loading three data layers concurrently (a) or in sequence (b).
1. Check the intersection of two data layers in the map (can be selected from
existing layers in GUI).
2. Check the intersection of two features in the data layers, using one from
each layer (rivers and roads), and keep all intersecting points (Layer
class).
3. Check the intersection of every line segment pair, using one from each
layer (rivers and roads), and keep all intersecting points (Polyline class).
4. Check the intersection and calculate the intersecting point (LineSeg class).
To speed up the process, add the bounding box check to the first three steps to:
1. Check whether two data layers are intersecting with each other and return
false if not.
2. Check whether two features bounding boxes are intersecting with each
other and return false if not.
3. Check whether two line segments bounding boxes intersect with each
other and return false if not.
The bounding boxes checking algorithm is relatively simple for bounding box
one (bb1) and bounding box two (bb2):
If this statement is true, then the two bounding boxes cannot intersect with each
other. If this is not true, the two bounding boxes intersect with each other and we
can proceed with the following, more time-consuming, calculations.
FIGURE 13.1
R-Tree example.
CODE 13.4
Using bounding box check to filter out most intersection calculations in Layer class.
Once the calculations are completed, you can display the resulting points on the
GUI. This can be handled in several ways, including (a) adding a data layer to
store the points as the intersecting point layer, (b) maintaining a data structure,
for example, a list of intersecting points of map object to keep all points, and (c)
associating the points with one of the two layers. To simplify the process, choose
the second method by maintaining a list data structure. After the intersection
calculations, obtain a list of points defined as self.intersectPoints in the Map class
of map.py file. When displaying the points after the calculation, you need to go
through a process similar to what you used to display points on Tkinter (Code
13.5)
PROBLEMS
The objective of this homework is to understand and design a comprehensive
performance tuning and management process.
1. Please select four different datasets or use the four datasets provided.
2. Design a comprehensive performance testing experiment.
3. Conduct the tests using the MiniGIS package.
4. Compare the performance improvements before and after adopting the
techniques.
5. Explain the performance differences and discuss the trade-off when using
different techniques.
NOTES: The results may differ according to the datasets selected and the computers
used to run the MiniGIS. The applicability of the three different categories of
techniques will determine the final performance of the software.
14
Advanced Topics
GIS algorithms and programming are critical to the research and development in
advancing geographical information sciences (GIScience), because most mature
GIS software packages are not flexible enough to be revised for the purpose of
testing new ideas, models, and systems. This chapter introduces how GIS
programming and algorithms are utilized in advancing several GIScience
frontiers.
FIGURE 14.1
(a) The architecture of classic data model. (b) The architecture of the improved data model. (From
http://www.unidata.ucar.edu/software/netcdf/papers/nc4_conventions.html.)
FIGURE 14.2
The logical structure of the improved data model.
To address the limitations of the classic data model, a new data model is
proposed and implemented (Figure 14.1b). It adds a top-level, unnamed group to
contain additional named variables, dimensions, attributes, groups, and types.
Groups are like directories in a file system, each with its own set of named
dimensions, variables, attributes, and types. Every file contains at least the root
group. A group may also contain subgroups to organize hierarchical datasets. The
variables depicted in Figure 14.2 can be divided into different groups by a certain
characteristic, such as the model groups that generated these data. When storing
these variables in a physical file, each 2D grid will be decomposed into one-
dimensional byte stream and stored separately, one by one, in a data file.
One month (January 2015) of the MAT1NXINT product (45.29 GB) was used as
experimental data. MapReduce was adopted to compute the daily mean in parallel
for a specified climate variable over a specified spatiotemporal range. Two
scenarios were evaluated: the first scenario, as the baseline, was performed
without using the spatiotemporal index; the second was performed using the
spatiotemporal index. The experiments were conducted on a Hadoop cluster
(version 2.6.0) consisting of seven computer nodes (one master node and six slave
nodes) connected via 1 Gigabit Ethernet (Gbps). Each node was configured with
eight CPU cores (2.35 GHz), 16 GB RAM, and CentOS 6.5.
Figure 14.4 shows the run times, comparing the baseline and the indexing
approach for different numbers of variables. When increasing the number of
variables in the query, the run time for the baseline condition increased from 55 to
1042 secondsnearly 19 times longer. In contrast, when the spatiotemporal index
was employed, the run time increased from 35 seconds to 85 secondsonly 2.4
times longer. This comparison result demonstrates that the spatiotemporal index
can significantly improve the data access rate for the MERRA data stored in
HDFS. Two factors lead to this improvement:
1. The spatiotemporal index can point exactly to data locations at the node,
file, and byte level. It can tell MapReduce at which node the specified
array of a variable is located, which file it is stored in, and its starting
and ending bytes within the file. With this information, MapReduce can
move the program close to the nodes where the referenced data are
stored, minimizing data transfer by the network.
2. The data structure information is extracted and stored externally in the
database. Therefore, when querying the data by variable names, only the
database needs to be queried; there is no need to repeatedly access the
metadata in MERRA data using HDFS I/O API, and, thus, avoiding the
limitation of HDFS when randomly accessing small data.
FIGURE 14.4
Run time for computing the daily global mean for January 2015 for different numbers of variables.
14.2.1 Data
Three datasets are used in this example: tweets in NYC during Chinese New Year
and the Asian population and number of Asian restaurants in NYC at census tract
level. Twitter data was collected using Twitter API; the Asian population data
were retrieved from U.S. Census Bureau; and the number of Asian restaurants was
acquired by using Google Places API (Figure 14.5).
FIGURE 14.5
Asian population in New York City at census tract level (left) and the number of Asian restaurants in New
York City at census tract level (right).
FIGURE 14.6
An example of Twitter data output.
FIGURE 14.7
Geographical distribution of collected Twitter data in New York City. (From CartoDB.)
A heat map allows for easy identification of hotspots and clustered points.
Figure 14.8 shows a heat map of filtered tweets in New York City created by
CartoDB, a popular online GIS analysis application. As we can see, most of the
tweets related to Spring Festival are concentrated in Manhattan and Queens.
FIGURE 14.8
Heat map of tweets in New York City (red represents high density, while blue represents low density).
FIGURE 14.9
The standard residual of OLS of each census tract.
FIGURE 14.10
The local R2 of GWR of each census tract.
Time series plotting (Figure 14.12): Users can select multiple data
variables in multiple areas of interest (AOIs) for the same time period and
plot the time series for better comparison.
Correlation analyses for two variables with the same AOI (Figure 14.13):
Display the relationships of any two variables or two AOIs using scatter
plots.
Synchronous visualization of multiple parameters (Figure 14.14): When
users interact with one map window, the other three map windows will
simultaneously zoom to the same scale and geographic region to compare
the selected variables.
Data validation analysis (Figure 14.15): A Taylor diagram is a way of
graphically summarizing how closely a pattern (or a set of patterns)
matches observations. The position of each point appearing on the plot
quantifies how closely that models simulated result pattern matches
observations. Users can input the spatiotemporal range and select the
climate data from different models for comparison. The server end will
then retrieve the specified data in parallel and compute the parameters,
such as standard deviation, root mean square, and correlation to
incorporate in Taylor diagram. After these parameters are visualized in a
Taylor diagram, the client will render the Taylor diagram images with the
information about its spatiotemporal range and input datasets. The Taylor
diagrams for different spatiotemporal ranges can also be displayed on the
same page for intercomparison.
FIGURE 14.12
Time series plotting for two variables and two AOIs.
FIGURE 14.13
Correlation analyses for two variables.
FIGURE 14.14
Four variables displayed in four windows.
FIGURE 14.15
GUI for Taylor diagram service.
FIGURE 14.17
Two scheduling methods for dispatching eight subdomains to two computing nodes. (a) Non-cluster scheduling
of 8 subdomains to 2 computing nodes (yellow and blue), (b) Non-cluster scheduling of 8 subdomains to 2
computing nodes (yellow and blue).
FIGURE 14.18
Different decomposition methods and their corresponding performance.
14.4.3.3 Dust Storm Events Are Isolated: Nested Model
The third spatiotemporal pattern of dust storm is that dust storm events are
isolated or restricted in their spatiotemporal scope and can, therefore, be treated
event by event. Various dust models have been developed to simulate the
emission, transport, and deposition of dust storms using different resolution
limitations and domain ranges. It would be ideal for a high-resolution model to
simulate a large spatial domain, but this goal is difficult to accomplish due to
computing capacity and memory consumption (Yang et al. 2011b). Therefore,
Huang et al. (2013a) proposed an adaptive, loosely coupled strategy, which
couples a high-resolution/small-scale dust model with a coarse-resolution/large-
scale model. Specifically, the adaptive, loosely coupled strategy would (1) run the
low-resolution model; (2) identify subdomains of high predicted dust
concentrations (Figure 14.19); and (3) run the higher-resolution model for only
those subdomains with much smaller area in parallel.
FIGURE 14.19
Low-resolution model domain area and subregions (AOI, area of interest,) identified for high-resolution model
execution.
PROBLEMS
Requirement:
Analyze the problems solved in each of the four examples in this chapter to
identify where programming can be used and how algorithms are
developed for modeling objectives.
Identify an academic GIScience journal paper, read and understand the
paper, and try to lay out the aspects of the problem, data, algorithm, and
solution.
Discuss how to create a program to implement the solution analyzed in the
journal paper.
Deliverables:
Project Report
Explain the paper problem.
Explain the solution.
Explain how the algorithms learned in class can be adopted to develop
the solution.
Keep it as simple but clear as possible, and try to use a diagram or
picture in your report.
References
Agarwal, D., Puri, S., He, X., and Prasad, S.K. 2012. A system for GIS polygonal overlay computation on
Linux cluster-an experience and performance report. In Parallel and Distributed Processing
Symposium Workshops and PhD Forum (IPDPSW), 2012 IEEE 26th International, 14339. IEEE,
Shanghai, China.
Aho, A.V. and Ullman, J.D. 1972. The Theory of Parsing, Translation, and Compiling. Upper Saddle River,
NJ: Prentice Hall, Inc.
Aji, A., Wang, F., Vo, H., Lee, R., Liu, Q., Zhang, X., and Saltz, J. 2013. Hadoop GIS: A high performance
spatial data warehousing system over MapReduce. Proceedings of the VLDB Endowment 6(11):1009
20.
Arnold, K., Gosling, J., Holmes, D., and Holmes, D. 2000. The Java Programming Language, Vol. 2.
Reading, MA: Addison-Wesley.
Benedetti, A., Baldasano, J.M., Basart, S. et al. 2014. Operational dust prediction. In Mineral Dust: A Key
Player in the Earth System, eds. P. Knippertz, and W.J.-B. Stuut, 22365. Dordrecht: Springer
Netherlands.
Bondy, J.A. and Murty, U.S.R. 1976. Graph Theory with Applications (290). New York: Citeseer.
Bosch, A., Zisserman, A., and Munoz, X. 2007. Image classification using random forests and ferns.
International Conference on Computer Vision, Rio de Janeiro, Brazil.
Bourke, P. 1988. Calculating the Area and Centroid of a Polygon. Swinburne University of Technology,
Melbourne, Australia.
Chang, K.T. 2006. Introduction to Geographic Information Systems. Boston, MA: McGraw-Hill Higher
Education.
Chen, Z. and Guevara, J.A. 1987. Systematic selection of very important points (VIP) from digital terrain
models for construction triangular irregular networks. Proceedings, AutoCarto 8, ASPRS/ACSM, Falls
Church, VA, 506.
Crooks, A., Croitoru, A., Stefanidis, A., and Radzikowski, J. 2013. Earthquake: Twitter as a distributed sensor
system. Transactions in GIS 17:12447.
Dale, M.R., Dixon, P., Fortin, M.J., Legendre, P., Myers, D.E., and Rosenberg, M.S. 2002. Conceptual and
mathematical relationships among methods for spatial analysis. Ecography 25(5):55877.
Dee, D. and National Center for Atmospheric Research Staff. eds. The Climate Data Guide: ERA-Interim.
https://climatedataguide.ucar.edu/climate-data/era-interim (accessed June 9, 2016).
Dijkstra, E.W. 1959. A note on two problems in connexion with graphs. Numerische Mathematik 1:26971.
Eckerdal, A., Thun, M., and Berglund, A. 2005. What does it take to learn programming thinking? In
Proceedings of the First International Workshop on Computing Education Research, 13542.
ACM, Seattle, WA.
ESRI. 1998. ESRI Shapefile Technical Description. An ESRI White Paper, 34.
ESRI. 2016a. What Is ModelBuilder?, http://pro.arcgis.com/en/pro-
app/help/analysis/geoprocessing/modelbuilder/what-is-modelbuilder-.htm (accessed September 9, 2016).
ESRI. 2016b. What Is ArcPy?, http://pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-arcpy-.htm
(accessed September 9, 2016).
Fowler, R.J. and Little, J.J. 1979. Automatic extraction of irregular network digital terrain models. Computer
Graphics 13:199207.
Fowler, M. 2004. UML Distilled: A Brief Guide to the Standard Object Modeling Language. Boston, MA:
Addison-Wesley Professional.
Gittings, B.M., Sloan, T.M., Healey, R.G., Dowers, S., and Waugh, T.C. 1993. Meeting expectations: A review
of GIS performance issues. In Geographical Information HandlingResearch and Applications, ed.
P.M. Mather, 3345. Chichester: John Wiley & Sons.
Goodchild, M.F. 1992. Geographical information science. International Journal of Geographical
Information Systems 6(1):3145.
Gosselin, T.N., Georgiadis, G., and Digital Accelerator Corporation. 2000. Digital data compression with quad-
tree coding of header file. U.S. Patent 6,094,453.
Grafarend, E. 1995. The optimal universal transverse Mercator projection. In Geodetic Theory Today, 5151.
Berlin, Heidelberg: Springer.
Guttman, A. 1984. R-trees: A dynamic index structure for spatial searching. Proceedings of the 1984 ACM
SIGMOD International Conference on Management of Data 14(2):4757. ACM.
Healey, R., Dowers, S., Gittings, B., and Mineter, M.J. eds. 1997. Parallel Processing Algorithms for GIS.
Bristol, PA: CRC Press.
Hearnshaw, H.M. and Unwin, D.J. 1994. Visualization in Geographical Information Systems. Hoboken,
NJ: John Wiley & Sons Ltd.
Hoeffding, W. 1963. Probability inequalities for sums of bounded random variables. Journal of American
Statistical Association 58:1330.
Huang, Q., Yang, C., Benedict, K., Chen, S., Rezgui, A., and Xie, J. 2013b. Utilize cloud computing to support
dust storm forecasting. International Journal of Digital Earth 6(4):33855.
Huang, Q., Yang, C., Benedict, K. et al. 2013a. Using adaptively coupled models and high-performance
computing for enabling the computability of dust storm forecasting. International Journal of
Geographical Information Science 27(4):76584.
Hutchins, W.J. 1986. Machine Translation: Past, Present, Future, 66. Chichester: Ellis Horwood.
Hwang, K. and Faye, A. 1984. Computer Architecture and Parallel Processing, Columbus, OH: McGraw-
Hill.
Jack, K. 2011. Video Demystified: A Handbook for the Digital Engineer. Burlington, MA: Elsevier.
Johnson, R.A. 1929. Modern Geometry: An Elementary Treatise on the Geometry of the Triangle and the
Circle, 1736, 24950, and 2689. Boston, MA: Houghton Mifflin.
Kanan, C. and Cottrell, G.W. 2012. Color-to-grayscale: Does the method matter in image recognition? PloS
One 7(1):e29740.
Kernighan, B.W. and Ritchie, D.M. 2006. The C Programming Language. Upper Saddle River, NJ: Prentice
Hall.
Khalid, M. 2016. Map, Filter and Reduce. http://book.pythontips.com/en/latest/map_filter.html (accessed
September 3, 2016).
Knippertz, P. and Stuut, J.B.W. 2014. Mineral Dust. Dordrecht, Netherlands: Springer.
Lee, J. 1991. Comparison of existing methods for building triangular irregular network, models of terrain from
grid digital elevation models. International Journal of Geographical Information System 5(3):26785.
Li, Z., Hu, F., Schnase, J.L. et al. 2016. A spatiotemporal indexing approach for efficient processing of big
array-based climate data with MapReduce. International Journal of Geographical Information
Science 119.
Lien, D.A. 1981. The Basic Handbook: Encyclopedia of the Basic Computer Language. San Diego, CA:
Compusoft Pub.
Linuxtopia. 2016. Set Operations.
http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch16s03.html
(accessed September 3, 2016).
Longley, P.A., Goodchild, M.F., Maguire, D.J., and Rhind, D.W. 2001. Geographic Information System and
Science. England: John Wiley & Sons, Ltd., 3279.
McCoy, J., Johnston, K., and Environmental Systems Research Institute. 2001. Using ArcGIS Spatial
Analyst: GIS by ESRI. Redlands, CA: Environmental Systems Research Institute.
Mitchell, J.C. 1996. Foundations for Programming Languages (1). Cambridge: MIT press.
Misra, P. and Enge, P. 2006. Global Positioning System: Signals, Measurements and Performance Second
Edition. Lincoln, MA: Ganga-Jamuna Press.
Nanjundiah, R.S. 1998. Strategies for parallel implementation of a global spectral atmospheric general
circulation model. High Performance Computing, 1998. HIPC98. 5th International Conference On
4528. IEEE.
Neteler, M. and Mitasova, H. 2013. Open Source GIS: A GRASS GIS Approach (689). New York: Springer
Science and Business Media.
NOAA. 2011. Dust Storm Database. https://www.ncdc.noaa.gov/stormevents/ (accessed August 30, 2016).
Ostrom, E., Burger, J., Field, C.B., Norgaard, R.B., and Policansky, D. 1999. Revisiting the commons: Local
lessons, global challenges. Science 284(5412):27882.
Peng, Z.R. 1999. An assessment framework for the development of Internet GIS. Environment and
Planning B: Planning and Design 26(1):11732.
Pick, M. and imon, Z. 1985. Closed formulae for transformation of the Cartesian coordinate system into a
system of geodetic coordinates. Studia geophysica et geodaetica 29(2):1129.
Pountain, D. 1987. Run-length encoding. Byte 12(6):3179.
Proulx, V.K. 2000. Programming patterns and design patterns in the introductory computer science course.
ACM SIGCSE Bulletin 32(1):804. ACM.
Python. 2001a. Built-in Functions. https://docs.python.org/3/library/index.html (accessed September 3, 2016).
Python. 2001b. Errors and Exceptions. https://docs.python.org/2/tutorial/errors.html (accessed September 3,
2016).
Pythoncentral. 2011. Pythons range() Function Explained. http://pythoncentral.io/pythons-range-function-
explained/ (accessed September 3, 2016).
PythonForBeginers. 2012. Reading and Writing Files in Python.
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python (accessed September 3,
2016).
Raschka, S. 2014. A Beginners Guide to Pythons Namespaces, Scope Resolution, and the LEGB Rule.
http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html (accessed September 3,
2016).
Rawen, M. 2016. Programming: Learn the Fundamentals of Computer Programming Languages (Swift,
C++, C#, Java, Coding, Python, Hacking, Programming Tutorials). Seattle, WA: Amazon Digital
Services LLC, 50.
Rew, R. and Davis G. 1990. NetCDF: An interface for scientific data access. IEEE Computer Graphics and
Applications 10(4):7682.
Ritter, N. and Ruth, M. 1997. The GeoTiff data interchange standard for raster geographic images.
International Journal of Remote Sensing 18(7):163747.
Rodrigue, J. 2016. Network Data Models. Methods in Transport Geography.
https://people.hofstra.edu/geotrans/eng/methods/ch2m3en.html (accessed June 22, 2016).
Rumbaugh, J., Blaha, M., Premerlani, W., Eddy, F., and Lorensen, W.E. 1991. Object-Oriented Modeling
and Design, 199(1). Englewood Cliffs, NJ: Prentice Hall.
Samet, H. 1990. The Design and Analysis of Spatial Data Structures, 199. Reading, MA: Addison-Wesley.
Shepard, D. 1968. A two-dimensional interpolation function for irregularly-spaced data. In Proceedings of the
1968 23rd ACM National Conference, New York, 51724. ACM.
Shimrat, M. 1962. Algorithm 112: Position of point relative to polygon. Communications of the ACM 5(8):434.
Stroustrup, B. 1995. The C++ Programming Language. Delhi, India: Pearson Education India.
Teampython. 2013. Conditioned Choropleth Maps (CCMaps) Generator.
http://www.arcgis.com/home/item.html?id=a1c79e4cb3da4e0db9dc01b11fea9112 (accessed September 9,
2016).
Tu, S., Flanagin, M., Wu, Y. et al. 2004. Design strategies to improve performance of GIS web services. ITCC
2:444.
Tullsen, D.M., Eggers, S.J., and Levy, H.M. 1995. Simultaneous multithreading: Maximizing on-chip
parallelism. ACM SIGARCH Computer Architecture News 23(2):392403. ACM.
Van Rossum, G. 2007. Python programming language. In USENIX Annual Technical Conference, Santa
Clara, CA, 41.
wiki.gis. 2011. Centroid. http://wiki.gis.com/wiki/index.php/Centroid (accessed September 3, 2016).
Wilkening, K.E., Barrie, L.A., and Engle, M. 2000. Trans-Pacific air pollution. Science 290(5489):65.
World Meteorological Organization (WMO). 2011. WMO Sand and Dust Storm Warning Advisory and
Assessment System (SDSWAS)Science and Implementation Plan 20112015. Geneva, Switzerland:
WMO.
Xie, J., Yang, C., Zhou, B., and Huang, Q. 2010. High-performance computing for the simulation of dust
storms. Computers, Environment and Urban Systems 34(4):27890.
Yang, C., Goodchild, M., Huang, Q. et al. 2011a. Spatial cloud computing: How can the geospatial sciences use
and help shape cloud computing? International Journal of Digital Earth 4(4):30529.
Yang, C., Wong, D.W., Yang, R., Kafatos, M., and Li, Q. 2005. Performance-improving techniques in web-
based GIS. International Journal of Geographical Information Science 19(3):31942.
Yang, C., Wu, H., Huang, Q. et al. 2011c. WebGIS Performance Issues and Solutions, 12138. New York,
USA: Taylor & Francis.
Yang, C., Wu, H., Huang, Q., Li, Z., and Li, J. 2011b. Using spatial principles to optimize distributed computing
for enabling the physical science discoveries. Proceedings of the National Academy of Sciences
108(14):5498503.
Yang, C., Xu, Y., and Nebert, D. 2013. Redefining the possibility of digital Earth and geosciences with spatial
cloud computing. International Journal of Digital Earth 6(4):297312.
Zhang, J. 2010. Towards personal high-performance geospatial computing (HPC-G): Perspectives and a case
study. In Proceedings of the ACM SIGSPATIAL International Workshop on High Performance and
Distributed Geographic Information Systems, San Jose, CA, 310. ACM.
Index
A
Adjacency matrix, 209
Aggregation, 12, 14
AOIs, see Areas of interest
Application software, 34
ArcGIS ModelBuilder, 153
ArcGIS programming, 21, 153; see also Python programming
accessing and editing data with cursors, 160166
ArcPy package, 154158, 159
ArcToolbox, ModelBuilder, and ArcMap Python window, 154
assignment, 182
automating ArcTools with Python, 158160, 161, 162
automating map production, 172, 173
creating ArcTools from scripts, 172, 174176
describing and listing objects, 166169
external document and video resources, 177178
handling errors and messages, 176177, 178
manipulating complex objects, 169171
ArcGIS software, 3, 7, 77
attribute information of raster dataset and calculating area, 200204
conversion among DEM, TIN, and contours, 236239
flow direction, 239242
hands-on experience with, 198, 218222, 236
implementing spatial relationship calculations using, 178180, 181, 182, 183, 184
raster color renders, 198199
raster data analysis, 199200
slope and aspect generation, 239
ArcMap
menu bar, 157
programming with ArcPy in, 155156
Python window, 156, 199201
user interface, 127128, 129
arcpy.da.NumPyArrayToFeatureClass function, 165
arcpy.GetMessage() method, 176
arcpy.mapping module, 172
arcpy.NumPyArrayToTable, 165
ArcPy package, 153, 154
functions, classes, and modules, 154155
using help documents, 157158, 159
programming with ArcPy in ArcMap, 155156
programming with ArcPy in Python window outside ArcMap, 156157
arcpy.TableToNumPyArray functions, 165
ArcToolbox, 153154
automating ArcTools with Python, 158162
ArcTools from scripts creation, 172, 174176
Area, 139
polygon with hole(s), 140141
of simple polygon, 139140
Area Practice window, 148, 149
Areas of interest (AOIs), 277
Arithmetic Operators, 5051
Artificial language, 19
Aspect, 232
clockwise in calculation, 233
generation, 239
surface aspect, 234
value, 233
Assignment Operators, 50, 52
Attributes, 9, 12, 2325
B
Base contour, 225
BASIC programming language, 19
Behaviors, 9
Big-endian byte orders, 99100, 103104
Binary data manipulation, 97100
Binary format, 97
Bit, 1920
Bitwise&Shift Operators, 5051
Bounding box, 103, 109
checking to speed up intersection, 258261
in GIS, 255256
Breaklines, 224
Break statement, 65
Brute force approach for shortest path algorithm, 211212
Built-in functions, 46
Built-in methods, 67
Built-in module, 126127
check module, 128
math methods, 128
C
C++ programming language, 6, 19, 2021
CAD, see Computer Aided Design
calDis method, 21
Canvas widget, 7980, 8485
Cartesian coordinate system, 141, 142
Catch exceptions, 122
Central processing units (CPU), 56
Centroid, 137
Centroid Practice window, 148, 149
of polygon, 138139
of rectangle, 137138
of triangle, 137, 138
CFSR data, see Climate Forecast System Reanalysis data
Chinese New Year, 270
Classes, 9, 12, 2123, 35
ArcPy package, 154155
attributes, 2325
composition, 2627
inheritance, 14, 2526
Classic data model, 266
Classified renderer, 196
Climate Forecast System Reanalysis data (CFSR data), 277
Climate science, 275
Climate study, application of NetCDF/HDF on, 266269
Closest facility analysis, 214, 215
Cloud computing, 253254, 285
Cluster computing, 253254
CMAP data, see CPC Merged Analysis of Precipitation data
Code style, 4950
Code writing, 87
Color
depth, 194
representation, 191194
screens, 192
Colormap, 191
Command-line GUI, 115
Python, 116
Comments, 4950
Comparison Operators, 51, 52, 61
Compiler, 19
Composite data types, 42
built-in functions, 46
dictionary data type, 4647
list, 4245
list methods, 45
Set operations, 4748
Composition, 12, 14, 2627
Comprehensive consideration, 249251
Computer, 3
hardware, 35
processing, 251
software, 35
system, 248249
Computer Aided Design (CAD), 190
Conditional expressions, 61
Continue statement, 66
Continuous data, 185, 223, 225
grid surface, 225226
TINs, 226227
Contours, 224225
interval, 225
lines, 224
Control structures, 61, 6667
Conversion of numbers, 41
Coordinate transformation, 8182
CPC Merged Analysis of Precipitation data (CMAP data), 277
C programming language, 6
CPU, see Central processing units
Cursors, accessing and editing data with, 160
InsertCursor, 164165
NumPy, 165166
SearchCursor, 160, 162163
UpdateCursor, 164
D
Data, 270271
coordinate systems, 81
DEM, 188
discrete, 223225
manipulation and analysis, 172
Twitter data, 271, 272
validation analysis, 278279
Data analysis, 160
raster, 199200
Data loading
with multithreading, 258
with RAM, 257258
Dataset, 127128
Data types, 40, 9899
built-in types, 4142
composite data types, 4248
float data types, 4041
datetime module, 127
Day-to-day geospatial coordinates, 81
Debugging, 118
add exception handles and clean-up actions to file read/write, 123124
handling exceptions, 122123, 124
run-time exceptions, 121122
SyntaxError, 120121
Decision-making process, 63
Decomposition method, 283, 284
Delaunay triangulation, 229, 230
DEMs, see Digital elevation models
Density analysis, 271272
Dependency, 12, 14
Describe function, objects, 166167
Descriptive operations, 197
Dictionary data type, 4647
Digital elevation models (DEMs), 185, 229
data, 188
Dijkstra algorithm, 212213
Directed networks, 207209
Direction coding, 235
Discrete Color renderer, 196
Discrete data, 223225
Disk access, 248
comprehensive consideration, 249251
file management, 249
Distance map, 197
Distance ordering, 229
Distributed GIS, 275; see also Mini-GIS
system architecture, 276277
user interface, 277280
docstring, see Documentation strings
Documentation strings, 50
.dbf file, 101
.shp file, 101
.shx file, 101
Double loop, 70
Dust
models, 280281
simulation, 281
transition process, 281
Dust storms, 280
clustered characteristics, 282283
events, 284285
forecasting, 285
spacetime continuity, 283
E
ECMWF Interim Reanalysis data (ERAInterim data), 277
Elevation, 230231
models, 225
Embedded software, 4
endByte, 268
ERA-INTRIM, 277
ERDAS IMAGINE, 190
Error message, 176
ESRI shapefile format, 99, 101
Except block, 122
except clause, 69
Exceptions, 69, 121
F
Feature bounding box, 255
Feature class, 27, 129
Field view, 185
File-based programming, 116117
FileBuffer, 249
File buffer, 257258
fileId, 268
File input/output, 6769
File length, 103, 105, 107
File management, 249
File read/write, add exception handles and clean-up actions to, 123124
Filter function, 46
Firmware, 4
float(str), 68
Float data types, 4041
Flow accumulation, 235236, 241
Flow direction, 239242
Focal operations, 197
Fonts setup for coding, Python IDE, 118, 119
For loop, 64, 105, 107
Four square matrices, 188189
Functional match, 89
Functions, 5456
ArcPy, 154155
G
Gbps, see Gigabit Ethernet
Geographical information sciences (GIScience), 5, 265
Geographically weighted regression (GWR), 273
local R2 value for, 274, 275
Geographic coordinate system, 8184
Geographic data, 190
Geographic information, 7
processing, 3
Geographic information system (GIS), 4, 137, 190
algorithms and modeling, 270275
algorithms and programming, 265
applications, 153, 190
ArcMap user interface, 129
data, 7781, 83
data models, 1011
data organization, 127128
data vector, 8081
map data organization hierarchy, 129
maps, 77
network representation in, 209210
objects creating, 9295
package, 35
programming, 21
software, 97, 194, 197, 214
spatial data structure, 265269
spatiotemporal thinking and computing, 280285
Geographic Resource Analysis Support System (GRASS), 2021
GeoInformatics, 5
Geometry, 170
GeoParameterValue functions, 174
GEOS-5, see Goddard Earth Observing System, version 5 data assimilation system
GetParameterAsText functions, 174
GetParameter functions, 174
GIF files, see Graphic Interchange Format files
Gigabit Ethernet (Gbps), 268
GIS, see Geographic information system
GIScience, see Geographical information sciences
Global Modeling and Assimilation Office (GMAO), 266
Global positioning system (GPS), 4, 57; see also Geographic information system (GIS)
Global Precipitation Climatology Project data (GPCP data), 277
Global variables, 39
GMAO, see Global Modeling and Assimilation Office
Goddard Earth Observing System, version 5 data assimilation system (GEOS-5), 266
Google Earth, 5
Google Maps, 3, 5
GPCP data, see Global Precipitation Climatology Project data
GPS, see Global positioning system
Graph, 207
Graphical user interface, 153
Graphic Interchange Format files (GIF files), 191
GRASS, see Geographic Resource Analysis Support System
Gray level intensity, 191192
Grayscale image, 191192
Grid, 265266
computing, 253254
model, 230231
surface, 225226
surface model creation, 228
gridId, 268
GWR, see Geographically weighted regression
H
Habitat analysis, 196197
Hadoop Distributed File System (HDFS), 268
Handling errors and messages, 176177
AddMessage examples, 178
Handling exceptions, 122123
tryexceptfinally to clean up variables, 124
Hands-on experience with mini-GIS, 131
package management and mini-GIS, 131132
run and practice mini-GIS package, 132134
Hands-on experience with python, 148150
Hard breaklines, 224
Hard drive (HDD), 249
Hardware, 35
HDD, see Hard drive
HDFS, see Hadoop Distributed File System
Heat map, 271
Help documents, 157158
syntax and sample code of Python script, 159
Hexadecimal representation, 99100
Hex number, 1920
HFA, see Hierarchical File Format
Hierarchical File Format (HFA), 190
High-performance computing, 281
dust storm events, 284285
dust storms clustered characteristics, 282283
dust storms spacetime continuity, 283
spatiotemporal thinking to optimizing, 281
Highlighting, Python IDE, 117118
High performance computing (HPC), 254
Hole(s), area of polygon with, 140140
HPC, see High performance computing
Hydrologic analysis, 234236
I
IDE, see Integrated Development Environment
IDW, see Inverse distance weighting
if. elifelse statement, 63
ifelse statement, 62
if statement, 159
IMAGINE image processing, 190
Indentation, 36
Indented codes, 49
Index file (.shx), 101, 105107
header, 105106
length, 107
Informative messages, 176
Inheritance, 12, 14, 2526
InsertCursor, 164165
Instruction based language, 19
int(str), 68
Integer, 103
data types, 40
Integrated Development Environment (IDE), 115
debugging, 118, 120124
fonts setup for coding, 118, 119
general setting of programming window, 118, 119
highlighting, 117118
python IDE, 115
python programming windows, 115117
settings, 117
Intensity method, 192
Interactive GUI, 115116
Interpretation
feature record, 104
index file structure, 105107
main file header, 102103
main file structure of shapefile, 102107
Interpreter, 19
Interpret polyline shapefiles, 109113
Inverse distance weighting (IDW), 228
J
Java, 6, 19, 21
JavaScript, 6
Joint Photographic Experts Group files (JPEG files), 191
K
Kernel density, 271272
Keywords for Python, 37
Kriging, 228
L
Land cover dataset, 201
Leaf nodes, 189
LempelZivWelch (LZW), 191
len() function, 6465
Length, 141
of polyline, 142
ratios, 82
of straight line segment, 141
LiDAR, see Light Detection and Ranging
Light-weight GIS software, 45
Light Detection and Ranging (LiDAR), 223
Line-based data, see Discrete data
Line intersection, 142
binary linear equation, 144
Line Intersection Practice window, 148, 150
line segments intersection, 143
mathematical representation of line in cartesian coordinate system, 143
parallel lines, 145
using R-tree index, 261262
vertical lines, 145146
Links table, 210
Linux, 34
command-line GUI, 115
List, 167169
composite data types, 4245
ListFeatureClasses method, 168
ListFields function, 167
Little-endian byte orders, 99100, 103104
Local operations, 197
Local variables, 39
Location-allocation analysis, 217218
Logic Operators, 51, 53, 62
long(str), 68
Long integers data types, 40
Loop and decisions combination, 66
Luma method, 192
Luster method, 192
LZW, see LempelZivWelch
M
Machine language instructions, 1920
Main file (.shp), 101
Manipulating complex objects, 169171
Map class, 259
Map drawing processes, 77
Map function, 46
Map production, automating, 172, 173
MapReduce, 268269
Mass points, 223224, 229
MAT1NXINT product, 268269
math module, 125, 128
Memory management, 248
comprehensive consideration, 249251
file management, 249
MERRA, see Modern-Era Retrospective Analysis for Research and Applications
Methods, 12
Mini-GIS; see also Distributed GIS
bounding box checking, 258261
data loading with multithreading, 258
data loading with RAM, 257258
hands-on experience with, 257
hands-on experience with mini-GIS, 131134
interpret polyline shapefiles, 109113
line intersection using R-Tree index, 261262
package, 128130
regular GIS data organization, 127128, 129
visualizing polylines and polygons, 108109
ModelE simulation data, 277
Modeling process, 270
data, 270271
density analysis, 271272
GIS algorithms and, 270
regression analysis, 272275
social media analysis, 270
Model synchronization, 254
Modern-Era Retrospective Analysis for Research and Applications (MERRA), 266, 268
Modules
ArcPy, 154155
Python, 124127
Monitor coordinate system, 8184
Multiband raster datasets, 196
Multilayer raster datasets, 187
Multiple assignments, 38
Multipoint, 170
Multithreading, 251
data loading with, 258
executing process, 252
loading multiple shapefiles concurrently using, 252253
multithreading. py file, 252
sequential and concurrent execution, 251
N
Namespace, 3839
Native byte order, 99
Native size and alignment, 99
Nested loops, 67
Nested model, 284285
NetCDF, see Network Common Data Form
NetCDF/HDF
application on climate study, 266269
raster data structure in, 265266
Network, 207
adjacency matrix, 209
directed networks, 207209
network representation in GIS, 209210
representation, 207
undirected networks, 207209
Network analysis types, 214
closest facility, 214, 215
location-allocation analysis, 217218
OD cost matrix, 216
routing, 214
service areas, 214215
VRP, 216217
Network Common Data Form (NetCDF), 190
Network data algorithms, 207; see also Surface data algorithms
finding shortest path, 210213
hands-on experience with ArcGIS, 218222
network analysis types, 214218
network representation, 207210
Node, 207
table, 210
nodeList, 268
Noncluster scheduling, 282283
Nondiscrete data, see Continuous data
Nonspatial attributes, 108
Numerical weather prediction model (WRF-NMM), 281
NumPy, 165166
NumPyArrayToFeatureClass, 165166
O
Object-oriented programming, 19
class and object, 2127
hands-on experience with Python, 30
point, polyline, and polygon, 2730
programming language and Python, 1921
Object Management Group (OMG), 1112
Objects, 9, 1214, 21, 35, 169
attributes, 2325
composition, 2627
describing and listing, 166169
generation, 23
inheritance, 2526
Octal number, 1920
OD cost matrix, see Origindestination cost matrix
Offset of record, 105106
OLS, see Ordinary least squares
OMG, see Object Management Group
Online mapping application, 5
Open-source
IDE, 115
packages, 153
Open() function, 68
Operating system (OS), 34
Operators, 5053
Ordinary least squares (OLS), 272275
result of analysis, 274
standard residual of, 274
Twitter users, 273
Organizing data, 89
Original pixel value, 195
Origindestination cost matrix (OD cost matrix), 216
OS, see Operating system
P
Package management, 131132
mini-GIS package, 128130
regular GIS data organization, 127128, 129
Parallel lines, 145
Parallel processing, 251, 281
and cluster, grid, and cloud computing, 253254
sequential and concurrent execution, 251
Parsing
coordinates, 89
data, 89
Pass statement, 63
Patterns, 77
Percent slope, 231
Performance-improving techniques, 247
disk access and memory management, 248251
hands-on experience with Mini-GIS, 257262
multithreading, 251254
parallel processing, 251254
problems, 247248
relationship calculation and spatial index, 254257
Perl, 19
Phosphors, 192
PHP, 19
Picture elements, see Pixels
Pixels, 185, 188, 192, 194, 201
values, 191192, 196197
PNG, see Portable Network Graphic
Point-based data, see Discrete data
Point, 1415, 8687, 170
class, 2730, 92
in polygon, 146148
PointGeometry, 170
Polygons, 11, 8687, 139, 170
area of polygon with hole(s), 140141
area of simple polygon, 139140
centroid, 138139
class, 2730
features, 229
function, 159
module, 128129
shapefiles, 109
visualizing, 108109
Polylines, 10, 8687, 170, 255
class, 2730, 259
length calculation, 89
length of, 142
point in polygon, 146148
visualizing, 108109
Polymorphism, 26
Portable Network Graphic (PNG), 191
Power parameter, 231
Private methods/attributes, 12
Professional GIS software, 45
Programming, 57
language, 1921, 35
structure match, 89
Programming thinking, 87
implement program, 8990
match programming language patterns and structure, 89
problem analysis, 88
think in programming, 88
Protected methods/attributes, 12
Public methods/attributes, 12
Pythagorean theorem, 141
Python, 68, 1921, 35, 79, 97, 99, 104, 106
automating ArcTools with, 158160, 161, 162
code, 109
data types, 4048
to draw line segments and calculate intersection, 148, 150
to draw polygon and calculate area of polygon, 148, 149
to draw polygon and calculate centroid, 148, 149
file access, 249
functions, 5456
GUI, 15, 36, 56, 77, 79, 115, 125
miscellaneous, 4850
object-oriented support, 3536
operators, 5053
program, 61, 69
programming with ArcPy in Python window outside ArcMap, 156157
statements, 53
syntax, 3639
Python, hands-on experience with, 1416, 56, 30, 70, 90, 148
create GIS objects and check intersection, 9295
input GIS point data from text file, 7475
I/O, create and read file, 7072
I/O, flow control, and file, 7273
longest distance between any two points, 70
reading, parsing, and analyzing text file data, 9092
Python IDE, 115124
debugging, 118, 120124
fonts setup for coding, 118, 119
general setting of programming window, 118, 119
highlighting, 117118
python programming windows, 115117
settings, 117
Python IDLE, 77, 90
Python language control structure
control structures, 6667
exceptions, 69
file input/output, 6769
hands-on experience with Python, 70
input GIS point data from text file, 7475
I/O, create and read file, 7072
I/O, flow control, and file, 7273
longest distance between any two points, 70
loops, 6466
making decisions, 6164
Python modules, 124
module introduction, 125
set up modules, 125126
system built-in modules, 126127, 128
Python programming, 153; see also ArcGIS programming
command-line GUI, 115, 116
file-based programming, 116117
hands-on experience with mini-GIS, 131134
interactive GUI, 115116
package management and mini-GIS, 127130
Python IDE, 115124
Q
QGIS software packages, 213
Quad tree, 188189
R
RAM, data loading with, 257258
random module, 127
Random. random() method, 95
range() function, 6465
Ranges of values process, 196197
Raster, 185
analysis, 196198
attribute information of raster dataset and calculating area, 200204
color renders, 198199
conversion between TIN and raster surface models, 229230
rendering, 194196
slope, 231
storage and compression, 186
Raster data, 185186, 265
analysis, 199200
models, 10
structure in NetCDF/HDF, 265266
Raster data algorithm
attribute information of raster dataset and calculating area, 200204
BMP, 190
color representation, 191194
GeoTIFF, 190
GIF files, 191
hands-on experience with ArcGIS, 198
IMG files, 190
JPEG files, 191
NetCDF, 190
PNG, 191
quad tree, 188189
Run Length Coding, 187188
SVG, 191
TIFF, 189
Raster data formats, 189
BMP, 190
GeoTIFF, 190
GIF files, 191
IMG files, 190
JPEG files, 191
NetCDF, 190
PNG, 191
SVG, 191
TIFF, 189
Raster storage and compression, 186
quad tree, 188189
Run Length Coding, 187188
Ray casting algorithm, 146, 147
readPolylineFile, 91, 93
ReadShapeFile module, 131132
Reclassification, 196197
Reclassify function, 201
Record content, 104
Record header, 104, 109
Rectangle centroid, 137138
Rectangle class, 92
Reduce method, 46
Reference point, 82
Regression analysis, 272275
Relationship, 12
bounding box in GIS, 255256
and spatial index, 254, 256257
RemapRange, 201
RemapValue, 201
Rendering, 194
Representational State Transfer (REST), 276277
REST, see Representational State Transfer
Return value, 68
RGB
Composite renderer, 195196
image, 191
values, 192
Root node, 189
Routing, 214
R-Tree, 256257
index, 261262
Run Length Coding, 187188
Run-time exceptions, 121122
S
Sample points, 229
Scalable Vector Graphics (SVG), 191
Scheduling methods, 282283
Scope, 39
SearchCursor, 160, 162163
Sequential and concurrent execution, 251
Sequential execution process, 251
Sequential match, 89
Service areas analysis, 214215
Set, 4748
SetParameterAsText functions, 174
SetParameter functions, 174
Shapefile handling
binary data manipulation, 97100
feature record, 104
general programming sequence for, 107108
hands-on experience with mini-GIS, 108113
index file structure, 105107
main file header, 102103
shapefile, 101107
Shortest path algorithm
brute force approach for, 211212
Dijkstra algorithm, 212213
finding, 210
problem statement, 210211
shutil module, 127
Sink, 234, 235
Slope, 231232
Slope generation, 239
Social media analysis, 270
Soft breaklines, 224
Software, 35; see also ArcGIS software
application, 34
development process, 63
embedded, 4
GIS, 97, 194, 197, 214
light-weight GIS, 45
Spatial attributes, 108
Spatial data structure, 265
application of NetCDF/HDF on climate study, 266269
logical structure of improved data model, 267
raster data structure in NetCDF/HDF, 265266
Spatial index, 256257
Spatial pattern, 255
Spatial relationship calculations using ArcGIS, 178180
calculate line intersection, 181
line intersection result, 182
point in polygon result, 184
railway stations in Virginia, 183
Spatiotemporal index, 269
Spatiotemporal thinking and computing, 280
cloud computing to support dust storm forecasting, 285
dust simulation and computing challenges, 280281
high-performance computing to support dust simulation, 281
to optimizing high-performance computing, 281284
WRF-NMM domain decomposition, 282
Special data models, 10
Spring Festival, 270
Standard size, 99
startByte, 268
Statements, 53
Statistics function, 159
Straight line segment, length of, 141
Stretched pixel value, 195
Stretched renderer, 194195
String format (String fmt), 98
Strings, 41
Struct, 97
handles, 97
module, 9798
package, 99
Surface, 223
Surface data, 223
continuous data, 225227
discrete data, 223225
model, 223
Surface data algorithms, 223; see also Network data algorithms
hands-on experience with ArcGIS, 236242
3D surface and data model, 223227
Surface data analysis, 230
aspect, 232234
direction coding, 235
elevation, 230231
flow direction example, 235
hydrologic analysis, 234236
slope, 231232
surface scanning window, 232
Surface model data creation, 228
conversion between TIN and raster surface models, 229230
grid surface model creation, 228
TIN surface model creation, 229
SVG, see Scalable Vector Graphics
Symbols, 77
Syntax, 36
case sensitivity, 36
indentation, 36
keywords, 37
multiple assignments, 38
namespace, 3839
scope, 39
special characters, 36, 37
SyntaxError, 120121
System architecture, distributed GIS, 276277
System built-in modules, 126127
check module, 128
math methods, 128
T
Tagged Image File Format (TIFF), 189
Taylor diagram, 278279
Thematic raster, 196
Three-dimensional space (3D space), 10
3D surface and data model, 223
surface data, 223
surface data model, 223227
TIFF, see Tagged Image File Format
TIN, see Triangulated irregular network
Tkinter, 79, 129
Traceback error message, 121
Transforming coordinate system, 80
determining ratio value, 8284
geographic area and window monitor size, 81
Translation, 19
Translator, 19
Trapezoid, 140
Triangle centroid, 137
intersection point of three medians, 138
Triangles, 229
Triangulated irregular network (TIN), 223, 226, 231
comparison of grid and, 226227
conversion between TIN and raster surface models, 229230
surface model creation, 229
triangles, 227
Try block, 122
tryexceptstatement, 69
Tuple data type, 46
Two-dimensional space (2D space), 10
U
Undirected networks, 207209
Unified Markup Language (UML), 7, 10, 11
class diagram, 12
class diagram and object diagram, 13
diagrams, 11, 12
object, 1214
OMG, 1112
Unique Values renderer, 196
Universal Transverse Mercator (UTM), 81
UpdateCursor, 164
User-defined function, 172, 174
User interface, distributed GIS, 277
correlation analyses for two variables, 278
GUI for Taylor diagram service, 279
Taylor diagram, 278279, 280
time series plotting, 278
UTM, see Universal Transverse Mercator
V
Variables, 4849
Vector data, 265
models, 10
visualizing, 8486
Vector data algorithms
area, 139141
centroid, 137139
hands-on experience with python, 148150
length, 141142
line intersection, 142146
point in polygon, 146148
Vector dataset, 101
Vector data visualization
create GIS objects and check intersection, 9295
determining ratio value, 8284
geographic area and window monitor size, 81
hands-on experience with Python, 90
implement program, 8990
match programming language patterns and structure, 89
point, polyline, polygon, 8687
problem analysis, 88
programming thinking, 87
reading, parsing, and analyzing text file data, 9092
think in programming, 88
transforming coordinate system, 80
visualizing GIS data, 7780
visualizing vector data, 8486
Vehicle routing problems (VRP), 216217
Vertex, 207
Vertical lines, 145146
Very Important Points algorithm (VIP algorithm), 229
Video resources, 177178
VIP algorithm, see Very Important Points algorithm
vis() function, 129
Visualization, 128129, 130
GIS data, 7780
vector data, 8486
VRP, see Vehicle routing problems
W
Walk function, 168
Warning messages, 176
WebGIS system, 277
while statement, 64
Windows, 34, 115
Windows Bitmap, 190
with statement, 163
Word processing, 3
WRF-NMM, see Numerical weather prediction model
write function, 68