GEOG 485:
GIS Programming and Software Development

1.6.2 Example: Printing the spatial reference of a feature class


This first example script reports the spatial reference (coordinate system) of a feature class stored in a geodatabase. If you want to use the USA.gdb referenced in this example to run the code yourself you can find it here.

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

import arcpy

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

# 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 (5:54) to get a visual walkthrough of the code. 

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


The purpose of this video is to walk through the first Python script in this course, which will be especially helpful if you're beginner with Python.

I have Spyder open here, which is the development environment, or code editor we recommend you use throughout the class.

The first couple lines of this script are a comment, and it says 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 your script has anything at all to do with geoprocessing or looking at Esri data sets, this is what you're going to put at the top of your script. 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 featureClass, 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, Spyder helps you out by putting that text in green 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.

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 the Catalog View in ArcGIS Pro, navigate to the feature class, and then you would see in the upper location bar the exact path to use.

In line 9, we actually call a method in 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 ArcGIS Pro 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:/PSU etc.

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

Now, we can't print out an object. If you try to print spatialRef to the Spyder Console, Spyder will just tell you that the thing you’re trying to print is an object, which is not very helpful. So we actually need to get a property off of this SpatialReference object, and this property is the name of the spatial reference. So that's why, in line 13, we do

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

And now, I'm going to run this script. To run a script, I just click on the Run File button, which looks like a Play icon. We’ll see later in the course that scripts sometimes require arguments, or pieces of information like a path name or a number, in order to do their job. And it’s possible to supply such arguments, but this is a pretty simple script that doesn’t require any kind of input, so we can just go ahead and click the Run button.

Now, any time you run a script that includes a print statement, you should look to the lower right pane of the window, to the area labeled as the IPython Console. Before your print statement output, you’ll actually see a message that indicates the path and name of the script you’re running. Then comes your output, which in this case is the word Geographic, meaning that this particular feature class has a Geographic coordinate system

Source: example script originally written by 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 Spyder and click File > New.
  2. Paste the code above into the new script file and modify it to fit your data (change the path).
  3. Save your script as a .py file.
  4. Click the Run button to run the script. Look to the IPython console to see the output from the print keyword. The print keyword does not actually cause a hard copy to be printed!  Note that the script will take several seconds to run the first time because of the need to import the arcpy module and its dependencies.  Subsequent runs of arcpy-dependent scripts during your current Spyder session will not suffer from this lag.


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).
ArcGIS Pro edition:
  • 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, reviewing 4.5, which you read earlier.)
  • Chapter 5 talks about working with arcpy and functions - read sections 5.1-5.2 and 5.4-5.7.
ArcMap edition:
  • 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, reviewing 4.5, which you read earlier.)
  • Chapter 5 talks about working with arcpy and functions - read sections 5.1-5.2 and 5.4-5.6.