Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GEOG 315, GIS Programming, Fall 2020; Lecture 3

Avatar for alan.kasprak alan.kasprak
September 11, 2020

GEOG 315, GIS Programming, Fall 2020; Lecture 3

Avatar for alan.kasprak

alan.kasprak

September 11, 2020
Tweet

More Decks by alan.kasprak

Other Decks in Education

Transcript

  1. GEOG 315: GIS Programming and Web Mapping Lecture 3 –

    Flow Control For Today… A review of tricky concepts from last week Introduction to Automating Tasks If/Else Statements [do something if a condition is true, otherwise do something else] While Loops [do something as long as a condition is true] For Loops [for every item in a list, do something to that item]
  2. Tricky Concepts from Last Week 1. ‘Object not callable’ errors

    This cell sets up the VARIABLE called list This cell sets up the VARIABLE called list This cell calls the FUNCTION list()to make a list …and Python doesn’t know the difference
  3. Tricky Concepts from Last Week 1. ‘Object not callable’ errors

    This cell sets up the VARIABLE called list This cell calls the FUNCTION list() …and Python doesn’t know the difference
  4. Tricky Concepts from Last Week 1. ‘Object not callable’ errors

    Solution 1: don’t use function names for your variables This cell sets up the VARIABLE called list produce This cell calls the FUNCTION list()to make a list
  5. Tricky Concepts from Last Week 1. ‘Object not callable’ errors

    Solution 2: restart the kernel and run ONLY the offending cell Kernel  Restart
  6. Tricky Concepts from Last Week 1. ‘Object not callable’ errors

    Solution 2: restart the kernel and run ONLY the offending cell Kernel  Restart But if we don’t fix the underlying problem, we’re right back to error town.
  7. Public Service Announcement #2 Comment your code! These two scripts

    produce the same error But one is much easier to understand than the other …and will get you more credit.
  8. On the first day of class, I said… 1. “Here

    are 500 polygon shapefiles. Make a single Excel spreadsheet that lists the area of each one.” 2. “Tell me the watershed area of the Animas River, the San Juan River, the Colorado River, Junction Creek, the Dolores River, the San Miguel River, and…actually, just tell me the watershed area of every stream longer than 5 miles in Colorado” 3. “Can you reproject these 5,000 shapefiles from UTM to Web Mercator, please?” I SAID PYTHON CAN MAKE YOUR LIFE EASIER, AND I (SERIOUSLY) WASN’T LYING
  9. …but let’s look at the code we’ve written so far:

    1. We provide a value 2. Python does some stuff 3. We get a new value
  10. What I’d rather be doing: 1. Provide a ton of

    values without writing them all out 2. Python does a ton of work on all those values 3. I get a ton of new values What if I wanted to know the square root of every number between 1 and 1,000,000? …or, what if I wanted to clip a million shapefiles?
  11. GEOG 315: GIS Programming and Web Mapping Lecture 3 –

    Flow Control If/Else Statements [do something if a condition is true, otherwise do something else] While Loops [do something as long as a condition is true] For Loops [for every item in a list, do something to that item] I’m going to present each one using: • A real-life example • A GIS example • Some Python code
  12. If/Else Statements [do something if a condition is true, otherwise

    do something else] What if I had all night? (and wasn’t hangry?) What if I had 10 minutes? (because I needed to make this lecture)
  13. If/Else Statements [do something if a condition is true, otherwise

    do something else] IF I have more than 20 minutes THEN cook these in the toaster oven OTHERWISE use the microwave then bake
  14. If/Else Statements [do something if a condition is true, otherwise

    do something else] IF I have more than 20 minutes THEN - Preheat oven to 430 - Bake for 16-20 minutes OTHERWISE - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes
  15. If/Else Statements [do something if a condition is true, otherwise

    do something else] IF I have more than 20 minutes THEN - Preheat toaster oven to 430 - Bake for 18-22 minutes OTHERWISE - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes IF I have more than 10 minutes but less than 20 minutes THEN - Preheat oven to 430 - Bake for 16-20 minutes
  16. If/Else Statements [do something if a condition is true, otherwise

    do something else] IF I have more than 20 minutes: - Preheat toaster oven to 430 - Bake for 18-22 minutes ELSE: - Preheat oven to 450 - Microwave for 15 seconds - Bake for 8-10 minutes ELSE IF I have more than 10 minutes but less than 20 minutes: - Preheat oven to 430 - Bake for 16-20 minutes
  17. If/Else Statements [do something if a condition is true, otherwise

    do something else] FEMA flood hazard maps
  18. If/Else Statements [do something if a condition is true, otherwise

    do something else] FEMA flood hazard maps if house is in floodway: add to floodway_houses.shp charge ridiculous insurance rates else if house is in floodplain: add to floodplain_houses.shp charge high insurance rates else: add to no_risk.shp charge reasonable insurance rates
  19. If/Else Statements [do something if a condition is true, otherwise

    do something else] FEMA flood hazard maps if house is in floodway: add to floodway_houses.shp charge ridiculous insurance rates else if house is in floodplain: add to floodplain_houses.shp charge high insurance rates else: add to no_risk.shp charge reasonable insurance rates ONE OF THESE THINGS IS ALWAYS TRUE!
  20. If/Else Statements [do something if a condition is true, otherwise

    do something else] What will this print? Prints nothing …because neither statement is true
  21. If/Else Statements [do something if a condition is true, otherwise

    do something else] What will this print? Make sure you cover all possibilities!
  22. While Loops [do something as long as a condition is

    true, then stop] While temperature is less than 145 degrees keep cooking the burgers Eat the burgers This gets done while the burgers are less than 145 degrees
  23. While Loops [do something as long as a condition is

    true, then stop] While temperature is less than 145 degrees keep cooking the burgers Eat the burgers Only then do we move on to this thing
  24. While Loops [do something as long as a condition is

    true, then stop] How to generate 20 random fish sampling locations? point = 1 sample_points = 20 while point < sample_points: generate random point on line x + 1 print ‘that’s 20 points!’
  25. While Loops [do something as long as a condition is

    true, then stop] n = 5 n = 5 n = n - 1
  26. While Loops [do something as long as a condition is

    true, then stop] n = 4 n = n - 1
  27. While Loops [do something as long as a condition is

    true, then stop] n = 3 n = n - 1
  28. While Loops [do something as long as a condition is

    true, then stop] n = 2 n = n - 1
  29. While Loops [do something as long as a condition is

    true, then stop] n = 1 n = n - 1
  30. While Loops [do something as long as a condition is

    true, then stop] n = 0 (n is no longer > 0)
  31. While Loops [do something as long as a condition is

    true, then stop] n = 0 (n is no longer > 0) ‘Done’ n = 0
  32. For Loops [for every item in a list, do something

    to that item] here’s a plate, two glasses, a salad bowl, a spatula, a knife, three forks, a cutting board, six spoons, and a crock pot
  33. For Loops [for every item in a list, do something

    to that item] here’s a plate, two glasses, a salad bowl, a spatula, a knife, three forks, a cutting board, six spoons, and a crock pot Make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack
  34. For Loops [for every item in a list, do something

    to that item] for each dish in the list of dishes: make soapy water wash dish with sponge rinse with hot water place in drying rack make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack
  35. For Loops [for every item in a list, do something

    to that item] for each dish in the list of dishes: make soapy water wash dish with sponge rinse with hot water place in drying rack Make soapy water for each dish in the list of dishes: wash dish with sponge rinse with hot water place in drying rack This one is efficient This one takes forever
  36. for every county in Colorado print the county’s name calculate

    the county’s area get the county’s population get the county’s highest elevation save these things to an Excel table
  37. For Loops [for every item in a list, do something

    to that item] list = [1,2,3] for item in list print(item) …and call `item` whatever you want!
  38. For Loops [for every item in a list, do something

    to that item] list = [1,2,3] for item in list print(item) …and call `item` whatever you want!
  39. For Loops [for every item in a list, do something

    to that item] list = [1,2,3] for item in list if item > 2 add item to list “items greater than 2” else add item to list “items less than 2” There’s nothing stopping you from nesting loops!