GEOG 485:
GIS Programming and Software Development

1.6.2 Example: Printing the spatial reference of a feature class

PrintPrint

This first example script reports the spatial reference (coordinate system) of a feature class stored in a geodatabase:

# Opens a feature class from a geodatabase and prints the spatial reference

import arcpy

featureClass = "C:/Data/USA/USA.gdb/StateBoundaries"

# Describe the feature class and get its spatial reference   
desc = arcpy.Describe(featureClass)
spatialRef = desc.spatialReference

# Print the spatial reference name
print spatialRef.Name

This may look intimidating at first, so let’s go through what’s happening in this script, line by line. Watch this video (4:42) to get a visual walkthrough of the code. You'll notice a typo in the video / screenshot below desc.SpatialReference should be desc.spatialReference - as per the documentation.

Click for a transcript of "Walk-through of your first Python script" video. (4:42)

PRESENTER: The purpose of this video is to walk through your first Python script and this will be especially helpful if you're beginning Python.

I have PythonWin open here. I've displayed the line numbers and I've made the text a little bit larger than normal just so you can see it on the video.

It also makes it so it's not quite as intimidating that way.

The first few lines of this script start out with a comment and they say actually what we're going to do with this script which is to look at a feature class inside of a geodatabase and then print the spatial reference of that feature class.

In line 4, we import the arcpy site package. Now if you work with ESRI geoprocessing in your script or looking at ESRI data sets this is what you're going to put at the top of your script most of the time. So you can get in the habit of using import arcpy at the top of your scripts.

In line 6 we create a string variable. We call this variable feature class but we could really call it anything. What to call the variable is up to us. What to assign the variable is the path of the feature class we want to look at, and we put that in quotes and that makes it a string in the eyes of Python. And when you make a string, PythonWin helps you out by putting that text in yellow so it's easy to spot in your code. In the path, we use forward slashes. This is different from the common backslash that you would see usually when you use a path. The backslash in python is a reserved character so you have to either use two backslashes or a forward slash. You'll see both in this course, just so you can get in the habit of knowing that you can use both. In this case, the path is pointing at a file geodatabase. That's why you see USA.gdb. And inside that file geodatabase is a feature class called StateBoundaries. This is probably a polygon or a line feature class. If you didn't know the exact path to get, you could open our catalog and and highlight the feature class itself, and then you would see in the upper location bar the exact type of path to use.

In line 9, we actually call a method on arcpy. The arcpy describe method and this gives us back a describe object. We've mentioned in the course material that everything in Python is an object. Objects have properties that describe them and methods that are things that they can do.

If you wanted to learn more about the describe object. You could look in the ESRI help.

In our case the describe object is going to have a spatial reference. Now before we move off of line 9. I'd like to point out the parentheses there. So when you call the method arcpy.Describe. You need to put something inside that parentheses which is the path of the feature class that you want to describe. Now we don't have to type the full path again because we made a variable earlier on to represent that. So this is where you can just put in the feature class variable name and Python will read that as the actual path C:data etc.

So after line 9 we have a describe object. That described object has a property which is the spatial reference. So in line 10 that's what we're getting that. "desc" is the name of our described object and .SpatialReference gets us a spatial reference object.

Now we can't print out an object, if you try to print an object to the interactive window you'll get a bunch of text it's hard to understand, so we actually need to get a property off of this spatial reference object and this property is the name of the spatial reference. So that's why in line 13 we do spatialRef.name.

By this time we've gone down far enough that we've gotten to a string, spatialRef.name just gives you back a string with the spatial reference and so that's printable to the interactive window.

And now I'm going to run this script. I'll just move this out of the way and behind there you can see the interactive window. it's ready to go.

To run a script I just highlight the window that I want to run and I click this running man icon. The script file name is populated for me. It just comes from this window that's open. And if my script had any arguments that it needed in order to run, I could type them in here separated by spaces. Sometimes you have to supply a path name or a number or something especially if you're gonna go ahead later on and make a tool out of this in ArcGIS. But for now, we're just going to run the script right out of PythonWin so there's no need to do any arguments.

And I'll just click OK and wait for this to run and the interactive window prints out geographic, meaning that this particular feature class has a geographic coordinate system.

Source: Sterling Quinn

Again, notice that:

  • A comment begins the script to explain what’s going to happen.
  • Case sensitivity is applied in the code. "import" is all lower-case. So is "print". The module name "arcpy" is always referred to as "arcpy," not "ARCPY" or "Arcpy". Similarly, "Describe" is capitalized in arcpy.Describe.
  • The variable names featureClass, desc, and spatialRef that the programmer assigned are short, but intuitive. By looking at the variable name, you can quickly guess what it represents.
  • The script creates objects and uses a combination of properties and methods on those objects to get the job done. That’s how object-oriented programming works.

Trying the example for yourself

The best way to get familiar with a new programming language is to look at example code and practice with it yourself. See if you can modify the script above to report the spatial reference of a feature class on your computer. In my example the feature class is in a file geodatabase; you’ll need to modify the structure of the featureClass path if you are using a shapefile (for example, you'll put .shp at the end of the file name, and you won't have .gdb in your path).

Follow this pattern to try the example:

  1. Open PythonWin and click File > New.
  2. Choose to make a Python script and click OK.
  3. Paste in the code above and modify it to fit your data (change the path).
  4. Save your script as a .py file.
  5. Click the Run button to run the script. Make sure the Interactive Window is visible when you do this, because this is where you’ll see the output from the print keyword. The print keyword does not actually cause a hard copy to be printed!

Readings

We'll take a short break and do some reading from another source. If you are new to Python scripting it can be helpful to see the concepts from another point of view.

Read parts of Zandbergen chapters 4 &  5. This will be a valuable introduction to Python in ArcGIS, on how to work with tools and toolboxes (very useful for Project 1), and also on some concepts which we'll revisit later in Lesson 2 (don't worry if the bits we skip over seem daunting - we'll explain those in Lesson 2).
  • Chapter 4 deals with the fundamentals of Python. We will need a few of these to get started and we'll revisit this chapter in Lesson 2. For now read sections 4.1-4.7 revising what we read in 4.5 earlier. 
  • Chapter 5 talks about working with arcpy and functions - read sections 5.1-5.6.