 This video shows one possible solution to Lesson 3, Practice Exercise B, which calculates the number of cities in the dataset that have 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 it. Lines 4 through 10 should be pretty familiar from that exercise. In lines 11 and 12, I'm setting up variables to reference a pair of 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, so I'm setting a variable to refer to that field. And 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 with unique values for each city. You might use the city name, but that would be dangerous in the event that you have 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 a pair of counters that we use to count the number of cities we run into that have at least two park and rides, and then the total number of cities. Now in line 17, I open an update cursor on the city's feature class. In specifying the fields tuple, I'm going to need the FIPS code so I can create a feature layer for each 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 that I want to update to true or false. It might be helpful at this point. To look in our chess pro conceptually that we're at what we need to do. So the first thing we're going to do. The first thing we're going to do for each city is to make an attribute query for that city's FIPS. And so for example, if I were to do this for the city of Seattle. I've got Seattle's FIPS code here in the pen. So I'll copy that and create a new expression where the FIPS code is equal to Seattle's code. Okay, so I end up with a selection showing just the city of Seattle selected. So the next step is going to be using select by location to select all the park and rides that are within that selected city. I'm setting the parameters for select by select player by location. I would choose park and ride as the input features for the relationship. I would use, I could use intersect I could use a number of differences. I'll go with within. And then for the selecting features. That's going to be city boundaries. And when I run this it's going to do the selection based on whatever features are selected in the city boundaries layer. And so that results in a number of park and rides being selected. And I can open up the attribute table to see those selected features. It tells me that I've got 14 out of 365 selected. So this would be a case where I definitely want to update that has to park and rides field to true. So I want to do this same process for each of the 108 cities, which makes this definitely a good candidate for automation. I'm getting back to my script. I've called my cursor city rows. And then I set up a loop in which I iterate through all the rows and city rows. And I set the iterator variable or loop variable to city. We get the FIPS code for the city that's currently being looked at on the current iteration of the loop. I'm using zero in square brackets here, because that's the index position of the city ID string field in the tuple defined on line 17. It was the first thing in the tuple. So it has index position zero. In line 21, I'm setting up a string to store a query for that city's FIPS code. And so I need the field name equals the city ID. The city ID has to be in single quotes. So I use string account nation to plug the field name and city ID into a larger string expression. Because this script takes a while to run, I also have a print statement that includes the city ID to help track the script's progress as it runs. On line 25, I use select layer by attribute to create a feature layer representing just the current city. I store a reference to that layer in a variable called current city layer. I then use that layer in a call to the select layer by location tool 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. So on line 32, I get that count using the expression selected park and ride layer and then to square brackets. Then in line 35, I do a check to see if that number is greater than or equal to two. If it is, then I'm going to assign a value of true to the park and ride field. So that's where I look back at how I define the cursor and see that I have park and ride field as the second of the two fields in the fields double. So I need to use the expression city with 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. And just like an exercise a I want to report the percentage of cities meeting the selection criteria. So I increment my cities with two park and rides variable by one, whenever num selected park and ride is greater than or equal to two. On line 44, I increment the num cities variable by one. I set this variable on every pass or loop, not just when there's two or more park and rides. So note that line 44 is indented one less level than line 42. For another words, so it's not part of the if block. After a loop, we want to clean up the objects that might maintain a lock on our data. We're using the delete tool on the two feature layers and a Python Dell statement to kill the objects held in the city and city rows variables. And after doing that cleanup, we're going to calculate the percentage of cities that have at least two parking rides. Now you can't divide by zero. So we're doing a check on line 54 for that. But if everything checks out okay, which it would unless we had a major problem with the data. So we go ahead and line 55 divide the number of cities with two parking rides by the total number of cities multiply by 100 to get a nice percentage figure. And then we report that in a print statement on line 56.