Using the spatial reference class

Geographic datasets, such as feature classes, coverages, and rasters, have a spatial reference that defines a dataset's coordinate system, x,y domain, m-domain, and z-domain. Each part of the spatial reference has a number of properties, especially the coordinate system, which defines what map projection options are used to define horizontal coordinates. All this information is available from describing the dataset and accessing its spatial reference property, which is actually another object containing a number of properties.


import arcpy

# Describe a feature class
#
fc = "D:/St_Johns/data.gdb/roads"
desc = arcpy.Describe(fc)

# Get the spatial reference 
#
sr = desc.spatialReference

# Check if the feature class is in projected space
#
if sr.type == "Projected":
    arcpy.Copy_management(fc,"D:/St_Johns/data.gdb/roads_UTM")

Creating a spatial reference

It is not often practical to keep all details of a spatial reference inside a Python script. By using a projection file, factory code, or spatial reference name as an argument to the SpatialReference class, you can quickly complete the properties of a spatial reference and use the object as input to a geoprocessing tool. In the following example, the spatial reference is constructed using a factory code (also known as an authority code) supplied as an input argument.

Tip:

For more information on coordinate system names and factory codes, see geographic_coordinate_systems.pdf and projected_coordinate_systems.pdf files in the ArcGIS Documentation folder.

import arcpy

inputWorkspace = "c:/temp"
outputName =  "rivers.shp"

# Get the input workspace, the output name for the new feature class
#  and path to an input projection file
#
inputWorkspace = arcpy.GetParameterAsText(0)
outputName     = arcpy.GetParameterAsText(1)
factoryCode    = arcpy.GetParameterAsText(2)

# Use a code as input to the SpatialReference class
#
sr = arcpy.SpatialReference(factoryCode)

# Use the SpatialReference object to create a new feature class with a 
#  specific coordinate system
#
arcpy.CreateFeatureclass_management(inputWorkspace, outputName, 
                                    "POLYLINE", spatial_reference=sr)
Note:

For a full list of properties and methods, see SpatialReference class.

Note:

Spatial references are sometimes referred to as Projection Engine strings. The Projection Engine (PE) is a library of code that all of ArcGIS uses for defining map projections and transforming from one projection to another.