GEOG 485:
GIS Programming and Automation

Lesson 4 Practice Exercise A Solution


Here's one way you could approach Lesson 4 Practice Exercise A with comments to explain what is going on. If you have a different or more efficient solution, please share in the forums.  If you find a more efficient way to code a solution, please share it through the discussion forums.

# Reads coordinates from a text file and writes a polygon
import arcpy
import csv
shapefile = "C:\\data\\Geog485\\MysteryState.shp"
pointFilePath = "C:\\data\\Geog485\\MysteryStatePoints.txt"
spatialRef = arcpy.Describe(shapefile).spatialReference
# Open the file and read the first (and only) line
pointFile = open(pointFilePath, "r")
csvReader = csv.reader(pointFile)
# This Array object will hold a clockwise "ring" of Point
#  objects, thereby making a polygon.
polygonArray = arcpy.Array()
# Loop through each coordinate pair and make a Point object
for coords in csvReader:
    # Create a point, assigning the X and Y values from your list    
    currentPoint = arcpy.Point(coords[0],coords[1])
    # Add the newly-created Point to your Array    
# Create a Polygon from your Array
polygon = arcpy.Polygon(polygonArray, spatialRef)
# Create an insert cursor and apply the Polygon to a new row
with arcpy.da.InsertCursor(shapefile, ("SHAPE@",)) as cursor:

Below is a video offering some line-by-line commentary on the structure of this solution:

Click for a transcript of "4A" video.

PRESENTER: This video walks through the solution to lesson 4, practice exercise A wherein 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 it to create the shape of a US state.

Notice that the first coordinate in this list and the last one are the same, indicating that it will be a polygon. This is a pretty basic list. There's no header here. So we're just going to blast right through this as if it were a csv file with two columns and no header.

In order to do this, we're going to use arcpy, which we import in line 3, and the csv module for Python, which we import in line 4. We're also-- in line 6 and 7, we set up some variables referencing the files that we're going to work with in this script. So we're working with the mystery state shapefile that was provided for you.

This shapefile has a coordinate system. I believe it's WG S84 geographic coordinates. However, there is no shape or polygon within this shapefile yet. You will add that. And then line 7 references the path to the text file containing all those coordinate points.

In line 8 we do a little bit of work to detect the spatial reference of MysteryState shapefile. We use that later when we start creating geometries. This pattern of using the describe method and then getting the spatial reference property should be familiar to you from your work in lesson 2.

In line 11 we open up that text file that has the points. So the first parameter with the open method is pointFilePath, which we created in line 7. And then the second parameter is r in quotes. And that means we're opening it in read mode. We're just going to read this file. We're not going to write anything into it.

And in line 12, we then pass that file into the csvReader constructor so that we can create this csvReader 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 16 we create an empty arcpy array object.

We know in this case we're just going to create one geometry. It's going to be one polygon with one array. So it's OK to create that array before we start looping. And we actually don't want to create it inside loop because we would be creating multiple arrays, and we just need one of them in this case.

In line 19 we actually start looping through the rows in the file. Each row is going to be represented here by a variable called coords. And so in line 22, we create an arcpy point object. And when you create a point, you need at least the longitude coordinate, which is the X value, and the latitude, which is Y.

Now, going back to our file, you'll see that in the first column or column 0, that's the longitude. So we have coords 0 in our code. And then the next piece is the latitude or the y-coordinate. That's in column with index 1. So we use coords 1 for the y.

Once we have that point, we can add it into the array. We just keep doing that, cycling through the file until we've added point after point after point inside that array and we've gotten to the end.

Now, in line 28 the loop has exited. Notice that because it's not indented. And we're going to take the array that we made and make a polygon object out of it. So we create arcpy.Polygon. And the things we pass in are the array itself, and then the spatial reference for this geometry, which we retrieved back in line 8.

So we've got the polygon all set up, but we haven't added it to the shapefile yet. And in order to do that, we're going to use an InsertCursor. In this case, we just use the very simple with statement to create the InsertCursor.

The InsertCursor needs a couple of parameters in order to get started. It needs the path to the file that we're going to modify, and that's represented by the variable shapefile. Remember we created that back in line six.

And then there's this tuple of fields that we're going to modify. In our case, we're only going to modify the geometry. We're not going to modify any attributes so the way that we supply this tuple is it just has one item called shape with the @ sign, and then a comma. A tuple of one just ends with a comma like this inside the parentheses.

And then our final line 32 actually inserts the row. And it inserts just the polygon geometry and nothing else as described. When this is done, we should be able to go into ArcMap and verify that we indeed have a polygon inside of that shapefile.