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)
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:
- Open PyScripter and click File > New.
- Paste the code above into the new script file and modify it to fit your data (change the path).
- Save your script as a .py file.
- 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.
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.
- 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.