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 spatialRef.name. By this time we've drilled down far enough that we've made our way to a string. spatialRef.name 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.