A class is analogous to an architectural blueprint. The blueprint provides the framework for how to create something. Classes can be used to create objects, often referred to as an instance. ArcPy classes, such as the SpatialReference and Extent classes, are often used as shortcuts to complete geoprocessing tool parameters that would otherwise have a more complicated string equivalent.
ArcPy includes several classes, including SpatialReference, ValueTable, and Point. Once instantiated, its properties and methods may be used. Classes have one or more methods called constructors. A constructor is a method for initializing a new instance of a class. In the example below, SpatialReference(prjFile) is the class constructor—it creates the spatialRef object by reading a projection file.
import arcpy prjFile = "c:/projections/North America Equidistant Conic.prj" spatialRef = arcpy.SpatialReference(prjFile)
Like most other classes, SpatialReference contains a number of methods and properties. Building on the previous sample, you can access the properties of spatialRef.
import arcpy prjFile = "c:/projections/North America Equidistant Conic.prj" spatialRef = arcpy.SpatialReference(prjFile) # Print the SpatialReference's name, and type # print(spatialRef.name) print(spatialRef.type)
Classes can be used repeatedly; in the following example, two unique point objects are created by using the Point class.
import arcpy pointA = arcpy.Point(2.0, 4.5) pointB = arcpy.Point(3.0, 7.0)
Using classes with geoprocessing tools
Tool parameters are usually defined using simple text strings. Dataset names, paths, keywords, field names, tolerances, and domain names can be specified using a quoted string.
Some parameters are harder to define using simple strings; they are more complex parameters that require many properties. Instead of using long, complicated text strings to define these parameters, you can use classes (for example, SpatialReference, ValueTable, and Point classes). The documentation for each tool contains a scripting example of how each tool parameter is defined and used.
In the following example, a SpatialReference object is created and used to define the output coordinate system of a new feature class created using the CreateFeatureClass tool.
import arcpy inputWorkspace = "c:/temp" outputName = "rivers.shp" prjFile = "c:/projections/North America Equidistant Conic.prj" spatialRef = arcpy.SpatialReference(prjFile) # Run CreateFeatureclass using the spatial reference object # arcpy.CreateFeatureclass_management(inputWorkspace, outputName, "POLYLINE", spatial_reference=spatialRef)
The string equivalent for this parameter looks something like this: PROJCS['North_America_Equidistant_Conic',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Equidistant_Conic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-96.0],PARAMETER['Standard_Parallel_1',20.0],PARAMETER['Standard_Parallel_2',60.0],PARAMETER['Latitude_Of_Origin',40.0],UNIT['Meter',1.0]];IsHighPrecision