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]
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
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
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
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
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
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
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
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??
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!
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
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!’
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
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
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)
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…
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.