Altering spatial references in Python

With ArcGIS 10.1, a spatial reference object can be created using a name or well-known ID (WKID).

# By name
sr = arcpy.SpatialReference('WGS 1984 UTM Zone 11N')

# By WKID
sr = arcpy.SpatialReference(32611)

However, once a spatial reference is created, many of the properties cannot be altered because they are read-only.

# Not possible
sr.centralMeridian = -110.0

Instead, if you need to change a property, you will need to take advantage of Python’s string manipulation capabilities. Since spatial reference properties can be expressed as well known strings, one solution is to export the spatial reference to a string, modify that string, and then use the altered string to create a new spatial reference.

import arcpy
import re
sr = arcpy.SpatialReference('WGS 1984 UTM Zone 11N')

# Change the central meridian.
sr.loadFromString(re.sub('PARAMETER\[\'Central_Meridian\'\,.+?]',
                         'PARAMETER\[\'Central_Meridian\',-120.0]',
                         sr.exportToString()))

References:
Well-known text representation of spatial reference systems

In addition to the documentation above, storage parameters like the coordinate domains and resolution, as well as tolerances are included in the spatial reference string.

List of Projected Coordinate system well-known IDs
List of Geographic Coordinate System well-known IDs

2 thoughts on “Altering spatial references in Python

  1. Why does this not work for single digit UTM zones when using the name?
    e.g.
    sr = arcpy.SpatialReference(‘WGS 1984 UTM Zone 9N’)

    it returns the following error:
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py”, line 927, in __init__
    self._arc_object.createFromFile(item)
    RuntimeError: ERROR 999999: Error executing function.

  2. nevermind. Figured out that you need to keep the padding so to speak and use a space.
    e.g.
    sr = arcpy.SpatialReference(‘WGS 1984 UTM Zone {0: =2d}N’.format(n))
    or
    sr = arcpy.SpatialReference(‘NAD 1927 UTM Zone {0: =2d}N’.format(n))
    or
    sr = arcpy.SpatialReference(‘NAD 1983 UTM Zone {0: =2d}N’.format(n))

    where n is your zone number.

    so it looks like ‘NAD 1983 UTM Zone 3N’

    note the extra space between ‘Zone’ and 3.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s