 This video walks through the solution to lesson 4, practice exercise A, where you have a text file with some coordinates that looks like this, and you need to loop through those and parse them out and then create a polygon out of the coordinates to create the shape of a U.S. state. It's a very basic list of coordinates, just two columns and no header. In order to solve this problem, we're going to use ArcPy, which we import on line 3, and the CSV module for Python, which we import on line 4. In lines 8 and 9, we set up some variables referencing the files that we're going to be working with in this script. So we're working with the mystery state shape file that was provided for you, and this shape file is in the WGS84 geographic coordinate system. However, it's an empty shape file, there's no polygons in it yet. We're going to add that. Line 9 references the path to the text file containing all the coordinate points that we're going to be using. So on line 12, we open up the text file that has the points. The first parameter with the open method is point file path, which we created up on line 9. And then the second parameter is the R in quotes, and that means we're going to be opening the file in read mode. We're just going to be reading the file, we don't need to write anything to it. In line 13, we pass that file into the CSV reader constructor so that we can create a reader object that will allow us to iterate through all the rows in the file in an easy fashion. Before we start doing that, though, in line 17, we create an empty list, which we're going to use to store the coordinate pairs that we read in from the text file. We know in this case that we're just going to create one geometry. It's going to be a single polygon created from a single list. So it's okay to create that list before we start looping. We don't actually want to create the list inside the loop because that would be creating a new list on each pass through. In line 20, we actually start looping through the rows in the file. Each row is going to be represented here by variable called points. Inside the loop, we're going to be getting the x-coordinate and the y-coordinate and append them to the list as a tuple. It's very important that the first value in the tuple be the x and the second value the y. Now, I think a lot of us typically say latitude, longitude when talking about coordinates with the word latitude coming first. But it's important to remember that latitude is the y value and longitude is the x. So in defining this tuple, we want to make sure that we're supplying the longitude first and then the latitude. Looking at our text file, we see that the first column is the longitude and the second column is the latitude. And we know that because latitudes can't go over 90. Anyway, the coordinates are in the correct x-y order. We just need to make sure that we keep them that way. So back to our script. As we iterate through the CSV reader object, remember that on each iteration, the reader object simply gives us a list of the values from the current line of the file, which we're storing in the coordinates variable. So if we want the latitude, excuse me, the longitude value, that'll be the item from the list that position zero. And if we want the latitude, that'll be the item at position one. So we use chords zero and square brackets to get the longitude, one and square brackets to get the latitude. So we're only doing one thing in this loop, appending a new x-y coordinate pair to the list on each iteration. Now line 25 is the first statement after the loop. We know that because that line is not indented at the same level. At this point, we've got the coordinates of the mystery state in a list. So we're ready to add the polygon to the shape file. To add new features to a feature class, we need to use an insert cursor. And that's what we're doing there on line 25, opening up a new insert cursor. That cursor needs a couple of parameters in order to get started. It needs the feature class that we want to modify. That's referenced by the variable shape file, which we created back on line eight. And then there's this tuple of fields that we want to modify. In our case, we're only going to modify the geometry. We're not going to change any attributes. So the way that we supply the tuple is it just has one item or token called shape followed by the at symbol. And then our line 26 actually inserts the row. And it inserts just the list of points that we populated in our loop. Because the shape file is created to whole polygons, a polygon geometry is going to be created from our list of points. When this is done, we should be able to go into our chess pro and verify that we indeed have a polygon inside that shape file. One little quirky thing to note about this code is in the last line where I supplied my tuple of values corresponding to the tuple of fields associated with the cursor. When you're supplying just a single value, it's necessary to follow that value up with a comma. If my cursor was defined with more than one field, say two fields, then my values tuple would not need to end in a comma. It's only when you're specifying a single value that the values tuple needs that trailing comma. And this is a good place to remind you that you're not limited to setting the value of a single field. For example, if there were a name field in the shape file, we happen to know the name of the state we were adding. Then we could add name to the fields tuple and then using insert row, we could supply the name of the state that we're adding. Now, the script we just walked through uses the approach to geometry creation that's recommended to use whenever possible because it offers the best performance. That approach works when you're dealing with simple single part features, and the coordinates are the same spatial reference as the feature class that you're putting the geometry into. And when you're working with ArcGIS test top 10.6 or later or ArcGIS pro 2.1 or later, because that's when support for that approach was added. So for example, this approach would not work if you wanted to add the state of Hawaii as a single feature, since that would require inserting a multi-part polygon. It also wouldn't work if our empty shape file was in, say, the state plane coordinate system rather than WGS84 geographic coordinates. So in those cases, you'd have no choice but to use an alternate solution, which is right here. In this version of script, note that in line 10, we get the spatial reference of the mystery state shape file using the describe method. We're going to go on to use this object later in the script when we create the polygon object. We didn't bother to get the spatial reference of the shape file in the other version of the script, because that method of creating geometries doesn't support supplying a spatial reference. The next difference comes on line 18. Instead of creating a new empty Python list, we create an ArcPy array object. Then inside the loop, instead of putting the coordinates into a tuple and appending the tuple to the list, we create an ArcPy point object, again in X, Y order. Then we use the arrays add method to add the point object to the array. After the loop is complete and we've populated the array, we're ready to create an object of the polygon class. The polygon class constructor requires that we specify an array of points, which is in our polygon array variable. Then optionally, we can specify a spatial reference object, which we're doing here with our spatial ref variable. This doesn't really have any effect in this particular instance, because our coordinates and our feature class are in the same spatial reference. But you could see the difference between these two scripts. If you're creating an empty feature class that was defined, to use a different spatial reference, say one of the state playing coordinate systems from primitive Mexico. Running this version of the script would result in the data being properly aligned because we're telling ArcPy how our data is projected. So it could re-project it behind the scenes into the state playing coordinates. With the other version of the script, the data would not be properly aligned because that version doesn't specify the coordinate system. In any case, this version of the script finishes up by passing the polygon object to the insert row method rather than a list of coordinate pairs. So those are two ways that you can solve this exercise.