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. 


Video: Walk-Through of First Python Script (5:33)

Click here for transcript of the first "Walk through PyScripter" video.

The purpose of this video is to walk through the first Python script in this course, which will be especially helpful if you're a beginner with Python. I have PyScripter open here, which is the development environment, or code editor, that 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 four, we import the arcpy site package. Now if your script has anything at all to do with geoprocessing or looking at Esri datasets, 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 this class.  In line six, we create a string variable. We call this variable featureClass, but we could really call it anything. What to call the variables is up to us; what to assign the variable? Here is the path of the feature class that we want to look at, and we put that in quotes, which makes it a string in the eyes of Python. When you make a string, pyScripter is going to help you out by putting that text in orange so that it's easy to spot in your code. Notice in the path we use forward slashes. This is different from the common backslash that you would see typically when you define a path. The backslash in Python is a reserved character, so you have to use either two backslashes or a forward slash. You're going to see us use both in this course. In this case, the path is pointing at a file geodatabase. That's why you see the USA.gdb. Inside that file geodatabase is a feature class called boundaries. This is probably a polygon or a line feature class, who knows? If you didn't know the exact path to get, you could open the Catalog view in ArcGIS Pro, navigate to that feature class and then you would see in the upper location bar the exact path to use. You could also navigate to it in your File Explorer in Windows.  In line 9, we call a method in arcpy, the arcpy Describe method, which returns to us what's called 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 want to learn about this particular object, the Describe object, you could look it up and you should look it up in the ArcGIS Pro Help.  In this case, since we're describing a feature class, the Describe object that we're working with is going to have a spatialReference property. Now, before we move off of line 9, I want to point out the parentheses there. When you call the method Describe, you need to put something inside that parentheses, which is the path of the feature class that you would like to Describe. Now, we don't have to type in that full path here because we made a variable earlier to store the path. This is where you would plug in the featureClass variable. Python is going to see that as the path stored in that variable. After line 9, we've got a Describe object stored in our desc variable. 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 the Describe object, and spatialReference gets us, returns to us, a SpatialReference object. Now we can't print out an object, we can't do print(spatialRef). If you tried to do that. pyScripter is going to tell you that the thing you're trying to print is an object, which isn't going to be very helpful. We actually need to get a property from the SpatialReference object. The property we're going to get is the name. That's why in line 13 we do By this time we've drilled down far enough that we've made our way to a string. gives you back a string, the name of the spatial reference. That's something that we are able to print to the Python Interpreter window. Now I'm going to run this script. To run a script I just click on the Run button, which looks like a Play icon on the top. 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. 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 on the Run button. Now, anytime you run a script that includes a print statement, you should look to the bottom of the pyScripter application window to the area labeled as the Python Interpreter. Before your print statement output, you'll see a message that indicates that you've re-initialized the Python interpreter. Then comes your output, which in this case is the word geographic, meaning that this particular feature class has a geographic coordinate system. The first time you run a script in any particular pyScripter session, it's going to take several seconds for the interpreter to load into memory. However, if you go on to run any other arcpy scripts, to re-run this script or another script, they should all run more quickly.

Credit: S. Quinn, J. Detwiler © Penn State is licensed under CC BY-NC-SA 4.0.

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 PyScripter 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 at 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 PyScripter 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.