Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ArcPy and ArcGIS

You're reading from   ArcPy and ArcGIS Automating ArcGIS for Desktop and ArcGIS Online with Python

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781787282513
Length 272 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Silas Toms Silas Toms
Author Profile Icon Silas Toms
Silas Toms
Dara OBeirne Dara OBeirne
Author Profile Icon Dara OBeirne
Dara OBeirne
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Python for ArcGIS FREE CHAPTER 2. Creating the First Python Script 3. ArcPy Cursors - Search, Insert, and Update 4. ArcPy Geometry Objects and Cursors 5. Creating a Script Tool 6. The arcpy.mapping Module 7. Advanced Analysis Topics 8. Introduction to ArcGIS Online 9. ArcPy and ArcGIS Online 10. ArcREST Python Package 11. ArcPy and ArcGIS Pro 12. ArcGIS API for Python

The basics of Python programming

Python has a number of language requirements and conventions, which allow for the control of modules and the structuring of code. Following are a number of important basic concepts which will be used throughout this book, and when crafting scripts for use with geospatial analysis.

To test these examples, open the IDLE (Python GUI) program from the Start Menu/ArcGIS/Python2.7 folder after installing ArcGIS for Desktop. It has a built-in "interpreter" or code entry interface, indicated by the triple chevron >>> and a blinking cursor. To create a script in IDLE to save your code, click on to the File menu and then click New File. Save any script with a .py extension. Otherwise, just enter commands into the interpreter and push Enter to execute or add the next line.

Import statements

Import statements are used to augment the power of Python by calling other modules for use in the script. These modules can be a part of the standard Python library of modules, such as the math module (used to do higher mathematical calculations), or, importantly, can be like ArcPy, which will allow us to interact with ArcGIS. Import statements can be located anywhere before the module is used, but, by convention, they are located at the top of a script.

There are three ways to create an import statement. The first, and most standard, is to import the whole module as follows:

import arcpy

Using this method, we can even import more than one module on the same line. Next, we will import three modules: arcpy, os (the operating system module), and sys (the Python system module):

import arcpy, os, sys   

The next method of importing a script is to import a specific portion of a module instead of importing the entire module using the from <module> import <submodule> syntax:

from arcpy import mapping

This method is used when only a portion of the code from ArcPy is needed; it has the practical effect of limiting the amount of memory used by the module when it is called. We can also import multiple portions of the module in the same fashion.

from arcpy import mapping, da

The third way to import a module is to write the from <module> import <submodule> syntax, but use an asterisk * to import all parts of the module as follows:

from arcpy import *

This method is still used, but it is discouraged as it can have unknown effects--the main one is that the names of the variables in the module might conflict with another variable in another module. For this reason, it is best to avoid this third method. However, lots of existing scripts include import statements in this format, so it is good to know that it exists.

Variables

Variables are a part of all programming languages. They are used to reference data objects stored in memory for using later in a script. There are a lot of arguments over the best method of naming variables. No variable standard has been developed for Python scripting for ArcGIS, so I will describe some common practices to use when naming variables here:

  1. Making them descriptive: Don't just name a variable, x; that variable will be useless later when the script is reviewed, and there is no way of knowing what it is used for, or why. They should be longer rather than shorter, and should explain what they do, or even what type of data they hold. For example:
shapefilePath = "C:/Data/shapefile.shp"
  1. Using CamelCase to make the variable readable: Camel case is a term used for variables that start with a lowercase letter but have uppercase letters in the middle, resembling a camel's hump. For example:
camelCase = 'camel case is twoWords stuck together like this'
  1. Using an underscore to separate parts of the name: This makes the name longer, but adds some clarity when reading the variable name, like this:
location_address = '100 Main St'
  1. Including the data type in the variable name: If the variable contains a string, call it variableString or variable_string. This is not standard, and will not be used in this book, but it can help organize the script, and is helpful for others who will read these scripts. Python is dynamically typed instead of statically typed, a programming language distinction, which means that a variable does not have to be declared before it can be used, unlike Visual Basic or other statically typed languages. For example:
variableString = 'this is a string'

For loops

Built into all programming languages is the ability to iterate over a dataset to perform an operation on the data, thus transforming the data or extracting data that meets specific criteria. The dataset must be iterable to be used in a for loop. We will use iteration in the form of for loops throughout this book. Here is a simple example of a for loop, which takes string values and prints them in uppercase using the string upper method. Open IDLE (Python GUI) from the Start Menu/ArcGIS/Python2.7 folder to try a for loop. Enter commands at the Python interpreter's triple chevron >>> :

>>> newlist = ['a','b','c','d']
>>>for value in newlist:
print value.upper()
A
B
C
D

If/Elif/Else statements

Conditional statements, called if...else statements in Python, are another programming language standard. They are used when evaluating data; when certain conditions are met, one action will be taken (the initial if statement); if another condition is met, another action is taken (this is an elif statement), and if the data does not meet the condition, a final action is assigned to deal with those cases (the else statement). They are similar to a conditional in an SQL statement used with the Select Tool in ArcToolbox. Here is an example using an if...else statement to evaluate data in a list. In the example, within the for loop, the modulo operator % produces the remainder of a division operation. The if condition checks for no remainder when divided in half, a elif condition looks for remainder of two when divided by three, and the else condition catches any other result, as shown:

>>> data = [1,2,3,4,5,6,7]
>>> for val in data:
if val % 2 == 0:
print val,"no remainder"
elif val % 3 == 2:
print val, "remainder of two"
else:
print "final case"


final case
2 no remainder
4 no remainder
5 remainder of two
6 no remainder
final case

While statements

Another important evaluation tool is the while statement. It is used to perform an action while a condition is true; when the condition is false, the evaluation will stop. Note that the condition must become false, or the action will be performed forever, creating an "infinite loop" that will not stop until the Python interpreter is shut off externally. Here is an example of using a while loop to perform an action until a true condition becomes false:

>>> x = 0
>>> while x < 5:
print x
x+=1
0
1
2
3
4

Comments

Comments in Python are used to add notes within a script. They are marked by a pound sign, and are ignored by the Python interpreter when the script is run. Comments are useful for explaining what a code block does when it is executed, or for any other helpful note that a script author would like to make for future script users:

#This is a comment
You have been reading a chapter from
ArcPy and ArcGIS - Second Edition
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781787282513
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image