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

GEOG 315, Lecture 4

alan.kasprak
September 20, 2021

GEOG 315, Lecture 4

alan.kasprak

September 20, 2021
Tweet

More Decks by alan.kasprak

Other Decks in Education

Transcript

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

    Flow Control For Today… 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. 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
  3. …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
  4. What I’d rather be doing: 1. Provide a ton of

    values without writing them all out 3. While python does a ton of work on all those values 4. 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? 2. Watch cat videos on YouTube
  5. GEOG 315: GIS Programming and Web Mapping Lecture 4 –

    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
  6. If/Else Statements [do something if a condition is true, otherwise

    do something else] What if I had a while? What if I only had 10 minutes?
  7. 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
  8. 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
  9. 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
  10. 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 ELSE: THEN - 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: THEN - Preheat oven to 430 - Bake for 16-20 minutes
  11. If/Else Statements [do something if a condition is true, otherwise

    do something else] FEMA flood hazard maps
  12. 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
  13. 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 What if I omitted this last part??
  14. 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! The quickest way to cause an if/else loop to fail is to not account for all possibilities!
  15. 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
  16. If/Else Statements [do something if a condition is true, otherwise

    do something else] What will this print? Make sure you cover all possibilities!
  17. 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
  18. 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
  19. 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 point = point + 1 print ‘that’s 20 points!’
  20. While Loops [do something as long as a condition is

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

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

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

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

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

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

    true, then stop] n = 0 (n is no longer > 0) ‘Done’ n = 0
  27. 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 an instant pot
  28. 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
  29. 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
  30. 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 (and wastes water)
  31. 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
  32. For Loops [for every item in a list, do something

    to that item] values = [1,2,3] for item in values: print(item) …and call `item` whatever you want!
  33. 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!
  34. values = [1,2,3] for item in values: if item >

    2: add item to list “items greater than 2” else: add item to list “items less than or equal to 2” There’s nothing stopping you from nesting loops! Here’s an if/else loop nested in a for loop…
  35. In pairs: Come up with a real life example (or

    a GIS example) of when you’d use: - an if/else loop - a while loop - a for loop They don’t have to be super in-depth or detailed! Take ~5 minutes and talk through these.