Setting a cursor's spatial reference
The spatial reference for a feature class describes its coordinate system, its spatial domain, and its precision.
- The coordinate system is analogous to a map projection (for example, geographic, universal transverse Mercator [UTM], and state plane). It defines the mathematical relationship between the stored coordinates and actual location on the earth.
- The spatial domain is best described as the allowable coordinate range for x,y coordinates, m (measure)-values, and z-values.
- The resolution describes the number of system units per unit of measure.
By default, the spatial reference of the geometry returned from a search cursor is the same as the feature class opened by the cursor. You can also set the spatial reference on an update or insert cursor.
When you set the spatial reference on an update or insert cursor, you are declaring the spatial reference of the geometries you are going to write with the cursor. For example, suppose you're going to insert geometries into a feature class that is in UTM coordinates. You're going to read geometries from a text file that contains state plane coordinates and insert them into this feature class. The spatial reference of the feature class (UTM) is different than the geometries you are going to read from the text file (state plane). When you open the insert cursor on the feature class, you set its spatial reference to state plane, declaring that you want the geometries you're inserting to be converted from state plane to UTM. Therefore, the only time you need to set the spatial reference of an insert or update cursor is when the geometries you are writing are in a different spatial reference than the cursor's feature class.
In the case of a search cursor, specifying a spatial reference different from the spatial reference of the cursor's feature class results in geometries that are transformed to the cursor's spatial reference.
The example below has a point feature class with a coordinate system of UTM zone 21 north, defined in its spatial reference. The script will produce a text file with the coordinates of the points in decimal degrees.
import arcpy # Describe a feature class with a geographic coordinate system # desc = arcpy.Describe("d:/base/data.gdb/latlongbnd") # Create search cursor. Use the spatial reference object from the # described feature class so geometries are returned in decimal degrees. # rows = arcpy.SearchCursor("d:/base/data.gdb/buildings", "", desc.spatialReference) # Open the file for output. This also creates the file if it does not exist. # out = open(arcpy.GetParameterAsText(0), "w") # Print the coordinates of each building point feature # for row in rows: # Create the geometry object # feat = row.shape # Get the geometry's point object. # pnt = feat.getPart() # Write the x,y coordinate to the output file # out.write(pnt.X + ";" + pnt.Y + "\n") # Close the output file # out.close()