 This video shows one possible solution to Lesson 3, Practice Exercise B, which calculates the number of cities in the dataset with at least two park and ride facilities using selections in ArcGIS. It also uses an update cursor to update a field indicating whether the city has at least two park and rides. The beginning of this script is very similar to Practice Exercise A so I won't spend a lot of time on that. The lines 4 through 10 are pretty familiar from that exercise. In lines 11 and 12, I'm setting up variables to reference some fields I'm going to use. Part of the exercise is to update a field called has two park and rides to be either true or false. And so I'm setting a variable to refer to that field. In line 12, I set up a variable for the city FIPS code. We're going to be querying each city one by one and we need some field that uniquely identifies each city. You might use a city name, but that could be dangerous in the event that you've got a dataset where two cities happen to have the same name. So we'll use the FIPS code instead. In lines 13 and 14, I'm setting up some counters that will be used to count the number of cities we run into that have at least two park and rides, and then the number of cities total, respectively. In line 17, I open an update cursor on the city's feature class in specifying the fields couple. I'm going to need the FIPS code so I can create a feature layer for the city in each iteration of the loop through the cursor. And I need the has two park and rides field, since that's the field I want to set to true or false. So I'm going to loop through each city here and select the number of facilities that occur in each city and count those up. I'm going to be working with two fields using this cursor, and I pass those in a couple. That's the city ID string field, and the park and ride field. And those are the variables that I saw earlier in script in lines 11 and 12 might be helpful at this point to take a look at what we're doing conceptually in ArcGIS pro. So the first thing we're going to do is for each city, make an attribute query for that city FIPS. So in order to do this for the city of Seattle, it's FIPS code is this string that I have here in notepad so just copy that. And I'll open up select by attributes tool. Select city boundaries. I want a new selection. Create an expression where city FIPS is equal to, and I'll plug in my Seattle FIPS code and click apply. Just the city of Seattle is selected. The next step then is to select the park and ride features that intersect the selected features in my city's layer. If that step selects two or more features, then I want to record true in my true false field. If it's zero one that I want to record false. So let me open up the select by location. I want to select from the park and ride layer. The selecting features would be city boundaries. And I want a new selection. Apply. And there you see the justice the park and rides within the city of Seattle have been selected. And I could open up the entry table for the park and ride layer. And it'll tell me that I've selected 14 out of 365. So this is definitely a case where I've got more than two or two or more. Park and rides within the city. So I would want to update that has to park and ride field to true in this case. I want to repeat this for each of the 108 cities. So this is definitely a good candidate for automation. Getting back to the script. I've called my cursor city rose and set up a loop to. In which I called the iterator variable or loop variable city in line 20. We get the tips code for the city that's currently being looked at on the current iteration of the loop. We use zero there because that's the index position of the city ID string field that we put in the tuple in line 17. It was the first thing in the tuple. So it has index position zero. In line 21. We're setting up a string to store a query for that city flips. So we need the field name equals the city ID. The city ID has to be in single quotes. So we use string cat nation to plug the field name and city ID into a string expression. The script takes a while to run. I also have a print statement here that includes the city ID to help track the scripts progress as it runs online 25. I use select layer by attribute to create a feature layer representing just the current city. I store reference to that later in a variable called current city layer. In a call to select layer by location in which I select the park and ride features that are contained by the feature in current city layer. The result of that selection is stored in the selected park and ride layer variable. As discussed in exercise a the third item in the result object is the count of features in the feature layer. In line 32. I get that count using the expression selected park and ride layer and then to inside square brackets. And then in line 35. I do a check to see if that number is greater than or equal to two. If it is that I'm going to assign a value of true to the park and ride field. That's where I look back at how I define the cursor and see that the park and ride field was the second of the two fields in the fields tuple. So I need to use the expression city. One in square brackets to refer to that field. I set that field equal to true, and then call update row. So that my change gets applied to the data set. As an exercise a I want to report the percentage of cities meeting the selection criteria. I want to increment my cities with two park and rides variable by one. When the num selected park and ride is greater than or equal to two. I'm line 44. I increment the num cities variable by one. I want to increment this variable on each pass through, not just for cities that have two or more parking rides. Note that line 44 is indented one level less in line 42. In other words, so it's not part of the if block. Next note the finally block aligns 47 through 50. Here we're disposing of the objects held in the current city layer. And selected park and ride variables using the delete tool. We're going to delete each iteration of the loop since we're not we'll no longer need those objects. I would like to free up the memory that's taken up by then after the loop. We use a Python Dell statement to kill the objects held in the city and city rows variables, so that we avoid retaining unnecessary locks on the data, once the script completes. So after doing that cleanup. We're going to calculate the percentage of cities that have at least two parking rides. They can't divide by zero. So we're doing a check in line 55 for that. If everything checks out okay, which it would unless we had a major problem with our data set, we go ahead and line 56 and divide the number of cities with two parking rides by the total number of cities. And divide by 100 to get a nice percentage figure, which we then report in the print statement on line 56.