 This video shows one possible solution to lesson 3 practice exercise B, which calculates the number of cities in the data set with at least two park and ride facilities using selections in ArcGIS. It also uses an update cursor to add to update a field indicating whether the city has at least two parking rides. The beginning of this script is very similar to practice exercise A, so I won't spend a lot of time on that. Lines 4 through 8 are pretty familiar from that exercise. In lines 9 and 10, I'm setting up variables to reference some fields that I'm going to use. Part of the exercise is to update a field called has2parkandrides to be either true or false, and so I'm setting up a variable for that. And in line 10, I set up a variable for the cityfips code. We're going to be querying each city one by one, and we need some field with unique values for each city. We might use the city name, but that could be dangerous in the case that you have a data set where two cities happen to have the same name. So we'll use the FIPS code instead. In lines 11 and 12, I'm setting up some counters that will be used to count the number of cities we run into that have at least two parking rides, and then the number of cities total, respectively. In line 15, I'm starting out by making a feature layer of just the park and ride facilities. So this is familiar from the previous exercise where the first parameter I pass in is the path to the park and ride data set. And then the second parameter is a name that I give this feature layer. Just for the purposes of this script, I'll call it park and ride layer. And in line 18, I'm opening an update cursor on all of the city boundaries. 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. And so I'm going to be working with two fields using this cursor, and I pass those in in a tuple. That's city ID, string field, and park and ride field. And those are the variables that I set up earlier in the script in lines 10 and 11. It might be helpful at this point to take a look at what we're doing conceptually in ArcMap. So the first thing we're going to do is for each city make an attribute query for that city FIPS. And so, for example, if I were to do this for the city of Seattle, I've set up this, the city FIPS code is this long number here. And if I were to query that, I have one city. So I'm going to narrow down my city's feature layer to just have one thing in it. And then I'm going to do the selection by location where I select park and ride facilities that are completely within the layer of city boundaries. So doing that, I would get these points back and then I could count those or I could start looping through them and see if I hit at least two in order to determine if the city has at least two park and ride facilities. Then I would go on to the next city and the next city and so on until I had worked my way through the entire data set. So there is some processing that will occur as you run this script. It might take a few seconds to run. All right. So we are back on line 18 where we set up the update cursor. We call it city rows. And then we could loop through each city in this. So that's what is done in line 19 with a for loop. In line 21, we get the fifths code for the city that's currently being looked at by the cursor. We use zero there because that's the index position of the city ID string field that we put in the tuple in line 18. It was the first thing in the tuple so it has index position zero. And then in line 22, we're setting up a string to query for that city fifths. And so we need the field name equals the city name. And the field name has to be in double quotes. The city name has to be in single quotes. So that involves a lot of string concatenation to get that just right as we set this up. But once we have that string, we can make a feature layer that just has that one city within it. The way we do that is in line 25. And we call it make feature layer. But instead of two parameters, we put in three. The first two parameters are the same as when we made the feature layer for the parking rides. So we put in the same pattern. We put in the path to the data set. We put in a name for that feature layer. We're going to call this current city layer. But then the third parameter is that query string that we just created in line 22. And that causes our feature layer to just have the one city in it that's returned by that query. And so now we're going to do the locational selection in line 30 to select just the parking rides that are contained by that city. The parameters here should be somewhat intuitive if you have that conceptual understanding of how the selection is working. Where just one city boundary is selecting a subset of parking rides. At that point you can call the get count tool and figure out how many parking rides were indeed selected. And if that, so that's what's going on in line 33. And remember with line 34 that you've got to call get output in order to get the result of the get count tool. You got exposed to that in the solution to practice exercise a. So what in the end, what you have in line 34 is this variable called num selected parking ride. That's the number of parking ride facilities that fell within that city. And in line 37 you can do a check to see if that number is greater than or equal to two. If it is, then we're going to set the value to true. So that's where we using our cursor go to the first item in the tuple up here. Actually it's the second item but it has index position one and we set that equal to true. And we don't want to forget to call update row so that that gets applied to the data set. And then in the end we can update our counter of cities that have at least two parking rides. Finally after we're done doing all this looping we delete the feature layer. We clean it up and we increment the number of cities we've counted by one here. And so we do that for every single city in the data set and then we clean up the parking ride layer in line 52. So notice there's some multiple levels of indentation here showing the if thens, the try and finallys, and the loop that runs the cursor. And so we need to manage that carefully. If we go back and stop indenting that means we're going out of the block of code. Either out of the if statement or out of the try catch loop and so on. So after doing the cleanup now 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 in line 55 for that. But if everything checks out okay, which it would unless we had a major problem with our data set, we go ahead in line 56 and we run some division to divide the number of cities with two parking rides by the total number of cities. In the video for exercise A I explained why we multiply by 1.0 so we can do some decimal division and then we multiply by 100 to get a nice percentage figure. In line 57 and 58 we have a little bit of error handling which would occur if the input data set was invalid and there were no cities in it. But otherwise we're going to go down to line 60 and print the percentage of cities with at least two parking rides. So give it a try.