Tuesday, January 21, 2014

Class Plan (01/24/2014)

New Topics: Loops, Conditionals, Lists, and ArcGIS Desktop User Forms

What is a loop?
  • A loop is where a program cycles through a series of procedures a set number of times, or infinitely. (Be careful of infinite loops.)
  • We will focus on the FOR and WHILE statements.
  • Example of a FOR statement:
    • a = range(1,51)
    • for x in a:
      • print x
  • Another example of FOR:
    • for x in range(1,51):
    • for x in a:
      • print x
  • Example of a WHILE statement:
    •   x = 1  while x <= 9:      print x     x += 1 
What is a conditional?
  • A conditional is where a program selects from among various possible procedures based on criteria set by the programmer.
  • We will focus on the IF statement.
  • Example of the IF statement:
    • x = -1
    • if x<0:
      • print "negative"
    • elif x==0:
      • print "zero"
    • elif x==1:
      • print "one"
    • else:
      • print "other"
What is a list?
  • A list is a collection (array) of numbers and/or text values.
  • Example of a LIST:
    • a = [0, 1, 5, "happy"]
    • print a
  • Another example of LIST:
    • a = []
    • a.append(5)
    • a.append(4)
    • for item in a:
      • print item
In-Class Exercise #1 (together):
  • Create a program that will (1) prompt the user for the number of desired values, and then (2) repeatedly prompt the user to enter each value, and then (3) present the user with the sum and multiplication of the values.
  • Use the FOR statement for the loop.
  • Solution (Don't peek until we all give it a shot first!)
In-Class Exercise #2 (together):
  • Edit the above program to also ask the user to specify whether they would like the sum or multiplication of the values.
  • Solution (Again, don't peek!)
In-Class Exercise #3 (solo):
  • Write a program that (1) prompts the user for their age, and then (2) presents the user with all of the numbers from 1 to their age.
    • For example, if you are 6 years old, the program will print:
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
  • Use the WHILE statement.
  • When we begin to integrate scripts into ArcGIS, the while statement will become essential.
  • Solution

Include reference to ArcGIS API
  • # Import Statements
    import arcpy
  • To send messages to ArcGIS Window:
    • arcpy.AddMessage()
    • Instead of PRINT 


Import Script into ArcGIS

THREE steps


Create an ArcGIS User Form (GUI)

In the Catalog window or ArcToolbox window, right-click the script and click Properties.
  • Setting Script Tool Parameters






No comments:

Post a Comment