programming arcgis with python workshop -...
TRANSCRIPT
![Page 1: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/1.jpg)
Programming ArcGIS with Python Workshop -Introduction
E-Learning for the GIS Professional – Any Time, Any Place!geospatialtraining.com
![Page 2: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/2.jpg)
Course Outline• Chapter 1: Python Basics for GIS Programming
• Chapter 2: Managing Map Documents and Layers
• Chapter 3: Finding and Fixing Broken Data Links
• Chapter 4: Automating Map Production and Printing
• Chapter 5: Executing Geoprocessing Tools from Scripts
• Chapter 6: Creating Custom Geoprocessing Tools
• Chapter 7: Querying and Selecting Data
2
![Page 3: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/3.jpg)
Chapter 1: Python Basics for GIS Programming
E-Learning for the GIS Professional – Any Time, Any Place!geospatialtraining.com
![Page 4: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/4.jpg)
Module Outline
• 7 Reasons to Learn How to Program with Python
• Creating Variables and Assigning Data
• Basic Python Statements
• Creating and Using Functions
• Advanced Python Data Structures (Lists, Tuples, Dictionaries)
• Advanced Python Data Structures (Classes and Objects)
4
![Page 5: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/5.jpg)
7 REASONS TO LEARN HOW TO PROGRAM WITH PYTHON
5
![Page 6: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/6.jpg)
Outline
1. Python is an easy language to learn and use
2. Many Python libraries for GIS professionals
3. Automate your common, routine geoprocessing tasks
4. Huge standard library and many open source libraries
5. Money, money, money (huge demand for GIS programmers who know Python)
6. Can do a lot of work with very few lines of code
7. It’s free!
6
![Page 7: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/7.jpg)
Python is an Easy Language to Learn
• Gradual learning curve
• Python code is easy to read and understand
• Requires far fewer lines of code than other language to accomplish tasks
• Great documentation
• Language structure is easy to learn
– No arcane syntax like in other languages
python.org7
![Page 8: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/8.jpg)
Many Python Libraries for GIS Professionals
• Python is one of the key languages for GIS programming
• Learn Python and you can implement geoprocessing tasks across a wide array of GIS
software
– Desktop and web
• arcpy, arcpy.mapping, arcpy.da for ArcGIS Desktop
• PyQGIS for QGIS
• GeoServer, PostGIS, Mapnik, Geos, PROJ.4
• shapely, fiona, simplekml
• GeoDjango (web)
• Also integrates well with C++ code
– OGR/GDAL
8
![Page 9: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/9.jpg)
Automate Your Common, Routine Geoprocessing Tasks
• Many GIS tasks are routine and monotonous
– Most can be automated with Python and GIS software
and/or libraries
– Python scripts can be scheduled to run at convenient
times
• Automation decreases errors and frees up your time for
higher level work
– Wouldn’t you rather work on analysis tasks than the
same old monotonous tasks?
9
![Page 10: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/10.jpg)
Huge Standard Library and Many Open Source Libraries
• Standard library
– Text processing
– File and Directory processing
– Data persistence
– Data compression and archiving
– Operating system services
– Internet data handling and networking
– GUI
– Error handling and debugging
– Much more
10
![Page 11: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/11.jpg)
Huge Standard Library and Many Open Source Libraries
• Open Source Libraries
– Web applications and frameworks
– Graphics frameworks
– UI frameworks
– Scientific packages
– Math libraries
– XML, CSV, JSON parsers
– Image processing
– Networking
– Database processing
11
![Page 12: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/12.jpg)
Money, Money, Money
• Separate yourself from the pack
• Most GIS Analysts and Technicians don’t have programming
skills
• Demand for competent GIS programmers far exceeds the
supply
• Supply/demand differential leads to high salaries for people
with good programming skills
• Python is the perfect language to get started
– Easy to learn so a great entry language
– Primary GIS software packages use Python
– Can be used for desktop and web application development
12
![Page 13: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/13.jpg)
Can Do a Lot of Work with Very Little Code
• Python is very succinct – Requires very few lines of
code to do a lot of work
• Designed that way
• Doesn’t use a lot of the syntax that clutters other
languages
– No curly braces to define code blocks
– No semi-colons to end lines
– Structured more like a natural language
13
![Page 14: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/14.jpg)
It’s Free
• Open source programming language
• Comes with a development environment (IDLE)
• Some development environments are also free to download
– PyScripter
• Distributed with ArcGIS and QGIS
• Documentation is very good
14
![Page 15: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/15.jpg)
CREATING VARIABLES AND ASSIGNING DATA
15
![Page 16: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/16.jpg)
Overview
• What are variables?
• How to create variables in Python
• How to name your variables
• Variable casing
• What kinds of data can be assigned to variables
16
![Page 17: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/17.jpg)
What are Variables?
• Can think of variables as an “Empty Box” for storing values
• Named area in memory used for storing data
• Data can change as the program is running
21“John Doe”[“Hydrants”,
“Mains”, “Valves”]
Map objectTrue
(“Monday”, “Tuesday”,
“Wednesday”)
17
![Page 18: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/18.jpg)
How to Create Variables in Python
• Variables have a name
– You define the name for variables
– Must follow specific rules
• All variables must start with a letter or underscore
• Variables can’t start with numbers or any special characters
– Correct
• Map, map, myMap, _initExtent, my_map
– Incorrect
• 2Map, %Map, @map
18
![Page 19: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/19.jpg)
How to Create Variables in Python• Syntax for assigning variables
<variable name> = <data>
• Examples
myName = “Eric”
extent = Map.extent
ssn = “457-32-3567”
myAge = 21
• Python does not require a semi-colon to end a variable assignment like other languages
– extent = Map.extent (Python)
– extent = Map.extent; (JavaScript)
• Python does not require the keyword var to begin a variable assignment
– var extent = Map.extent; (the var keyword is not needed in Python)
19
![Page 20: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/20.jpg)
Python Variables Data Types are Interpreted
• Most languages require that you define the type of data that will be assigned to a
variable
– Python does not require this
• Example
– C# .NET
int aTouchdown = 6;
– Python
aTouchdown = 6
• The Python interpreter will figure out the type of data being assigned to a variable
– Known as an interpreted language
– .NET, Java are compiled languages
20
![Page 21: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/21.jpg)
Variables are Case Sensitive in Python
• Python is a case sensitive language
mapsize = “24x34”
MapSize = “24x34”
• Python sees this as two completely different variables due to the casing
• One of the most common mistakes made by beginners is to reference variable in a
different case than how they were created.
21
![Page 22: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/22.jpg)
What Kinds of Data Can be Assigned to a Variable?
• Strings
• Numbers
• Booleans
• Lists
• Dictionaries
• Tuples
• Objects
22
![Page 23: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/23.jpg)
Strings
• Surrounded by single or double quotes
• Any number of characters and can include numbers
• Example:
myName = “Eric”
mySSN = “467-35-3567”
myAge = “21”
featureClassName = ‘Roads’
polyCentroid = ‘-95.4567, 34.6468’
23
![Page 24: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/24.jpg)
Manipulating Strings
• String Concatenation
– Strings can be concatenated through the use of the “+” operator.
s1 = “c:\\data\\floodplain”
s2 = “.shp”
theShp = s1 + s2
Result = “c:\\data\\floodplain.shp”
• String Repeating
– Strings can be repeated by using the “*” operator.
s1 = “CensusTract”
s2 = s1 * 2
Result = “CensusTractCensusTract”
24
![Page 25: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/25.jpg)
Manipulating Strings
• String Equality
– Strings can be tested for equality using the “==“ operator.
s1 = “hello”
s2 = “hello”
s3 = “hello world”
s1 == s2 // returns true
s2 == s3 //returns false
• String Containment
– Strings can be tested for containment using the “in” operator which returns true if the first operand is contained in the second.
x = “floodplain.shp”
y = “.shp”
print y in x
Result = true //.shp is “in” floodplain.shp
25
![Page 26: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/26.jpg)
String Indexing
• Python strings are an ordered collection of characters.
– This means that we can access the characters by position to pull out a single character or a series of characters.
– We do this in Python through indexing.
• Strings are zero based.
– The first character in a string occupies the 0 position, while the next character occupies the 1 position. Each successive character occupies the next numeric value (2,3,4,etc)
– Example:
fc = “Floodplain.shp”
fc[0] = “F”
fc[10] = “.”
fc[13] = “p”
26
![Page 27: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/27.jpg)
String Indexing
• Characters in a string are obtained by providing the numeric offset contained within square brackets after a string.
– fc[0]
– fc[4]
• Can use negative offsets to search backward.
Fc = “Floodplain.shp”
fcNew = Fc[-4] ##Returns “.”
27
![Page 28: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/28.jpg)
String Slicing
• String slicing can be used to extract a contiguous sequence of strings.
• The format is similar to indexing, but with the addition of a second offset.
• Example:
theString = “Floodplain.shp”
– theString[0:5]
• Returns: “Flood”
• Extracts from offset 1 up to, but not including, offset 5
– theString[1:]
• Returns: “loodplain.shp”
• Extracts characters starting from offset 1 to the end of the string
– theString[:-1]
• Returns: “Floodplain.sh”
• Extracts characters from 0 up to, but no including the last item
28
![Page 29: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/29.jpg)
Common String Functions
• There are many built in string functions that can be used to manipulate strings
– isalnum, isalpha, isdigit, islower, isupper, istitle, isspace
• These functions return a True/False value
• Example:
S1 = “floodplain.shp”
S1.isalnum() ##returns false (punctuation)
S1.isupper() ##returns false (not upper case)
S1.islower() ##returns true (is lower case)
S1.isalpha() ##returns true (alphabetic characters)
– title, upper, lower, swapcase, capitalize
• Returns the string converted to title, upper case, lower case, inverted case, and capitalized respectively.
• Example:
S1 = “floodplain.shp”
S1.title() ##returns “Floodplain.Shp”
S1.upper() ##returns “FLOODPLAIN.SHP”
S1.lower() ##returns “floodplain.shp”
S1.swapcase() ##returns “FLOODPLAIN.SHP”
S1.capitalize() ##returns “Floodplain.shp” 29
![Page 30: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/30.jpg)
Common String Functions• strip, rstrip, lstrip
– Returns a string with leading and trailing white space removed (strip), trailing white space removed (rstrip), and leading white space removed (lstrip)
– Example:
strZip = “ Zipcode: 97865 “
strZip.strip() ##returns “Zipcode: 97865”
strZip.rstrip() ##returns “ Zipcode: 97865”
strZip.lstrip() ##returns “Zipcode: 97865 “
• find
– Returns the index of the first found occurrence of the given string. If it is not found, find returns -1
– Example:
strFC = “Tracts.shp”
strFC.find(‘.’) ##Returns 6
• replace
– Returns a copy of the string with each occurrence of the first parameter replaced by all occurrences of the second parameter
– Example:
strShp = “Census Tracts”
strShp.replace(“Tracts”, “Blocks”) ##Returns “Census Blocks”
30
![Page 31: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/31.jpg)
Common String Functions
•split– Returns a list of words in a string.– Example:
strShp = “USA Census Tracts”
strShp.split()
##returns [“USA”, “Census”, “Tracts”]
• len– Returns the length of a string.– Example:
strShp = “State of Michigan”
##returns 17
31
![Page 32: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/32.jpg)
Numbers
• Variables can also hold numbers.
• Python supports the following numeric data types: int, long, float, and complex.
• Numbers are assigned to variables without the use of quotes.
• Example:
– myNumber = 21
– myString = “21”
32
![Page 33: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/33.jpg)
Numeric Operators
• Python supports a typical set of numeric operators including:
– Addition (+)
– Subtraction (-)
– Multiplication (*)
– Division (/)
– Remainder/Modulus (%)
33
![Page 34: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/34.jpg)
Built-In Numeric Functions
• These numeric functions are provided as part of the basic
Python language.
• These functions are always available.
• Advanced functionality available in the math module.
• abs(x) – Returns the absolute value of a number.
• int(x) – Converts a string to an integer.
• long(x) – Converts a string to a long.
• round(x[,n]) – Returns the floating point value x round to n
digits after the decimal point.
34
![Page 35: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/35.jpg)
Math Module• Provides access to variety of mathematical functions.
• Must import the math module to use these functions.
– Example:
>>> import math
>>> math.ceil(100.25)
• Most return float values
• Common functions
– ceil(x) – returns the ceiling of x as a float, the smallest integer value greater than or equal to x
– floor(x) – returns the floor of x as a float, the largest integer value less than or equal to x
– fabs(x) – returns the absolute value of x
– Many others
• Trigonometric functions
– acos(x), asin(x), atan(x), cos(x), hypot(x,y), sin(x), tan(x)
• Power and logarithmic functions
– exp(x), log(x[,base]), log10(x), pow(x,y), sqrt(x)
• Angular conversion
– degrees(x), radians(x)
• Hyperbolic functions
– cosh(x), sinh(x), tanh(x)
35
![Page 36: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/36.jpg)
BASIC PYTHON STATEMENTS
36
![Page 37: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/37.jpg)
Webinar Outline
• What are statements?
• print statement
• if/else statements
• Looping statements
– for
– while
37
![Page 38: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/38.jpg)
What are statements?
• Each line of code that you write is a statement
• Many types
– Variable assignment
– Importing modules
– Print statements
– Decision support statements (if/elif/else)
– Looping statements
– Function calls
38
![Page 39: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/39.jpg)
Basic Statements
• Statements are the lines of code you write in Python that tell it what to do.
• Python has many statements
– print – Sends output to the interactive window.
– import – Imports a module.
– Variable assignment statement
• “=“
– Decision statement
• if…elif…else
– Looping statements
• for…in
• while
– Many other statements
• break, continue
• try, except
39
![Page 40: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/40.jpg)
Print Statement
• Sends output to the shell window
• print <statement>
40
![Page 41: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/41.jpg)
Decision Statements
• if…elif…else
• Primary decision making statement in Python
• Tests for a True condition
• Enable you to control the flow of your programs, and make decisions
• Example decisions that you can make in your code
– If the variable holds a point feature class, get the X,Y coordinates.
– If the feature class name equals “Roads” get the “Name” field.
• Otherwise get the “Value” field
41
![Page 42: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/42.jpg)
Decision Statements
• Syntax
– Colons at the end of each condition
– Two equal signs to test for conditions
– Indentation required
– To finish a condition, un-indent your code
if <test1>:
<statements>
elif <test2>:
<statements>
else:
<statements>
Example
42
![Page 43: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/43.jpg)
Python Syntax Rules
• Statements execute one after another, until your code branches.
– Branching typically occurs through decision statements (if…elif… else)
– Also occurs through looping structures (for, while).
• Python detects statement and block boundaries automatically.– No braces or delimiters around blocks of code.
– Indentation is used to group statements in a block.
– Statements are not terminated with a semicolon; end of line character marks the end of a statement.
• Compound statement include a “:” character– Compound statements follow the pattern: header line terminated by a
colon
– Next statements follow an indented pattern. These are called blocks.
• Spaces and comments are ignored
43
![Page 44: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/44.jpg)
Python Indentation
• Statement indentation deserves special mention as it is critical to the way Python interprets code.
• As we mentioned, code blocks are detected by Python through indentation.
– All statements indented the same distance belong to the same block of code until that block is ended by a line less indented.
• Example:
44
![Page 45: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/45.jpg)
Looping Statements
• Looping allows your program to repeat lines of code over and over as needed
• 2 Looping structures in Python
• For loops execute a block of statements a predetermined number of times
• While loops repeatedly execute a block of statements while a test at the top of the loop evaluates to true
For While
45
![Page 46: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/46.jpg)
While Loops
• Repeatedly execute a block of code
– As long as test at top of loop is ‘True’
• When test evaluates to ‘False’, Python breaks out of the loop
– Continues processing with the first line below loop
• Need something in the code block that will eventually set the test to ‘False’
– Otherwise you get an endless loop
• Syntax:
while <test>:
<statements1>
Example
46
![Page 47: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/47.jpg)
For Loops
• For loops can be one of two types:
– Counted Loops
• Increment and test a variable on each iteration of the loop
• When variable reaches a pre-determined number the loop exits
– List Loops
• Iterate over each value in a list
• The loop executes once for each value and then stops looping
• More common type of for loop
1 to 10
[‘Hydrants’, ‘Mains’, ‘Valves’]
47
![Page 48: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/48.jpg)
For Loops
• Syntax:
for <target> in <object>:
<statements>
• Items in the <object> are assigned to the target, one by one during execution of the loop.
– <target> is a dynamic variable Example
48
![Page 49: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/49.jpg)
• There are times when you may need to break the execution of a loop
• Break statement– Jumps out of the closest enclosing loop
• This statement can appear anywhere in the block of code inside a loop
• Syntax:
while <test>:
<statements>
if <test>: break ## exit loop now
Break Out of a Loop
49
![Page 50: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/50.jpg)
CREATING AND USING FUNCTIONS
50
![Page 51: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/51.jpg)
Outline
• What are Functions()
• Calling Functions
• Passing Parameters to a Function
• Using Parameters Inside a Function
• Creating a Function
• Naming a Function
• Returning a Value from a Function
51
![Page 52: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/52.jpg)
What are Functions?
• Groups of statements that can be executed as one
• Server two primary roles
– Code reuse
– Break down tasks into smaller sections
• Functions have
– A name
– Can be passed parameters
– Can return a value
52
![Page 53: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/53.jpg)
Structure of a Function
1. Use ‘def’ to define a
function
2. Functions are given a
name
3. Can pass parameters to
a function
4. Execute a block of code
5. Can return a value
53
![Page 54: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/54.jpg)
Calling a Function
• Functions can be called or executed from any other statement including another
function
54
![Page 55: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/55.jpg)
Passing Parameters to a Function
• When calling a function you often pass parameters to the function
• These parameters become variables inside the function and are used in some way
55
![Page 56: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/56.jpg)
Variables Inside Functions
• Variables created inside functions are local to that function
– Can’t be accessed outside the function
• Unless returned by the ‘return’ statement
56
![Page 57: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/57.jpg)
Creating a Function
• Use the ‘def’ statement along with a name and optional parameters
• Compound statement (use the colon : at the end of the list of parameters)
– All statements that are part of the function should be indented
• Function names follow regular variable naming conventions
– Start with a letter or underscore
• Value can be returned using the ‘return’ statement and a value
• Syntax:
def <name>(arg1, arg2,….,argN):
<statements>
return <value>
57
![Page 58: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/58.jpg)
Function Naming
• Function names follow regular variable naming conventions
– Start with a letter or underscore
• Don’t start a function with a number or special character
• Should be named according to the action they perform
– Don’t just randomly name your functions!
58
![Page 59: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/59.jpg)
Returning a Value from a Function
• Use the ‘return’ statement to return a result to the calling function or statement
• You don’t have to return a value; this is optional
59
![Page 60: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/60.jpg)
ADVANCED PYTHON DATA STRUCTURES
(LISTS, TUPLES, DICTIONARIES)
60
![Page 61: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/61.jpg)
Outline
• Lists
• Tuples
• Dictionaries
61
![Page 62: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/62.jpg)
PYTHON LISTS
62
![Page 63: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/63.jpg)
What are Lists?
• Python data type for storing multiple data values
• Data values can be of any type: strings, numbers, other lists, dictionaries, tuples, objects
• Lists are ordered
– Each item in the list occupies a position referenced by an index number
• Assigned to variables
• Can grow and shrink and can be sorted
• Example:
63
![Page 64: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/64.jpg)
Creating and Accessing Data in a List
• Creating a list
– Use square braces
– Assign data inside the braces and separate values with a comma
– Lists assigned to variables like any other data type
• Accessing a list
– Reference the list variable and index number
– Slicing works as well – returns a new list
64
![Page 65: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/65.jpg)
Basic List Operations
• Get number of items in a list. Use len() function
len(listOfValues)
• Concatenate lists. Use the + operator
listOfValues + anotherListOfValues
• Add an item to a list. Use the append() method
listOfValues.append(‘cities’)
• Remove an item from a list
del listOfValues[1]
• Sort a list
listOfValues.sort()
listOfValues.reverse()
65
![Page 66: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/66.jpg)
Changing List Contents
• Python supports changing the contents of a list.
• Doesn’t force you to make a copy of the list.
• Changes to the contents of a list can be done by index or slice
– Index Example:
>>> fcList = [‘Roads’, ‘Railroads’, ‘Airports’]
>>> fcList[1] = ‘Subways’
Result: [‘Roads’,’Subways’,’Airports’]
– Slice Example:
– Remember that slicing operates up to, but not including the second offset.
>>> fcList = [‘Roads’, ‘Railroads’, ‘Airports’]
>>> fcList[0:2] = [‘Bus Stops’, ‘Subways’]
Result: [‘Bus Stops’, ‘Subways’, ‘Airports’]
66
![Page 67: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/67.jpg)
PYTHON TUPLES
67
![Page 68: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/68.jpg)
What are Tuples?
• Similar to lists in the following ways
– Store a sequence of data - can be any type of data
– Accessed by offset.
– Ordered. Each value occupies an index position – Indexing and slicing can be used
• Differences?
– Defined with parentheses vs. square brackets
(2008, 2, 5, 11, 55, 34, 1, 36, 0)
– Values can’t be changed
– Tuples can’t grow and shrink (lists can)
• This can be good! Sometimes you want data to occupy a specific position each time
Data from fields in table and feature classes in cursors
68
![Page 69: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/69.jpg)
Creating Tuples
• Creating a tuple
– Use parentheses braces
– Assign data inside the parentheses and separate values with a comma
– Tuples assigned to variables like any other data type
• Access items by index number
streets = tupleValues[0]
• Tuples can be nested
wildfireValues = ( ‘Bastrop’, ‘Y’, 3000, (-95.234, 31.895 ) )
69
![Page 70: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/70.jpg)
PYTHON DICTIONARIES
70
![Page 71: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/71.jpg)
What are Dictionaries?
• Similar to a traditional dictionary on your bookshelf
– Look up a term (key) - a value for that terms is defined
• Unordered collection of data
– Can’t access values using an index value. Unlike lists and tuples.
• Items stored and fetched using a ‘key’ value
• Can grow and shrink
71
![Page 72: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/72.jpg)
Creating a Dictionary
• Defined inside a set of curly braces
• Create a set of key/value pairs separated by a colon
• Each set of key/value pairs separated by a comma
• Assigned to a variable
• Syntax{Key:Value, Key:Value, ……}
72
![Page 73: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/73.jpg)
Accessing Values in a Dictionary
• Values accessed using a key
73
![Page 74: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/74.jpg)
Basic Dictionary Operations
• Get an value using the key
– dFires[‘FireName’]
• Determine if a key exists
– dFires.has_key(‘Acres’) ##Returns a True/False
• Get a list of keys
– dFires.keys()
• Get a list of values
– dFires.values()
74
![Page 75: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/75.jpg)
Changing Dictionary Contents
• Dictionary objects can be changed, expanded, and shrunk in place.
• Assign a value to a key.
• Example:
dFires[‘Acres’] = 4000
• Slicing does not work with dictionaries since it is an unordered list.
• You can use the keys() method to iterate over a list of all objects in a dictionary.
– Example:
listKeys = dFires.keys()
for x in listKeys: print x
75
![Page 76: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/76.jpg)
USING ADVANCED PYTHON DATA STRUCTURES
(CLASSES AND OBJECTS)
76
![Page 77: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/77.jpg)
Outline
• What are Classes and Objects?
• Creating Objects
• Objects Properties
• Object Methods
• Accessing Properties and Methods
– Using Dot Notation
77
![Page 78: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/78.jpg)
What are Classes and Objects?
• Fundamental concepts in object oriented programming
• Python supports object oriented programming
• Classes are blueprints for creating objects
– Classes are abstract representations
used to create objects
• Each object has properties and methods
defined by the class
– Each object can define different properties and call methods
• Most objects you use will come from an existing Python library
– arcpy, arcpy.mapping, ogr, gdal
– You can also create your own
78
![Page 79: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/79.jpg)
Objects
• Created from abstract classes
– Methods for creating an object
• Constructor (method)
• From a property or method on another object
• Objects have
– Properties (data)
– Methods (actions)
• Assigned to variables
ext = polygon.Extent
79
![Page 80: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/80.jpg)
Creating Objects
• Objects created from classes in two basic ways
1. Constructor
– Specific type of method used to create an object
– Example:
mxd = arcpy.mapping.MapDocument(r”c:\Data\Crimes.mxd”)
2. Using properties or methods on other objects
– sr = extent.SpatialReference
80
![Page 81: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/81.jpg)
Object Properties
• Objects can have data associated with them
• Used to get descriptive information about an object
• Example:
– MapDocument object in arcpy.mapping
• Properties
– title
– description
– pageSize
– author
– activeDataFrame
81
![Page 82: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/82.jpg)
Object Methods
• Actions that an object can take
• Look like functions
– Have parentheses at the end of the name
– Can pass parameters into the methods
• Examples
– mxd.save()
– mxd.saveACopy(r”c:\Data\CrimeCopy.mxd”)
82
![Page 83: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/83.jpg)
Accessing Properties and Methods
• Dot Notation
– object.<property> or object.<method>
– MapDocument.title
– MapDocument.save()
83
![Page 84: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/84.jpg)
Creating Your Own Classes
• Can also create your own classes
• Most of the time you’ll be using classes already created in other Python libraries
• Complex subject
84
![Page 85: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/85.jpg)
Chapter 2: Managing Map Documents and LayersCopyright 2015: GeoSpatial Training Services, LLC
![Page 86: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/86.jpg)
Chapter Outline• Introduction to Arcpy Mapping
• Referencing a map document
• Arcpy list functions
• Working with data frames
• Adding layers to a map document
• Inserting layers in a map document
• Updating layer symbology
• Updating layer properties
• Working with time enabled layers in a data frame
86
![Page 87: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/87.jpg)
INTRODUCTION TO ARCPYMAPPING
87
![Page 88: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/88.jpg)
What is ArcPy.Mapping?
• Python scripting library for
– Managing map documents (.mxd)
– Managing layer files (.lyr)
– Managing data within .mxd and .lyr files
– Finding and fixing broken data links
– Automate exporting and printing map documents
– Automate map production and creation of PDF map books
– Publish tasks to ArcGIS Server
• Easy to use scripting environment for the GIS Analyst
88
![Page 89: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/89.jpg)
Where ArcPy.Mapping Fits
arcpy
core sa da mapping
89
![Page 90: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/90.jpg)
Referencing the Map Document
• Need to get an instance of the map document before performing any operations
• Code example:
– Gets the currently running document
– ArcMap should be running
– Document conflicts can be present
– Point to a map document at a specific location
90
![Page 91: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/91.jpg)
ARCPY MAPPING LIST FUNCTIONS
91
![Page 92: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/92.jpg)
List Functions
• All list functions return a Python list
• Normally used as part of a multi-step process where creating a list is only the first step
– Example:
• Finding and repairing broken links
• First create a list of all layers with broken links
• Then iterate through the list of layers with broken links and fix each one
92
![Page 93: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/93.jpg)
Listing Layers in a Map Document
• ListLayers(mxd) returns a Python list object
– All layers within an mxd, data frame in an mxd, or layer (.lyr) file
• Can iterate list with ‘for’ loop
• Syntax
93
![Page 94: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/94.jpg)
List Table Views
• ListTableViews() returns Python list of TableView objects in a map document
• Can use a wildcard on the name property
• Syntax
94
![Page 95: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/95.jpg)
Listing Data Frames
• ListDataFrames() returns a Python list of data frames in a map document file
• Use index value to access individual data frame
• Use wildcard to filter data frames returned
• Code example below print out name of each data frame
95
![Page 96: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/96.jpg)
WORKING WITH DATA FRAMES
96
![Page 97: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/97.jpg)
DataFrame Object
• DataFrame object references individual data frames in ArcMap table of contents
• Can be used to set map extent, scale, rotation, spatial reference, zoom to selected
features
• Can also be positioned or sized in the layout view
• Used as an optional object for printing and exporting
97
![Page 98: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/98.jpg)
Zooming to Selected Features with DataFrame
• Use DataFrame.zoomToSelectedFeatures property to zoom to the extent of all selected
features from all layers in data frame
– Zooms to full extent of all layers if no features are selected
• Can also use result of Layer.getSelectedExtent() to zoom to extent of selected features
for a specific layer
– Returns Extent object that is passed to df.extent
98
![Page 99: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/99.jpg)
Changing the Map Extent
• Use DataFrame.Extent to set the map extent
• Use an Extent object as input to the property
99
![Page 100: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/100.jpg)
Working with Time Enabled Layers
• Layers can include time information
• Data Frame can be time enabled if at least one layer has time information
– Use Time tab on Layer Properties to time enable
100
![Page 101: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/101.jpg)
Working with Time Enabled Layers
• Use the Time Slider button on the ArcMap toolbar to display the Time Slider Options
dialog.
101
![Page 102: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/102.jpg)
DataFrameTime Object
• Accessed through DataFrame.time property
• Properties for retrieving
– Current time
– End time
– Start time
– Time step interval
• These times were set with the Time Slider Options and saved
102
![Page 103: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/103.jpg)
MANIPULATING LAYERS
103
![Page 104: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/104.jpg)
Adding Layers
• Can add layers or group layers in map document
• Can use “auto-arrange” to place layer in data frame for visibility
– Like existing “Add Data” button
– Based on layer weight rules and geometry type
• CAN NOT add layers to a layer file (.lyr)
104
![Page 105: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/105.jpg)
Adding Layers
• Layer added must reference existing layer
– Layer file (.lyr) on disk
– Same map document and data frame
– Same map document different data frame
– Separate map document
• Must first create an instance of ‘Layer’ and then call ‘AddLayer()’ function passing in this
instance.
105
![Page 106: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/106.jpg)
Adding Layers
106
![Page 107: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/107.jpg)
Adding a Layer to GroupLayer
• AddLayerToGroup() used to add a layer to a group layer within a map document
• Layer can be added
– To the top or bottom of the group layer
OR
– Using auto-arrange
• Can also add layers to an empty group layer
• CAN NOT add to group layers within .lyr file
• Syntax
107
![Page 108: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/108.jpg)
Removing Layers
• RemoveLayer() removes a layer from a data frame
– Single layer or group layer from specific data frame
• If two layers have same name
– Only first is removed unless script setup to iterate
108
![Page 109: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/109.jpg)
Inserting Layers
• Allows for more precise positioning of a new layer into a data frame or group layer
• Uses a reference layer to specify location
• Layer added before or after reference layer
• CAN NOT add layers to an empty data frame or empty group layer
109
![Page 110: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/110.jpg)
Moving Layers
• MoveLayer() provides the ability to reposition the layer within a data frame or group
layer
• Must be in the same data frame
– Can’t move a layer from one data frame to another
• Uses a reference layer
• Very similar to InsertLayer() with the exception that the layer already exists in data
frame
110
![Page 111: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/111.jpg)
Updating Layers
• UpdateLayer() can update layer properties or just symbology for a layer
• Extracts information from a source layer
– Source layer is either
• Layer file (.lyr)
• Layer within a map document
• Limited number of properties
• Default behavior is updating symbology
111
![Page 112: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/112.jpg)
Updating Layers - Symbology
• Default behavior of UpdateLayer()
• Syntax
• update_layer and source_layer must have same geometry type
• Attribute definitions also need to be same depending upon the renderer
– Graduated color based on particular attribute
112
![Page 113: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/113.jpg)
Updating Layers – Layer Properties
• Can update all or specific layer properties
– Field aliases, selection symbology, query definitions, label fields, etc
• Common scenario is to have a layer in many map documents that needs to have specific
property updated in each map document
– How to do this?
• Use ArcMap to modify the layer with appropriate properties and save as layer file (.lyr)
• This layer file then used as source_layer to update properties
113
![Page 114: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/114.jpg)
Exercise
• Please complete the exercises from Chapter 2 in the Programming ArcGIS 10.1 with
Python Cookbook
114
![Page 115: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/115.jpg)
Chapter 3:Finding and Fixing Broken Data LinksCopyright 2015: GeoSpatial Training Services, LLC
![Page 116: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/116.jpg)
List Broken Data Sources
• Data sources can become broken or simply need to be redirected to a new source
• ListBrokenDataSources()
– Returns Python list of Layer objects
– Each Layer is a broken data connection
• Use ‘for loop’ to iterate through each layer and fix
• Syntax
116
![Page 117: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/117.jpg)
Fixing Broken Data Sources
• Need to make same data source change across many map documents?
– Can be an overwhelming task
– Process can be automated without opening any map documents
• Changes can be made to
– Individual layers
– All layers in common workspace
117
![Page 118: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/118.jpg)
Fixing Broken Data Sources
• Some definitions before getting started
– Workspace
• Container for data. Can be a folder, ArcInfo coverage, personal geodatabase, file
geodatabase, ArcSDE connection
– Workspace path
• System path to the workspace
– Dataset
• Feature class or table in the workspace
– Data Source
• Combination of the workspace and dataset.
118
![Page 119: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/119.jpg)
Fixing Broken Data Sources
• Three arcpy.mapping classes involved
– MapDocument
– Layer
– TableView
• Methods on classes used to fix data souces
119
![Page 120: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/120.jpg)
MapDocument Methods for Fixing Broken Data Sources
• MapDocument class provides two methods for fixing broken data sources
• findAndReplaceWorkspacePaths( )
• replaceWorkspaces( )
120
![Page 121: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/121.jpg)
MapDocument.findAndReplaceWorkspacePaths
• Used to perform a global find and replace workspace paths for all layers and tables in
map document
• Can replace the paths to multiple workspace types at once
– CAD, shapefiles, file geodatabases, etc
121
![Page 122: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/122.jpg)
MapDocument.replaceWorkspaces
• Similar to findAndReplaceWorkspacePaths()
• Also allows you to switch from one workspace type to another
– Personal geodatabase to file geodatabase
• Only works on one workspace type at a time
122
![Page 123: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/123.jpg)
Layer and TableView findAndReplaceWorkspacePath()
• Both Layer and TableView also have findAndReplaceWorkspacePath() method
• Find and replace path for single layer or table
• Layer can be in a map document or layer file
• Table can be in a map document
123
![Page 124: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/124.jpg)
Layer and Table View replaceDataSource()
• Both Layer and TableView also have replaceDataSource() method
• Similar to findAndReplaceWorkspacePath()
– Can also change the dataset
– Useful when dataset name has changed in the workspace
124
![Page 125: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/125.jpg)
Exercise
• Please complete the exercises found in Chapter 3 in the book Programming ArcGIS 10.1
with Python Cookbook
125
![Page 126: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/126.jpg)
Chapter 4: Automating Map Production and PrintingCopyright 2015: GeoSpatial Training Services, LLC
![Page 127: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/127.jpg)
In This Section
• Working with the Layout View
• Printing Map Documents
• Exporting Map Documents
• Working with PDF Files
• Exporting a Report
• Building a MapBook with Data Driven Pages
127
![Page 128: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/128.jpg)
WORKING WITH LAYOUT ELEMENTS
128
![Page 129: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/129.jpg)
List Layout Elements
• ListLayoutElements() returns Python list of Layout Classes
– Not map annotation elements
• Each element returned an instance of one layout class
– GraphicElement
– LegendElement
– PictureElement
– TextElement
– MapSurroundElement
129
![Page 130: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/130.jpg)
Layout Element Classes
130
![Page 131: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/131.jpg)
ListLayoutElements()
• Syntax
– Reference the map document
– Element_type can be used to filter the returned layout types
• DATAFRAME_ELEMENT, GRAPHIC_ELEMENT, LEGEND_ELEMENT, MAPSURROUND_ELEMENT,
PICTURE_ELEMENT, TEXT_ELEMENT
– Wildcard used on ‘name’ property to filter returned list
131
![Page 132: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/132.jpg)
Overview of Layout ClassesDataFrame
• Provides access to data frame properties in the .mxd
• Object can work with both map units and page layout units
– Depends on property
– Page layout properties
• Positioning (elementPositionX, elementPositionY)
• Sizing (elementWidth, elementHeight)
132
![Page 133: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/133.jpg)
Overview of Layout ClassesGraphicElement
• Generic elements added to page layout
– Groups of elements
– Tables
– Graphs
– Neatlines
– Markers
– Lines
– Area Shapes
• Often set positioning and size
133
![Page 134: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/134.jpg)
Overview of Layout ClassesLegendElement
• Provides operations for
– Positioning
– Modification of title
– Getting legend items and parent data frame
• Associated with single data frame
134
![Page 135: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/135.jpg)
Overview of Layout ClassesMapsurroundElement
• North arrows, scale bars, scale text
• Associated with a single data frame
• Properties enable repositioning on the page
135
![Page 136: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/136.jpg)
Overview of Layout ClassesPictureElement
• Represents a raster or image on the page layout
• Properties
– Repositioning of the picture
– Getting and setting data source
• Useful when you need to change the picture.
– Logo change for example
136
![Page 137: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/137.jpg)
Overview of Layout ClassesTextElement
• Represents text on a page layout
– Inserted text, callouts, rectangle text, titles
– Does not include legend titles or text that is part of a table
• Properties enable repositioning as well as modifying the text string
137
![Page 138: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/138.jpg)
PRINTING AND EXPORTING MAPS
138
![Page 139: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/139.jpg)
Printing and Exporting
• Many functions for exporting and printing maps
– PrintMap
– ExportToAI
– ExportToBMP
– ExportToEMF
– ExportToEPS
– ExportToGIF
– ExportToJPEG
– ExportToPDF
– ExportToPNG
– ExportToSVG
– ExportToTIFF
139
![Page 140: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/140.jpg)
Listing Printer Names
• ListPrinterNames() returns Python list of available printers to the local computer
• Returned values used as input parameters to PrintMap() function
140
![Page 141: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/141.jpg)
Printing Maps
• PrintMap() prints
– Specific data frame
– Page layout of map document
• Uses printer saved with map document or default system printer if a specific printer not
provided
• Use PrintMap() with ListPrinterNames()
• Example: Printing a specific data frame
141
![Page 142: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/142.jpg)
Exporting Maps to Image Files
• Can use one of many Export functions
– Exports to a specific image type
– Exports the page layout or a data frame from a map document
• Available functions
– ExportToAI, ExportToBMP, ExportToEMF, ExportToEPS, ExportToGIF, ExportToJPEG,
ExportToSVG, ExportToTIFF
• Parameters provided to each function will vary
142
![Page 143: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/143.jpg)
Exporting Maps to PDF
• ExportToPDF() exports a page layout or data frame to PDF format
• PDF popular interchange format
143
![Page 144: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/144.jpg)
WORKING WITH PDF FILES
144
![Page 145: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/145.jpg)
Manipulating PDF Documents
• PDFDocument class used to manipulate PDF documents
– Merge pages
– Set document open behavior
– Add file attachments
– Create or change document security settings
• PDFDocumentOpen() and PDFDocumentCreate() functions provide reference to
PDFDocument
• Often used in the creation of map books
145
![Page 146: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/146.jpg)
Creating a PDF Document
• Use PDFDocumentCreate(pdf_path)
• Accepts a path and file name
• PDF not actually created until
– Insert or append pages appendPages() or insertPages()
– File saved PDFDocument.saveAndClose()
146
![Page 147: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/147.jpg)
Opening a PDF Document
• PDFDocumentOpen(pdf_path)
– Returns an instance of PDFDocument
• Once open can make modifications to properties add or insert files, attach documents
• Use PDFDocument.saveAndClose() after operations
147
![Page 148: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/148.jpg)
PDF Document Properties
• Can get and set PDF document properties
– PDFDocument.pageCount
• Returns an integer representing the number of pages
– PDFDocument.attachFile()
• Attaches files to an existing PDF document
– PDFDocument.updateDocProperties()
• Can update title, author, subject, keywords, open view, layout
– PDFDocument.updateDocSecurity()
• Used to set password, encryption, security restrictions
148
![Page 149: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/149.jpg)
EXPORTING A REPORT
149
![Page 150: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/150.jpg)
Introduction to Reports
• Reports provide a way of presenting information about your data or analysis
• Report information pulled from attribute table or feature class
• Can contain
– Attribute information
– Maps
– Pictures
– Graphics
– Other supporting information
150
![Page 151: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/151.jpg)
Report Wizard and Designer
• ArcMap includes Report Wizard and Designer
– Creates and modifies reports
• Report Wizard can create
– Report Document File (RDF)
• Static report of data
• Report Designer
– Report Layout File (RLF)
• Template file (includes fields in the report and how they are grouped, sorted, formatted)
• Can be used over and over to generate new reports using recent data
• Use report template with arcpy.mapping to automate production of reports
151
![Page 152: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/152.jpg)
ExportReport() Function
• Arcpy mapping includes an ExportReport() function
• Connects a data source to a template file to automate the report creation
• Can be combined with PDFDocument class to create map book
152
![Page 153: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/153.jpg)
BUILDING A MAP BOOK WITH DATA DRIVEN PAGES
153
![Page 154: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/154.jpg)
Introduction to Map Books
• Map books contain a series of individual maps that cover a larger geographic area
• Can also include ancillary information including title pages, overview maps, reports, and
tables
• Maps and ancillary information joined into a single document
154
![Page 155: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/155.jpg)
What are Data Driven Pages?
• Functionality that allows you to create multiple maps through the use of a single layout
that has multiple map extents applied
– Extents defined by features in a layer (tiles, sections, areas of interest)
• Index layer
• Data Driven Pages created in ArcMap with Setup Data Driven Pages dialog box
– Accessed through Data Driven Pages toolbar or Page and Print Setup dialog box
155
![Page 156: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/156.jpg)
Data Driven Pages and Arcpy Mapping
• Most efficient way to create a map book?
– Use a combination of Data Driven Pages toolbar and Arcpy.mapping script
– Data Driven Pages toolbar used to create a basic series of maps using layout view
• Contains operational data and index layer
• Index layer used to define the extent of each map in the series
– Arcpy.mapping used to
• Automate the export of each map in the series
• Join maps with ancillary information (overview map, title page)
156
![Page 157: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/157.jpg)
Scripting Data Driven Pages in Arcpy
• Arcpy Mapping module has a DataDrivenPages object
• Manages the individual pages within a map document
• Can create Data Driven Pages without ArcMap user interface
– Good reasons to combine the techniques in ArcMap with scripting techniques
• Data Driven Pages enabled map documents can save many lines of code
– Page extents, scales, dynamic text are all managed automatically within map document
file
• Doesn’t require any code
157
![Page 158: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/158.jpg)
Scripting Data Driven Pages
• Data Driven Pages must be enabled and authored within a map document (mxd) before
it can be referenced with arcpy.mapping
• DataDrivenPages class in arcpy.mapping
• To export a map
– DataDrivenPages.exportToPDF()
– DataDrivenPages.printPages()
158
![Page 159: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/159.jpg)
Data Driven Pages Export
• Export is sent to a multi-page PDF document
• Annotation, labeling, attribute data are retained
• Support embedding of fonts
– Symbols display correctly even if user does not have Esri fonts installed
159
![Page 160: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/160.jpg)
Exercise
• Please complete the exercises found in Chapter 4 in the book Programming ArcGIS 10.1
with Python Cookbook
160
![Page 161: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/161.jpg)
Chapter 5: Executing Geoprocessing Tools from ScriptsCopyright 2015: GeoSpatial Training Services, LLC
![Page 162: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/162.jpg)
In This Section
• Available Geoprocessing Tools
• Toolbox Aliases
• Tool Syntax
• Using Tools
– Tool Examples
• Buffer
• Tool Results
• Tool Help
162
![Page 163: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/163.jpg)
Introduction
• All geoprocessing tools are accessible through ArcPy
• Each tool exposed as a native method
• Any tool can be run as a method
• Tools available depend on license
level
– Basic, Standard, Advanced
– Now 800+ geoprocessing tools
163
![Page 164: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/164.jpg)
Available Tools
• Must first import ArcPy
• Then
– Have access to all geoprocessing tools in the standard toolboxes installed with ArcGIS
164
![Page 165: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/165.jpg)
Toolbox Aliases
• All toolboxes have an alias property
System Toolbox Alias
Analysis Tool analysis
Cartography Tools cartography
Conversion Tools conversion
Data Management Tools management
Editing Tools edit
Geocoding Tools geocoding
Linear Referencing Tools lr
Multidimension Tools md
Spatial Statistics Tools stats
165
![Page 166: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/166.jpg)
Toolbox Aliases
• All toolboxes have an alias
• Analysis Tools Toolbox example
Toolbox Name
Toolbox Alias
166
![Page 167: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/167.jpg)
How to Reference a Tool
• Geoprocessing tools organized in 2 ways
1. As functions in ArcPy
• Take the form <tool> _ <toolbox alias>
– Example: Buffer tool in Analysis Tools Toolbox
2. In modules matching the toolbox alias name
167
![Page 168: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/168.jpg)
Tool Syntax
• Each tool requires specific parameters to run correctly
– Example: Buffer tool requires several inputs including a buffer distance, the input features to buffer, and an output feature class.
– Parameters have several important properties:
– You must supply the parameters a tool needs to run the correctly.
168
![Page 169: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/169.jpg)
Tool ExampleBuffer Tool• Creates buffer polygons around input features at specified distance
• Three required arguments.– Input features, output feature class, buffer distance.
• Syntax
• Example:
• Notice that we add the suffix _analysis to the Buffer tool syntax.– There are two buffer tools; one in the Analysis Tools toolbox and one in the Coverage Tools
toolbox.
– We need a way of distinguishing between the two.
169
![Page 170: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/170.jpg)
Tools and Data Paths
• Many tools require paths to datasets
• Paths must be the same as path reported in ArcCatalog
170
![Page 171: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/171.jpg)
Paths to Data in Python
• Backslash is a reserved character in Python
• When specifying a path in Python you can use
– A single forward slash (/)
c:/data/roads.shp
– Two backslashes (\\)
c:\\data\\roads.shp
– String literal with the letter r before the string r”c:\data\roads.shp”
• Scripts will not execute unless you use these techniques
171
![Page 172: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/172.jpg)
Tool Results
• The output value of a tool is stored in a Result object
– Same information that is stored in Result window of ArcMap
• Result object
– Maintains information about the tool execution
• Messages, parameters, output
• Results maintained
172
![Page 173: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/173.jpg)
Tool Help
• ArcGIS Desktop help system provides help for each geoprocessing tool
http://resources.arcgis.com/en/help/main/10.2/index.html#/A_quick_tour_of_geoprocessing_tool
_references/002t0000000z000000/
• Script samples showing usage of the tool
173
![Page 174: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/174.jpg)
Tool Messages
• Tools generate messages during execution
– Written to geoprocessor object
• Types of messages
– Operation start and end times
– Parameter values passed to tool
– Progress information
– Warnings and Errors
• Messages can be read from your script
• Messaging and results covered extensively in next module
174
![Page 175: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/175.jpg)
Exercise
• Please complete the exercises in Chapter 5 in the book Programming ArcGIS 10.1 with
Python Cookbook
175
![Page 176: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/176.jpg)
Chapter 6: Creating Custom Geoprocessing ToolsCopyright 2015: GeoSpatial Training Services, LLC
![Page 177: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/177.jpg)
Module Outline
• Creating a Custom Geoprocessing Tool (ArcGIS Script Tool) in a Custom Toolbox
• Creating a Python Toolbox
177
![Page 178: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/178.jpg)
CUSTOM ARCGIS SCRIPT TOOLS
178
![Page 179: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/179.jpg)
ArcGIS Script Tools
• Visual interface
• Use in ArcToolbox, ModelBuilder, Command Line
• Write messages to progress dialog
• Provide documentation to your users
179
![Page 180: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/180.jpg)
Custom Script Tools
• Can create your own custom tools
– Behave like system tools
– Can be opened and run from Search or Catalog
– Used in ModelBuilder, Python Window, stand-alone Python script
– Build your own library of tools
180
![Page 181: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/181.jpg)
Script Tool Parameters
• Script tools have parameters
– Input
• Allows users to input data
• No need to hard code paths and data sources
– Output
• Must have output parameters to use in ModelBuilder
– Output connected to input of another tool
• Makes your tool flexible
181
![Page 182: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/182.jpg)
Script Tool Advantages
• Script becomes part of geoprocessing framework.
– Can be run from a model, command line or another script.
– Script has access to ArcMap environment settings.
– Help documentation available.
• User interface.
– Easier to use.
• Buttons available for browsing to data sources.
• Error prevention.
– Dialog informs the user of certain errors.
• Data source problems
182
![Page 183: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/183.jpg)
Script Execution Environments• Python scripts can be executed
– Outside ArcGIS
• Command Line or IDLE
• No visual interface
• Known as ‘Stand alone scripts’
• Can be scheduled to batch process data
• Uses sys.argv[ ]
– Inside ArcGIS
• Script Tools
• Inside Toolbox
• Visual interface
• Write messages to progress dialog
• Provide documentation
183
![Page 184: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/184.jpg)
Creating a Script Tool
Step 1: A Script
Step 2: Custom Toolbox
Step 3: Parameters for your Script
3 Steps to Creating a Script Tool
184
![Page 185: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/185.jpg)
Step 1: Creating a Script
• Python script with .py extension
• Should be able to accept parameters
• Perform geoprocessing
• Will be added to a custom toolbox
185
![Page 186: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/186.jpg)
Step 2: Custom Toolbox
• Script tools attached to custom toolbox
– Create new custom toolbox to store the tool or use an existing
custom toolbox.
– Can’t add custom scripts to an existing, system toolbox.
• These are read-only toolboxes.
Custom
Toolbox and
Script Tools
186
![Page 187: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/187.jpg)
How to Create Custom Toolbox
• Right click ArcToolbox and select New Toolbox.
• Give the Toolbox a name and alias.
187
![Page 188: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/188.jpg)
Step 3: Script Parameters
• Most tools have parameters
• Parameter values sent to your script
– Script reads parameters and continues to execute
– Parameters read with
• GetParameterAsText()
OR
• sys.argv[ ]
• Either can be used
• GetParameterAsText()
is preferred
188
![Page 189: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/189.jpg)
sys.argv[ ]
• Allow you to capture parameter input
• Words stored in a Python list
• List is zero based
– sys.argv[0] stores script name
– So, first parameter stored as sys.argv[1]
• Example on next slide
189
![Page 190: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/190.jpg)
sys.argv[ ] Example
190
![Page 191: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/191.jpg)
GetParameterAsText()
• Used to capture input parameters
• This method can only be used with ArcToolbox.
– Not for use with IDLE or Command Prompt
• Zero based. First argument is at position 0, and each argument thereafter is incremented by one.
191
![Page 192: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/192.jpg)
Add Custom Script to Toolbox
• Once you have created a custom toolbox,
right-click the toolbox and click Add…Script.
• You will then be prompted for general script
properties and to browse for the script file.
192
![Page 193: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/193.jpg)
General Script Properties
• Name
– Must be a unique ArcToolbox name.
– No spaces or restricted characters.
• Label
– The text to display in the toolbox that references the Name.
– Doesn’t have to be unique.
• Description
– General description of what functionality the script provides.
• Stylesheet
– Used to customize the properties of dialog items and background of the tool.
193
![Page 194: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/194.jpg)
Script Source• The second form in the Add
Script wizard prompts you to
define the script source.
• This is the script that will be
executed from the tool.
• Browse to the Python script (.py)
extension
194
![Page 195: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/195.jpg)
Running Scripts In Process
• Python scripts can be run in process
– Improves performance
• Script run out of process requires ArcGIS to start a second process
to run the script
– Performance suffers
195
![Page 196: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/196.jpg)
Script Parameters• Final step of the Add Script wizard
• Each parameter corresponds to each sys.argv[] or
GetParameterAsText argument in the script.
• Parameters have properties
– Type
• Required, optional, derived
– Direction
• All tool should have some type of output
– Environment
– Filter
196
![Page 197: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/197.jpg)
WORKING WITH PYTHON TOOLBOXES
197
![Page 198: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/198.jpg)
Section Outline
• What is a Python toolbox?
• Creating a Python toolbox
• Tool parameters
• Assessing parameters from a script
• Parameter validation
198
![Page 199: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/199.jpg)
How Python Toolbox Differs from Custom Toolbox
• Covered Script Tools in Custom Toolboxes in our Programming ArcGIS with Python
Workshop
• Starting with ArcGIS 10.1 there are 2 ways to create a toolbox
– Script Tools in Custom Toolboxes
– Script Tools in Python Toolboxes
• Python Toolbox encapsulates everything in one place
– Parameters, validation code, source code
– Not the case with Custom Toolboxes
• Everything created in Python (no wizard though like in Custom Toolboxes)
199
![Page 200: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/200.jpg)
What is a Python Toolbox?
• Toolbox created entirely in Python
• Has a file extension of .pyt
• Created as a class named Toolbox (don’t change the class name!)
– __init__ method
• Used as a constructor for setting various toolbox properties
– Label
– Alias
– Description
• Name of the toolbox set by the name of the file
– Example: BufferBusinesses.pyt
200
![Page 201: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/201.jpg)
Python Toolbox Template
• To create a new Python Toolbox select the folder where the toolbox will be created and
then New Python Toolbox
• Creates a new toolbox with a single default tool named “Tool”
201
![Page 202: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/202.jpg)
Code Behind Python Toolbox
• Python code is automatically generated for the Toolbox and Tool
– Tool should be renamed
• To view the code Right click the Toolbox and select Edit
202
![Page 203: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/203.jpg)
Code Behind Python Toolbox
• Can edit code for a Python Toolbox
• Code opens in Notepad by default
– Can be edited in any editor
• IDLE, PythonWin, PyScripter, etc
• Go to Geoprocessing
Geoprocessing Options Editor
203
![Page 204: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/204.jpg)
Code Behind Python Toolbox
• Edits you make in the editing environment of
choice can be saved
• Python Toolbox will be automatically updated
when editor is closed
– Can manually refresh by right clicking the
Toolbox and selecting Refresh
204
![Page 205: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/205.jpg)
Toolbox Class
1. Class name should always remain as ‘Toolbox’ (don’t change the name)
2. Set the label, alias, description properties in the __init__ method
3. Python list containing all tool classes in the toolbox. One entry per tool.
205
![Page 206: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/206.jpg)
Tool Class
• Toolbox template includes a stubbed-out tool named Tool
• Each tool class can have several methods (2 are required)
– __init__
• Initializes the tool
– execute()
• Tools source code
• Rename the Tool
• Can be duplicated
– Creates new tools
206
![Page 207: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/207.jpg)
Defining Parameters
• Most tools have parameters
– Values set by the end user from the dialog box
OR
– Within the script (hard coded)
• Parameters passed into tool
• Tool parameters defined in Tool’s getParameterInfo() method
– Create Parameter objects
• More info on Parameter object on next slide
207
![Page 208: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/208.jpg)
Parameter Object
• Parameter object properties (required)
– displayName – name displayed on tool’s dialog box
– name – name shown in tool’s syntax
– datatype – all parameters have a datatype. Used for checking values and browsing for
data.
– parameterType – required, optional, derived
– direction – input or output
• Defined in getParameterInfo() method
208
![Page 209: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/209.jpg)
MultiValue Parameters
• Some parameters need to be able to handle a list
of values instead of a single value
– Multivalue parameters
• Values are stored in a Python list
• Set multiValue property = true
• Multivalues can be created from check boxes or
the multivalue control in ArcGIS
– Fields, strings, longs, doubles use CheckBox
209
![Page 210: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/210.jpg)
Default Values for a Parameter
• Default values can be set in 2 ways
– Using the value property
– Apply the value of an environment setting using defaultEnvironmentName
210
![Page 211: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/211.jpg)
Applying Filters to a Parameter
• Filters let you narrow the list of available choices to the end user
– Example: Filter a list of feature classes so that only polygon feature classes are displayed
• Only a list of valid choices are presented – user selects a valid choice
– If user types a value rather than selecting a value it will be checked against filter
• Two properties can be set to enable filters
– filter.type
• Can be set to ValueList, Range, FeatureClass, File, Field, Workspace
– filter.list
• Python list of values for the filter
• Most values can only use a single filter.type
– Long and Double can use either ValueList or Range
211
![Page 212: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/212.jpg)
Filters - ValueList
• Good when you need a set of keywords
– Example: Set of field type keywords
• Can also set multiValue = true to give user the ability to select more than one value from
the list
212
![Page 213: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/213.jpg)
Filters - Range
• Used with Long and Double parameters (numeric)
• Two values (min and max)
213
![Page 214: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/214.jpg)
Filters - FeatureClass
• Choose one or more filter values
– Point, Polyline, Polygon
• Used with datatype = “GPFeatureLayer”
214
![Page 215: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/215.jpg)
Filters - File
• List of file suffixes
– txt, csv, png
– Any file extension can be used
– Don’t include the dot
• Correct = txt
• Incorrect = .txt
215
![Page 216: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/216.jpg)
Filters - Field
• Used to define permissible field types
– Short, Long, Float, Single, Double, Text, Date, OID, Geometry, Blob, Raster, GUID,
GlobalID, XML
216
![Page 217: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/217.jpg)
Filters - Workspace
• Type of input workspace that can be used
– File System (folder for shapefiles, coverages, INFO tables, grids)
– Local Database (personal or file geodatabase)
– Remote Database (ArcSDE connection)
217
![Page 218: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/218.jpg)
Symbology
• If tool output is:
– Feature set
– Raster
– TIN
– Network Analyst Layer
• Can specify a layer file (.lyr) to define symbology for the output
• Use symbology property
218
![Page 219: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/219.jpg)
Parameter Data Types
• Use datatype property of Parameter class to define the datatype
• List of datatypes
219
![Page 220: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/220.jpg)
Accessing Parameters
• Business logic of a tool located in execute() method
220
![Page 221: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/221.jpg)
Accessing Parameters
Use valueAsText property to retrieve individual parameters
221
![Page 222: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/222.jpg)
What is Validation?
• What occurs before the OK button is clicked on a tool
• Checks parameter values to see if they are correct
• Uses a block of Python code
• 2 parts to validation
– Part you control through code
– Part ArcGIS does automatically (internal validation)
222
![Page 223: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/223.jpg)
Internal Validation
• Validation performed automatically by ArcGIS
– Checks to see that required parameters have been provided
– Checks data type of value
– Checks for existence of input values
– Checks filter membership
– Checks existence of an output dataset. If dataset exists and overwriteOutput is false, an
error is produced.
– If parameter is a Field data type, check that the field exists in table.
– Check that output dataset isn’t same as input dataset
223
![Page 224: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/224.jpg)
Your Validation Code
• ArcGIS can’t validate everything
• Things you can control in your validation code
– Update filters based on interaction with other parameters
– Enable/disable parameters
– Calculate default values
– Perform tool specific parameter interaction
• Have no consequence on internal validation
• Only affect appearance of tool dialog
224
![Page 225: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/225.jpg)
Your Validation Code
• Parameter objects useful in validation
– Defined in getParameterInfo() method of tool class
– Behavior of these parameters is validated in updateParameters()
225
![Page 226: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/226.jpg)
Exercise
• Please complete the exercises found in Chapter 6 of the book Programming ArcGIS 10.1
with Python Cookbook
226
![Page 227: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/227.jpg)
Chapter 7: Querying and Selecting DataCopyright 2015: GeoSpatial Training Services, LLC
![Page 228: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/228.jpg)
Module Outline
• Constructing proper attribute query syntax
• Creating feature layers and table views
• Selecting features and rows with the Select Layer by Attribute tool
• Selecting features with the Select by Location tool
• Combining spatial and attribute queries
228
![Page 229: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/229.jpg)
Attribute Query Syntax
• Attribute queries have a specific syntax and follow certain rules
• Rules
– Surround field name with quotes
– Surround value being evaluated with quotes
• If the field is a Text data type
– Surround entire statement with quotes
229
![Page 230: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/230.jpg)
Attribute Query Rules
• Field names in a query surrounded by quotes
qry = ‘ “SVCAREA” = \’ North \’ ‘
– Exception for personal geodatabases
• Use square brackets instead
qry = ‘ [SVCAREA] = \’ North \’ ‘
230
![Page 231: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/231.jpg)
Attribute Query Rules
• ‘Text’ datatype fields must use quotes around the string being evaluated
–In Python, quotes must be escaped
• Use a forward slash followed by the character being escaped
qry = ‘ “SVCAREA” = \’ North \’ ‘
231
![Page 232: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/232.jpg)
Using Wildcards
• For shapefiles, file geodatabases, ArcSDE geodatabases
– Percent sign (%) used to represent any number of characters
– Underscore (_) used to represent a single character
• Personal geodatabase
– Asterisk (*) used to represent any number of characters
– Question mark (?) used to represent a single character
232
![Page 233: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/233.jpg)
Querying for Null Values
• Null is the absence of a value
• You can use the following to test for Null values IS NULL
IS NOT NULL
qry = ‘ “SVCAREA” IS NULL’
233
![Page 234: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/234.jpg)
Combining Expressions
• Use AND/OR to combining expressions where multiple query conditions must be met
qry = ‘ “SVCAREA” = \’ North \’ AND “HR” = \’ 23 ‘\ ‘
234
![Page 235: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/235.jpg)
Creating Feature Layers and Table Views
• Intermediate datasets held in memory
• Discarded after tools has executed
• Use with certain tools
– Select by Location
– Select Layer by Attributes
• Created with Make Feature Layer or Make Table View tools
– Must be called before Select by Location or Select Layer by Attributes
• Temporary output dataset is used by these tools
235
![Page 236: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/236.jpg)
Select Layer by Attribute Tool
• Used to perform attribute queries
• Feature Class or Table
• Uses properly constructed queries
• Requires a Feature Layer or Table View
• New selection, add to selection, remove from selection, subset selection, switch
selection, or clear selection
236
![Page 237: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/237.jpg)
Select by Location Tool
• Selects features based on a spatial relationship
• Uses an input Feature Layer
• Intersect, contains, within, boundary touch, many others
237
![Page 238: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/238.jpg)
Combining Spatial and Attribute Queries
• Example: Select all burglaries within the Edgewood school district that occurred on a
Monday
• Uses Select by Location and Select Layer by Attributes
– Execute one and then the other
• Apply an initial selection set
– Apply a subset selection on the attribute query
238
![Page 239: Programming ArcGIS with Python Workshop - …s3.amazonaws.com/.../Programming_ArcGIS_with_Python_Workshop.… · –Error handling and debugging –Much more 10. Huge Standard Library](https://reader030.vdocuments.mx/reader030/viewer/2022021802/5b6b0d177f8b9a60188cffad/html5/thumbnails/239.jpg)
Exercise
• Please complete the exercises in Chapter 7 in the Programming ArcGIS 10.1 with Python
Cookbook
239