Programming Concepts - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Programming Concepts

Description:

Many attributes can be assigned values individually. Attribute values can be assigned to variables ... Geoprocessor Programming Model shows many, many more (see ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: peopleOre
Category:

less

Transcript and Presenter's Notes

Title: Programming Concepts


1
Programming Concepts
  • GEO 599
  • Week 4, Oct. 19, 2009

2
Objects
  • Python is an object-oriented language
  • Technically speaking, an object has
  • Identity (location in memory)
  • Type
  • Value
  • In Python everything is an object
  • Strings, integers floats
  • Lists, dictionaries, geographic layers
  • Functions

3
Attributes
  • Some objects have attributes
  • Attributes are properties or pieces of the
    object
  • Ex Point objects have attributes for
  • ID
  • X coordinate
  • Y coordinate
  • Many attributes can be assigned values
    individually
  • Attribute values can be assigned to variables

4
Methods
  • Many objects have methods
  • Methods do things to objects
  • Have already seen some methods of lists
  • Append adds a single element to the end of a list
    object
  • Extend adds a list of new elements to the end of
    a list object
  • Methods typically have parameters
  • Results of methods can be assigned to variables

5
Dot Notation
  • Attributes and methods are both accessed using
    dot notation syntax
  • ltobjectgt.ltattributegt
  • ltobjectgt.ltmethodgt(ltparametersgt)

6
Examples
  • Assign a value to an attribute
  • CenterPt.x Xvalue
  • Assign an attribute value to a variable
  • Yvalue CenterPt.y
  • Use a method to modify an object
  • NameList.append(Bob)
  • Assign the results of a method to a variable
  • Name TableRow.GetValue(FULL_NAME)

7
Geoprocessor Object
  • ESRI has constructed a big geoprocessor object
  • Python interacts with ArcGIS through this object
  • Geoprocessor object is created and assigned to a
    variable at the beginning of scripts
  • Geoprocessor object has methods that parallel
    tools in ArcToolbox

8
Other types of objects
  • Feature Class objects, with attributes including
  • ShapeType
  • HasSpatialIndex
  • Table objects, with attribute
  • Fields (as a Python list)
  • Row objects, with methods
  • GetValue(fieldName)
  • SetValue(fieldName, value)
  • Geoprocessor Programming Model shows many, many
    more (see links page on course web site)

9
Modules
  • Python has a limited set of built-in functions
  • Additional functions are available in modules
  • Modules are imported at the beginning of scripts
  • import ltmodulegt
  • After a module is imported, its functions are
    accessed using dot notation
  • ltmodulegt.ltfunctiongt(ltparametersgt)
  • See Python Library for long list of available
    modules

10
Activity
  • Open ArcGIS Desktop Help
  • Find the topic Describe method
  • Copy and paste the example at the bottom into a
    new Python script window
  • Change the 3rd line to point to your geodatabase
  • (e.g. "C\\GEO599_Python\\GEO599_Python_ExData.gd
    b)
  • Change the 4th line to get the UrbanizedAreas
    feature class
  • Delete the line dealing with Topology Name
  • Use the format menu to Comment out the section
    dealing with Relationship Class Names (4 lines,
    through for loop)
  • Save and run the script

11
Activity, cont.
  • Work with a partner to figure out the script and
    add comments to it
  • Include a header
  • Add a comment for each line before the series of
    print statements
  • A single comment should be sufficient to describe
    the series of print statements
  • Download the question sheet from the course
    website and answer the questions

12
Debugging Getting scripts to work
  • Use print statements to keep track of your script
  • Status reports
  • Created new feature class
  • Updated values in table
  • Now processing...
  • Track variable values
  • A buffer will be created at str(BuffDist)
  • The UA code for MSAname is UAcode
  • Remove most of these statements once the script
    is working

13
Error Handling
  • Errors that are not handled cause a script to
    stop
  • When debugging, stopping the script might not be
    a problem
  • May not want the script to continue anyway
  • Error message is usually informative
  • In finished scripts, problems should be dealt
    with more gracefully

14
Try...Except Structure
  • Syntax
  • try
  • ltcode blockgt
  • except ltexceptionTypegt
  • lterror handlinggt
  • If a problem is encountered in the ltcode blockgt,
    execution jumps immediately to lterror handlinggt
  • lterror handlinggt typically includes a print
    statement indicating the problem
  • May have multiple except statements for different
    types of exceptions

15
Try...Except Example
  • try
  • StAbrev StAbrevDictMSAname
  • except KeyError
  • print MSAname was not found in the state
    abbreviation dictionary
  • KeyError is the type of exception that occurs
    when a key is not found in a dictionary

16
Exception Types
  • Exception types are Python key words
  • Accessing non-existent key in a dictionary ?
    KeyError
  • Searching for a value that doesnt exist in a
    list ? ValueError
  • Referencing a non-existent variable ? NameError
  • Trying to do something to the wrong type of data
    ? TypeError

17
Try...Except in Loops
  • Often useful to enclose the code block of a loop
    in a try code block
  • Except statement placed at end of loop code block
  • Using the key word continue as the last line of
    the error handling block will restart the loop
    with the next item in the list

18
Syntax for Try...Except in a Loop
  • for ltloopVargt in ltListgt
  • try
  • ltstatements to repeatgt
  • except
  • lterror handlinggt
  • continue
  • ltfirst statement outside loopgt

19
GetMessages
  • Method of the geoprocessor object
  • Returns ArcGIS-specific messages
  • Often used in error handling
  • print ltGPobjectgt.GetMessages(ltseveritygt)
  • Severity
  • 0 message
  • 1 warning
  • 2 error
  • None specified returns all levels

20
Exercise 2 additions
  • Add print statements tracking progress
  • Incorporate the try...except structure
  • 5 additional points
Write a Comment
User Comments (0)
About PowerShow.com